55from collections import Counter
66import random
77
8+ from aiokafka .partitioner import DefaultPartitioner , RoundRobinPartitioner
9+
810
911class Benchmark :
1012
@@ -26,9 +28,19 @@ def __init__(self, args):
2628 else :
2729 self ._is_transactional = False
2830
31+ if args .partitioner == "default" :
32+ self ._producer_kwargs ["partitioner" ] = DefaultPartitioner ()
33+ elif args .partitioner == "round-robin" :
34+ self ._producer_kwargs ["partitioner" ] = RoundRobinPartitioner ()
35+
36+ if args .key :
37+ self ._key = b"abc"
38+ else :
39+ self ._key = None
40+
2941 self .transaction_size = args .transaction_size
3042
31- self ._partition = args .partition
43+ self ._partition = args .partition if args . partition != - 1 else None
3244 self ._stats_interval = 1
3345 self ._stats = [Counter ()]
3446
@@ -76,7 +88,7 @@ async def bench_simple(self):
7688 if not self ._is_transactional :
7789 for i in range (self ._num ):
7890 # payload[i % self._size] = random.randint(0, 255)
79- await producer .send (topic , payload , partition = partition )
91+ await producer .send (topic , payload , partition = partition , key = self . _key )
8092 self ._stats [- 1 ]['count' ] += 1
8193 else :
8294 for i in range (self ._num // transaction_size ):
@@ -117,7 +129,7 @@ def parse_args():
117129 help = 'Topic to produce messages to. Default {default}.' )
118130 parser .add_argument (
119131 '--partition' , type = int , default = 0 ,
120- help = 'Partition to produce messages to. Default {default}.' )
132+ help = 'Partition to produce messages to. Default {default}. Set to -1 to omit. ' )
121133 parser .add_argument (
122134 '--uvloop' , action = 'store_true' ,
123135 help = 'Use uvloop instead of asyncio default loop.' )
@@ -130,6 +142,12 @@ def parse_args():
130142 parser .add_argument (
131143 '--transaction-size' , type = int , default = 100 ,
132144 help = 'Number of messages in transaction' )
145+ parser .add_argument (
146+ '--partitioner' , type = str , default = "default" , choices = ["default" , "round-robin" ],
147+ help = 'Partitioner, either `default` or `round-robin`' )
148+ parser .add_argument (
149+ '--key' , action = 'store_true' ,
150+ help = 'Whether to use a partitioning key' )
133151 return parser .parse_args ()
134152
135153
0 commit comments