github.com/annchain/OG@v0.0.9/scripts/monitor/tps.py (about) 1 import datetime 2 import json 3 4 import websocket 5 6 try: 7 import thread 8 except ImportError: 9 import _thread as thread 10 11 start_count_time = datetime.datetime.now() 12 last_count_time = datetime.datetime.now() 13 sum_counted = 0 14 sum_tocount = 0 15 16 17 def hosts(fname): 18 with open(fname) as f: 19 return [line.strip() for line in f] 20 21 22 def on_message(ws, message): 23 global sum_tocount, last_count_time, sum_counted 24 j = json.loads(message) 25 if j['type'] == 'new_unit': 26 seconds = (datetime.datetime.now() - last_count_time).total_seconds() 27 if seconds < 1: 28 sum_tocount += len(j['nodes']) 29 print(len(j['nodes'])) 30 else: 31 sum_counted += sum_tocount 32 tps = float(sum_tocount) / seconds 33 tps_start = float(sum_counted) / (datetime.datetime.now() - start_count_time).total_seconds() 34 print("TPS: %0.3f From Start: %0.3f" % (tps, tps_start)) 35 last_count_time = datetime.datetime.now() 36 sum_tocount = 0 37 38 39 def on_error(ws, error): 40 print(error) 41 42 43 def on_close(ws): 44 print("### closed ###") 45 46 47 def on_open(ws): 48 def run(*args): 49 ws.send(json.dumps({"event": "new_unit"})) 50 ws.send(json.dumps({"event": "confirmed"})) 51 52 thread.start_new_thread(run, ()) 53 54 55 if __name__ == "__main__": 56 websocket.enableTrace(True) 57 host_ips = hosts('hosts') 58 # host_ips = ['127.0.0.1'] 59 60 ws = websocket.WebSocketApp("ws://%s:30002/ws" % (host_ips[4]), 61 on_message=on_message, 62 on_error=on_error, 63 on_close=on_close) 64 ws.on_open = on_open 65 ws.run_forever()