En 400-6655-581
5
返回列表
> 資源中心 > 技術(shù)干貨 | 怎么做Kafka的壓力測(cè)試(實(shí)操篇)

技術(shù)干貨 | 怎么做Kafka的壓力測(cè)試(實(shí)操篇)

2020-02-25瀏覽次數(shù):1314

什么是Kafka?

Kafka是一種高吞吐量的分布式發(fā)布訂閱消息系統(tǒng),它可以處理消費(fèi)者在它可以處理消費(fèi)者規(guī)模的網(wǎng)站中的所有動(dòng)作流數(shù)據(jù)。簡(jiǎn)單而言它就是一個(gè)消息緩存池,既可以往其中插數(shù)據(jù)(Producer),也可以從其中取數(shù)據(jù)(Consumer)。


Kafka通常以集群的方式進(jìn)行部署使用,每一個(gè)Kafka節(jié)點(diǎn)稱為一個(gè)Broker。Kafka中會(huì)有很多個(gè)消息隊(duì)列,每個(gè)消息隊(duì)列稱為一個(gè)topic。每個(gè)Topic也會(huì)分成很多Partition,分別存儲(chǔ)在每個(gè)Broker上面。


Kafka的broker信息以及會(huì)話偏移量存儲(chǔ)在Zookeeper中,當(dāng)某一topic配置replication屬性時(shí),Zookeeper還會(huì)決定哪一個(gè)Broker成為特定Partition的Leader。


Kafka的架構(gòu)


Kafka性能測(cè)試內(nèi)容


性能測(cè)試內(nèi)容:


kafka的測(cè)試主要分為producer端的吞吐量,consumer端的吞吐量,以及判斷影響兩者的因素。在實(shí)際測(cè)試環(huán)境中,需根據(jù)具體情況調(diào)整測(cè)試的數(shù)據(jù)量與參數(shù)。


性能測(cè)試工具:


我們使用Kafka自帶的性能測(cè)試工具完成本次測(cè)試,當(dāng)安裝了Apache 版本的Kafka時(shí),該測(cè)試工具的位置在安裝目錄的bin目錄下;當(dāng)安裝了CDH版本的Kafka時(shí),該測(cè)試工具一般位于Kafka Parcel路徑下,默認(rèn)位置為:/opt/cloudera/parcels/KAFKA/lib/kafka/bin。


這里我們用到的測(cè)試工具為:


kafka-producer-perf-test.sh  (生產(chǎn)端)

kafka-consumer-perf-test.sh  (消費(fèi)端)


性能測(cè)試環(huán)境:


三臺(tái)同樣的虛擬機(jī)作為kafka broker,IP地址為192.168.1.81-192.168.1.83,配置如下:


CPU       內(nèi)存       磁盤

4core     16G       100G

Producer端性能測(cè)試


a.Partition與速率的關(guān)系


方法:

建立兩個(gè)topic, 兩個(gè)topic除了partition數(shù)量不一致外其他參數(shù)完全一致,使用kafka-producer-perf-test.sh進(jìn)行測(cè)試。


過程:

首先建立兩個(gè)topic,分別為test_part1與test_part2:



kafka-topics.sh --zookeeper 192.168.1.81:2181 --create --topic test_part1 --partitions 1 --replication-factor 1


kafka-topics.sh --zookeeper 192.168.1.81:2181 --create --topic test_part2 --partitions 5 --replication-factor 1




test_part1進(jìn)行測(cè)試跑批:


kafka-producer-perf-test.sh --topic test_part1 --num-records 2000000 --record-size 200 --throughput 300000 --producer-props bootstrap.servers=192.168.1.82:9092


運(yùn)行結(jié)果:


2000000 records sent, 136211.945788 records/sec (25.98 MB/sec), 1002.59 ms avg latency, 2559.00 ms max latency, 801 ms 50th, 2271 ms 95th, 2538 ms 99th, 2553 ms 99.9th.


test_part2進(jìn)行測(cè)試跑批:


