1+ import requests
2+ import time
3+ import datetime
4+ import os
5+ import base64
6+ import urllib .parse
7+ import requests
8+ import argparse
9+ import re
10+
11+ def send_waku_msg (node_address , kbytes , pubsub_topic , content_topic ):
12+ # TODO dirty trick .replace("=", "")
13+ base64_payload = (base64 .b64encode (os .urandom (kbytes * 1000 )).decode ('ascii' )).replace ("=" , "" )
14+ print ("size message kBytes" , len (base64_payload ) * (3 / 4 )/ 1000 , "KBytes" )
15+ body = {
16+ "payload" : base64_payload ,
17+ "contentTopic" : content_topic ,
18+ "version" : 1 , # You can adjust the version as needed
19+ "timestamp" : int (time .time ())
20+ }
21+
22+ encoded_pubsub_topic = urllib .parse .quote (pubsub_topic , safe = '' )
23+
24+ url = f"{ node_address } /relay/v1/messages/{ encoded_pubsub_topic } "
25+ headers = {'content-type' : 'application/json' }
26+
27+ readable_time = datetime .datetime .utcnow ().strftime ('%Y-%m-%d %H:%M:%S.%f' )[:- 3 ]
28+ print ('[%s] Waku REST API: %s PubSubTopic: %s, ContentTopic: %s' % (readable_time , url , pubsub_topic , content_topic ))
29+ s_time = time .time ()
30+
31+ response = None
32+ readable_time = datetime .datetime .utcnow ().strftime ('%Y-%m-%d %H:%M:%S.%f' )[:- 3 ]
33+ try :
34+ print ('[%s] Sending request' % readable_time )
35+ response = requests .post (url , json = body , headers = headers )
36+ except Exception as e :
37+ print (f"Error sending request: { e } " )
38+
39+ if (response != None ):
40+ elapsed_ms = (time .time () - s_time ) * 1000
41+ readable_time = datetime .datetime .utcnow ().strftime ('%Y-%m-%d %H:%M:%S.%f' )[:- 3 ]
42+ print ('[%s] Response from %s: status:%s content:%s [%.4f ms.]' % (readable_time , node_address , \
43+ response .status_code , response .text , elapsed_ms ))
44+
45+ parser = argparse .ArgumentParser (description = '' )
46+
47+ # these flags are mutually exclusive, one or the other, never at once
48+ group = parser .add_mutually_exclusive_group (required = True )
49+ group .add_argument ('-sn' , '--single-node' , type = str , help = 'example: http://waku-simulator-nwaku-1:8645' )
50+ group .add_argument ('-mn' , '--multiple-nodes' , type = str , help = 'example: http://waku-simulator-nwaku-[1..10]:8645' )
51+
52+ # rest of araguments
53+ parser .add_argument ('-c' , '--content-topic' , type = str , help = 'content topic' , default = "my-ctopic" )
54+ parser .add_argument ('-p' , '--pubsub-topic' , type = str , help = 'pubsub topic' , default = "/waku/2/rs/66/0" )
55+ parser .add_argument ('-s' , '--msg-size-kbytes' , type = int , help = 'message size in kBytes' , default = 10 )
56+ parser .add_argument ('-d' , '--delay-seconds' , type = int , help = 'delay in second between messages' , required = 15 )
57+ args = parser .parse_args ()
58+
59+ print (args )
60+
61+ if args .single_node != None :
62+ print ("Injecting traffic to single node REST API:" , args .single_node )
63+
64+ # this simply converts from http://url_[1..5]:port to
65+ # [http://url_1:port or from http://url-[1..5]:port to
66+ # [http://url-1:port
67+ nodes = []
68+ if args .multiple_nodes :
69+ start , end = (int (x ) for x in re .search (r"\[(\d+)\.\.(\d+)\]" , args .multiple_nodes ).groups ())
70+
71+ if start is None or end is None :
72+ print ("Could not parse range of multiple_nodes argument" )
73+ exit
74+
75+ print ("Injecting traffic to multiple nodes REST APIs" )
76+ for i in range (end , start - 1 , - 1 ):
77+ nodes .append (re .sub (r"\[\d+\.\.\d+\]" , str (i ), args .multiple_nodes ))
78+
79+ for node in nodes :
80+ print (node )
81+
82+ while True :
83+ # calls are blocking
84+ # limited by the time it takes the REST API to reply
85+
86+ if args .single_node != None :
87+ send_waku_msg (args .single_node , args .msg_size_kbytes , args .pubsub_topic , args .content_topic )
88+
89+ if args .multiple_nodes != None :
90+ for node in nodes :
91+ send_waku_msg (node , args .msg_size_kbytes , args .pubsub_topic , args .content_topic )
92+
93+ readable_time = datetime .datetime .utcnow ().strftime ('%Y-%m-%d %H:%M:%S.%f' )[:- 3 ]
94+ print ('[%s] sleeping: %s seconds' % (readable_time , args .delay_seconds ))
95+ time .sleep (args .delay_seconds )
0 commit comments