python librdkafka producer针对本机apache kafka producer执行

hl0ma9xz  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(396)

我正在用原生java实现测试apachekafka producer和python的合流kafka,看看哪一个具有最大的吞吐量。
我正在使用docker compose部署一个带有3个kafka代理和3个zookeeper示例的kafka集群。我的docker文件:https://paste.fedoraproject.org/paste/bn7rr2~yruiihz06o3q6vw/raw
这是一个非常简单的代码,其中大部分是python confluent kafka的默认选项,并在java producer中进行了一些配置更改,以匹配confluent kafka。
python代码:

from confluent_kafka import Producer

producer = Producer({'bootstrap.servers': 'kafka-1:19092,kafka-2:29092,kafka-3:39092', 'linger.ms': 300, "max.in.flight.requests.per.connection": 1000000, "queue.buffering.max.kbytes": 1048576, "message.max.bytes": 1000000,
    'default.topic.config': {'acks': "all"}})

ss = '0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357'

def f():
    import time
    start = time.time()
    for i in xrange(1000000):
        try:
            producer.produce('test-topic', ss)
        except Exception:
            producer.poll(1)
            try:
                producer.produce('test-topic', ss)
            except Exception:
                producer.flush(30)
                producer.produce('test-topic', ss)
        producer.poll(0)
    producer.flush(30)
    print(time.time() - start)

if __name__ == '__main__':
    f()

java实现。配置与librdkafka中的config相同。按照edenhill的建议更改了linger.ms和回调。

package com.amit.kafka;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.LongSerializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.nio.charset.Charset;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class KafkaProducerExampleAsync {

    private final static String TOPIC = "test-topic";
    private final static String BOOTSTRAP_SERVERS = "kafka-1:19092,kafka-2:29092,kafka-3:39092";

    private static Producer<String, String> createProducer() {
        int bufferMemory = 67108864;
        int batchSizeBytes = 1000000;
        String acks = "all";

        Properties props = new Properties();

        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS);
        props.put(ProducerConfig.CLIENT_ID_CONFIG, "KafkaExampleProducer");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class.getName());
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSizeBytes);
        props.put(ProducerConfig.LINGER_MS_CONFIG, 100);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
        props.put(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, 1000000);
        props.put(ProducerConfig.ACKS_CONFIG, acks);

        return new KafkaProducer<>(props);
    }

    static void runProducer(final int sendMessageCount) throws InterruptedException {
        final Producer<String, String> producer = createProducer();
        final String msg = "0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357";

        final ProducerRecord<String, String> record = new ProducerRecord<>(TOPIC, msg);
        final long[] new_time = new long[1];

        try {
            for (long index = 0; index < sendMessageCount; index++) {
                producer.send(record, new Callback() {
                    public void onCompletion(RecordMetadata metadata, Exception e) {
                        // This if-else is to only start timing this when first message reach kafka
                        if(e != null) {
                           e.printStackTrace();
                        } else {
                            if (new_time[0] == 0) {
                                new_time[0] = System.currentTimeMillis();
                            }
                        }
                    }
                });
            }
        } finally {
            // producer.flush();
            producer.close();
            System.out.printf("Total time %d ms\n", System.currentTimeMillis() - new_time[0]);
        }
    }

    public static void main(String... args) throws Exception {
        if (args.length == 0) {
            runProducer(1000000);
        } else {
            runProducer(Integer.parseInt(args[0]));
        }
    }
}

基准结果(经edenhill建议修改后编辑)

acks=0,消息:1000000
java :12.066
python:9.608秒
确认:全部,消息:1000000
java:45.763 11.917秒
python:14.3029秒
java实现的性能与python实现相同,即使在做了我能想到的所有更改以及下面的评论中edenhill建议的更改之后。
关于kafka在python中的性能,有各种各样的基准测试,但是我找不到任何librdkafka或python-kafka与apache-kafka的比较。
我有两个问题:
这个测试是否足以得出结论:使用1kb大小的默认配置和消息,librdkafka会更快?
有没有人有过将librdkafka与合流kafka进行比较的经验或来源(博客、文档等)?

w8f9ii69

w8f9ii691#

python客户端使用librdkakfa,它覆盖了kafka的一些默认配置。

Paramter = Kafka default
batch.size = 16384
max.in.flight.requests.per.connection = 5 (librdkafka's default is 1000000)

librdkafka中的message.max.bytes可能等效于max.request.size。
我认为在kafka的producerapi中没有librdkafka的queue.buffering.max.messages的等价物。如果你发现了什么就纠正我。
另外,从java程序中删除buffer.memory参数。
https://kafka.apache.org/documentation/#producerconfigshttpshttp://github.com/edenhill/librdkafka/blob/master/configuration.md
接下来的事情是java需要一些时间来加载类。所以你需要增加消息的数量。如果生成所有消息至少需要20-30分钟,那就太好了。然后可以比较java客户机和python客户机。
我喜欢比较python和java客户机的想法。继续在stackoverflow上发布结果。

相关问题