kafka-producer-perf-test.sh --topic test_part2 --num-records 2000000 --record-size 200 --throughput 300000 --producer-props bootstrap.servers=192.168.1.82:9092


運(yùn)行結(jié)果:


2000000 records sent, 187529.301453 records/sec (35.77 MB/sec), 520.00 ms avg latency, 2196.00 ms max latency, 336 ms 50th, 1922 ms 95th, 2159 ms 99th, 2190 ms 99.9th.


結(jié)論:Producer端傳輸效率和partition數(shù)成正比。



b.Replication與速率的關(guān)系


方法:

首先建立兩個(gè)topic, 兩個(gè)topic除了replication數(shù)量不一致外其他參數(shù)完全一致,使用kafka-producer-perf-test.sh進(jìn)行測(cè)試。


過程:

首先建立兩個(gè)topic,分別為test_rep1與test_rep2:



kafka-topics.sh --zookeeper 192.168.1.81:2181 --create --topic test_rep1 --partitions 1 --replication-factor 1


kafka-topics.sh --zookeeper 192.168.1.81:2181 --create --topic test_rep2 --partitions 1 --replication-factor 3



test_rep1進(jìn)行跑批測(cè)試:


kafka-producer-perf-test.sh --topic test_rep1 --num-records 2000000 --record-size


運(yùn)行結(jié)果:


2000000 records sent, 217438.573603 records/sec (41.47 MB/sec), 610.43 ms avg latency, 1387.00 ms max latency, 497 ms 50th, 1284 ms 95th, 1341 ms 99th, 1386 ms 99.9th.


test_rep2進(jìn)行跑批測(cè)試:


kafka-producer-perf-test.sh --topic test_rep2 --num-records 2000000 --record-size 200 --throughput 300000 --producer-props bootstrap.servers=192.168.1.82:9092


運(yùn)行結(jié)果:


2000000 records sent, 103423.311614 records/sec (19.73 MB/sec), 1353.03 ms avg latency, 4050.00 ms max latency, 1213 ms 50th, 3042 ms 95th, 3998 ms 99th, 4047 ms 99.9th


結(jié)論:Producer端傳輸效率和replication數(shù)量成反比。



C.傳輸?shù)臄?shù)據(jù)記錄大小與速率的關(guān)系


方法:

向一個(gè)topic寫入大小不同的數(shù)據(jù)記錄,檢驗(yàn)是否對(duì)速率有影響。


過程:

首先建立一個(gè)test topic:



kafka-topics.sh --zookeeper 192.168.1.81:2181 --create --topic test --partitions 3 --replication-factor 3


使用小數(shù)據(jù)塊跑批:


kafka-producer-perf-test.sh --topic test --num-records 1000000 --record-size 200 --throughput 300000 --producer-props bootstrap.servers=192.168.1.82:9092


運(yùn)行結(jié)果:


1000000 records sent, 149231.457991 records/sec (28.46 MB/sec), 814.24 ms avg latency, 1334.00 ms max latency, 810 ms 50th, 1290 ms 95th, 1327 ms 99th, 1333 ms 99.9th.


使用大數(shù)據(jù)塊跑批:


kafka-producer-perf-test.sh --topic test --num-records 1000000 --record-size 2048 --throughput 300000 --producer-props bootstrap.servers=192.168.1.82:9092


運(yùn)行結(jié)果:


1000000 records sent, 22161.155926 records/sec (43.28 MB/sec), 637.75 ms avg latency, 1598.00 ms max latency, 576 ms 50th, 959 ms 95th, 1547 ms 99th, 1580 ms 99.9th.


結(jié)論:Producer端傳輸數(shù)量和單條記錄大小成正比。


Customer端性能測(cè)試


由于kafka-consumer-perf-test.sh返回結(jié)果并不像producer端那么簡(jiǎn)潔易懂,這里解釋一下,這個(gè)命令會(huì)返回類似以下的結(jié)果:


2019-11-18 13:47:53:297, 2019-11-18 13:47:55:078, 190.7349, 107.0943, 1000000, 561482.3133


