77import nats
88
99try :
10- import uvloop
11- asyncio .set_event_loop_policy (uvloop .EventLoopPolicy ())
10+ import uvloop
11+
12+ asyncio .set_event_loop_policy (uvloop .EventLoopPolicy ())
1213except :
13- pass
14+ pass
1415
1516DEFAULT_FLUSH_TIMEOUT = 30
1617DEFAULT_NUM_MSGS = 100000
1718DEFAULT_MSG_SIZE = 16
1819DEFAULT_BATCH_SIZE = 100
1920HASH_MODULO = 1000
2021
22+
2123def show_usage ():
2224 message = """
2325Usage: pub_perf [options]
@@ -30,32 +32,34 @@ def show_usage():
3032 """
3133 print (message )
3234
35+
3336def show_usage_and_die ():
3437 show_usage ()
3538 sys .exit (1 )
3639
40+
3741async def main ():
3842 parser = argparse .ArgumentParser ()
39- parser .add_argument ('-n' , ' --count' , default = DEFAULT_NUM_MSGS , type = int )
40- parser .add_argument ('-s' , ' --size' , default = DEFAULT_MSG_SIZE , type = int )
41- parser .add_argument ('-S' , ' --subject' , default = ' test' )
42- parser .add_argument ('-b' , ' --batch' , default = DEFAULT_BATCH_SIZE , type = int )
43- parser .add_argument (' --servers' , default = [], action = ' append' )
43+ parser .add_argument ("-n" , " --count" , default = DEFAULT_NUM_MSGS , type = int )
44+ parser .add_argument ("-s" , " --size" , default = DEFAULT_MSG_SIZE , type = int )
45+ parser .add_argument ("-S" , " --subject" , default = " test" )
46+ parser .add_argument ("-b" , " --batch" , default = DEFAULT_BATCH_SIZE , type = int )
47+ parser .add_argument (" --servers" , default = [], action = " append" )
4448 args = parser .parse_args ()
4549
4650 data = []
4751 for i in range (0 , args .size ):
4852 s = "%01x" % randint (0 , 15 )
4953 data .append (s .encode ())
50- payload = b'' .join (data )
54+ payload = b"" .join (data )
5155
5256 servers = args .servers
5357 if len (args .servers ) < 1 :
5458 servers = ["nats://127.0.0.1:4222" ]
5559
5660 # Make sure we're connected to a server first..
5761 try :
58- nc = await nats .connect (servers , pending_size = 1024 * 1024 )
62+ nc = await nats .connect (servers , pending_size = 1024 * 1024 )
5963 except Exception as e :
6064 sys .stderr .write (f"ERROR: { e } " )
6165 show_usage_and_die ()
@@ -64,8 +68,11 @@ async def main():
6468 start = time .time ()
6569 to_send = args .count
6670
67- print ("Sending {} messages of size {} bytes on [{}]" .format (
68- args .count , args .size , args .subject ))
71+ print (
72+ "Sending {} messages of size {} bytes on [{}]" .format (
73+ args .count , args .size , args .subject
74+ )
75+ )
6976 while to_send > 0 :
7077 for i in range (0 , args .batch ):
7178 to_send -= 1
@@ -86,11 +93,14 @@ async def main():
8693 print (f"Server flush timeout after { DEFAULT_FLUSH_TIMEOUT } " )
8794
8895 elapsed = time .time () - start
89- mbytes = "%.1f" % (((args .size * args .count )/ elapsed ) / (1024 * 1024 ))
90- print ("\n Test completed : {} msgs/sec ({}) MB/sec" .format (
91- args .count / elapsed ,
92- mbytes ))
96+ mbytes = "%.1f" % (((args .size * args .count ) / elapsed ) / (1024 * 1024 ))
97+ print (
98+ "\n Test completed : {} msgs/sec ({}) MB/sec" .format (
99+ args .count / elapsed , mbytes
100+ )
101+ )
93102 await nc .close ()
94103
95- if __name__ == '__main__' :
96- asyncio .run (main ())
104+
105+ if __name__ == "__main__" :
106+ asyncio .run (main ())
0 commit comments