4
4
from __future__ import absolute_import , print_function
5
5
6
6
import argparse
7
- import logging
8
7
import pprint
9
8
import sys
10
9
import threading
10
+ import time
11
11
import traceback
12
12
13
- from kafka .vendor .six .moves import range
14
-
15
- from kafka import KafkaConsumer , KafkaProducer
16
- from test .fixtures import KafkaFixture , ZookeeperFixture
17
-
18
- logging .basicConfig (level = logging .ERROR )
19
-
20
-
21
- def start_brokers (n ):
22
- print ('Starting {0} {1}-node cluster...' .format (KafkaFixture .kafka_version , n ))
23
- print ('-> 1 Zookeeper' )
24
- zk = ZookeeperFixture .instance ()
25
- print ('---> {0}:{1}' .format (zk .host , zk .port ))
26
- print ()
27
-
28
- partitions = min (n , 3 )
29
- replicas = min (n , 3 )
30
- print ('-> {0} Brokers [{1} partitions / {2} replicas]' .format (n , partitions , replicas ))
31
- brokers = [
32
- KafkaFixture .instance (i , zk , zk_chroot = '' ,
33
- partitions = partitions , replicas = replicas )
34
- for i in range (n )
35
- ]
36
- for broker in brokers :
37
- print ('---> {0}:{1}' .format (broker .host , broker .port ))
38
- print ()
39
- return brokers
13
+ from kafka import KafkaConsumer
40
14
41
15
42
16
class ConsumerPerformance (object ):
43
-
44
17
@staticmethod
45
18
def run (args ):
46
19
try :
@@ -53,28 +26,17 @@ def run(args):
53
26
pass
54
27
if v == 'None' :
55
28
v = None
29
+ elif v == 'False' :
30
+ v = False
31
+ elif v == 'True' :
32
+ v = True
56
33
props [k ] = v
57
34
58
- if args .brokers :
59
- brokers = start_brokers (args .brokers )
60
- props ['bootstrap_servers' ] = ['{0}:{1}' .format (broker .host , broker .port )
61
- for broker in brokers ]
62
- print ('---> bootstrap_servers={0}' .format (props ['bootstrap_servers' ]))
63
- print ()
64
-
65
- print ('-> Producing records' )
66
- record = bytes (bytearray (args .record_size ))
67
- producer = KafkaProducer (compression_type = args .fixture_compression ,
68
- ** props )
69
- for i in range (args .num_records ):
70
- producer .send (topic = args .topic , value = record )
71
- producer .flush ()
72
- producer .close ()
73
- print ('-> OK!' )
74
- print ()
75
-
76
35
print ('Initializing Consumer...' )
36
+ props ['bootstrap_servers' ] = args .bootstrap_servers
77
37
props ['auto_offset_reset' ] = 'earliest'
38
+ if 'group_id' not in props :
39
+ props ['group_id' ] = 'kafka-consumer-benchmark'
78
40
if 'consumer_timeout_ms' not in props :
79
41
props ['consumer_timeout_ms' ] = 10000
80
42
props ['metrics_sample_window_ms' ] = args .stats_interval * 1000
@@ -92,14 +54,18 @@ def run(args):
92
54
print ('-> OK!' )
93
55
print ()
94
56
57
+ start_time = time .time ()
95
58
records = 0
96
59
for msg in consumer :
97
60
records += 1
98
61
if records >= args .num_records :
99
62
break
100
- print ('Consumed {0} records' .format (records ))
101
63
64
+ end_time = time .time ()
102
65
timer_stop .set ()
66
+ timer .join ()
67
+ print ('Consumed {0} records' .format (records ))
68
+ print ('Execution time:' , end_time - start_time , 'secs' )
103
69
104
70
except Exception :
105
71
exc_info = sys .exc_info ()
@@ -143,32 +109,27 @@ def get_args_parser():
143
109
parser = argparse .ArgumentParser (
144
110
description = 'This tool is used to verify the consumer performance.' )
145
111
112
+ parser .add_argument (
113
+ '--bootstrap-servers' , type = str , nargs = '+' , default = (),
114
+ help = 'host:port for cluster bootstrap servers' )
146
115
parser .add_argument (
147
116
'--topic' , type = str ,
148
- help = 'Topic for consumer test' ,
117
+ help = 'Topic for consumer test (default: kafka-python-benchmark-test) ' ,
149
118
default = 'kafka-python-benchmark-test' )
150
119
parser .add_argument (
151
120
'--num-records' , type = int ,
152
- help = 'number of messages to consume' ,
121
+ help = 'number of messages to consume (default: 1000000) ' ,
153
122
default = 1000000 )
154
- parser .add_argument (
155
- '--record-size' , type = int ,
156
- help = 'message size in bytes' ,
157
- default = 100 )
158
123
parser .add_argument (
159
124
'--consumer-config' , type = str , nargs = '+' , default = (),
160
125
help = 'kafka consumer related configuration properties like '
161
126
'bootstrap_servers,client_id etc..' )
162
127
parser .add_argument (
163
128
'--fixture-compression' , type = str ,
164
129
help = 'specify a compression type for use with broker fixtures / producer' )
165
- parser .add_argument (
166
- '--brokers' , type = int ,
167
- help = 'Number of kafka brokers to start' ,
168
- default = 0 )
169
130
parser .add_argument (
170
131
'--stats-interval' , type = int ,
171
- help = 'Interval in seconds for stats reporting to console' ,
132
+ help = 'Interval in seconds for stats reporting to console (default: 5) ' ,
172
133
default = 5 )
173
134
parser .add_argument (
174
135
'--raw-metrics' , action = 'store_true' ,
0 commit comments