總共六個(gè)參數(shù):

第一個(gè)是開始時(shí)間;

第二個(gè)是結(jié)束時(shí)間;

第三個(gè)是本次消費(fèi)了多少M(fèi)B數(shù)據(jù);

第四個(gè)是本次消費(fèi)的速率MB/S;

第五個(gè)是總的消費(fèi)數(shù)據(jù)條數(shù);

第六個(gè)是每秒消費(fèi)數(shù)據(jù)條數(shù)。


a.Partition與速率的關(guān)系


方法:

首先建立兩個(gè)topic, 兩個(gè)topic除了partition數(shù)量不一致外其他參數(shù)完全一致,使用kafka-consumer-perf-test.sh進(jìn)行測(cè)試。


過程:

建立topic,這里沿用Customer端測(cè)試過程中建立的test_part1和test_part2.


test_part1進(jìn)行跑批測(cè)試:


kafka-consumer-perf-test.sh --topic test_part1 --messages 1000000 --threads 1  --num-fetch-threads 1 --zookeeper 192.168.1.81:2181


運(yùn)行結(jié)果:


2018-12-18 13:47:53:297, 2018-12-18 13:47:55:078, 190.7349, 107.0943, 1000000, 561482.3133


test_part2進(jìn)行跑批測(cè)試:


kafka-consumer-perf-test.sh --topic test_part2 --messages 1000000 --threads 1  --num-fetch-threads 1 --zookeeper 192.168.1.81:2181


運(yùn)行結(jié)果:


2018-12-18 13:51:43:887, 2018-12-18 13:51:45:619, 190.7349, 110.1241, 1000000, 577367.2055


結(jié)論:Customer端消費(fèi)效率和partitions數(shù)成正比(但影響不大)。


b.Threads與速率的關(guān)系


方法:

在一個(gè)topic中消費(fèi)相同的數(shù)據(jù),使用不同的threads,比較速率大小。


過程:

建立Topic,這里沿用Customer端測(cè)試過程中建立過的test。


使用一個(gè)線程消費(fèi)時(shí):


kafka-consumer-perf-test.sh --topic test --messages 500000 --threads 1  --num-fetch-threads 1 --zookeeper 192.168.1.81:2181



運(yùn)行結(jié)果:


2018-12-18 14:11:32:106, 2018-12-18 14:11:32:951, 95.3671, 112.8604, 500000, 591715.9763


使用三個(gè)線程消費(fèi)時(shí):


kafka-consumer-perf-test.sh --topic test --messages 500000 --threads 3  --num-fetch-threads 1 --zookeeper 192.168.1.81:2181


運(yùn)行結(jié)果:


2018-12-18 14:12:54:716, 2018-12-18 14:12:54:718, 95.3671, 47683.5270, 500000, 250000000.0000


使用六個(gè)線程消費(fèi)時(shí):


kafka-consumer-perf-test.sh --topic test --messages 500000 --threads 6  --num-fetch-threads 1 --zookeeper 192.168.1.81:2181


運(yùn)行結(jié)果:

2018-12-18 14:14:12:935, 2018-12-18 14:14:12:938, 95.3671, 31789.0180, 500000, 166666666.6667


結(jié)論:Cumsomer端消費(fèi)速率和thread成正比,但是達(dá)到一定數(shù)量(parttion數(shù)量)以后趨于平穩(wěn),再增加也不會(huì)繼續(xù)變大。


Kafka性能測(cè)試結(jié)論


由于每個(gè)kafka集群環(huán)境差異都很大,本文不代表所有情況。

但是一個(gè)通常的kafka生產(chǎn)集群以下特性是應(yīng)該達(dá)到的:



1.單個(gè)consumer的消費(fèi)速率必須遠(yuǎn)大于單個(gè)producer的生產(chǎn)速率。


2.單個(gè)broker數(shù)據(jù)生產(chǎn)效率不應(yīng)小于50M/s,否則增加JVM內(nèi)存,并增加緩沖區(qū)大小。