forked from aleksuss/tx-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx_stats.py
More file actions
112 lines (87 loc) · 2.9 KB
/
tx_stats.py
File metadata and controls
112 lines (87 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# This scripts outputs TPS stats in runtime
# Run example: ./tx_stats.py node.hostname.com:8080
import requests
import sys
from datetime import datetime
from time import sleep
count_blocks = 10
def get_hostname():
if len(sys.argv) < 2:
print("Provide a hostname of the node")
exit(1)
if "http" in sys.argv[1]:
return sys.argv[1]
else:
return "http://" + sys.argv[1]
def parse_datetime(d_time):
d_time_parts = d_time[:-1].split('.')
return datetime.strptime(d_time_parts[0] + '.' + d_time_parts[1][:5], "%Y-%m-%dT%H:%M:%S.%f")
def update_stats(stats, data):
times = data["times"]
for i, block in enumerate(data["blocks"]):
height = block["height"]
tx_count = block["tx_count"]
if height in stats: # skip existence entries
continue
if tx_count == 0: # skip blocks with no transactions
continue
if (
i == len(data["blocks"]) - 1
): # skip last block info, we won't be able to calculate time delta
continue
block_time = (
parse_datetime(times[i]) - parse_datetime(times[i + 1])
).total_seconds()
stats[height] = tx_count / block_time
def calc_min_tps(stats):
try:
return int(min(stats.values()))
except ValueError:
return 0
def calc_max_tps(stats):
try:
return int(max(stats.values()))
except ValueError:
return 0
def calc_average_tps(stats):
count = len(stats)
if count == 0:
return 0
return int(sum(stats.values()) / count)
def calc_current_tps(data):
times = data["times"]
delta_time = parse_datetime(times[0]) - parse_datetime(times[1])
return int(data["blocks"][0]["tx_count"] / delta_time.total_seconds())
def main():
hostname = get_hostname()
blocks_url = "{}/api/explorer/v1/blocks?count={}&add_blocks_time=true".format(
hostname, count_blocks
)
stats = dict()
print("TPS statistics for host: %s" % hostname)
while True:
try:
response = requests.get(blocks_url)
if response.status_code == 200:
data = response.json()
update_stats(stats, data)
min_tps = calc_min_tps(stats)
max_tps = calc_max_tps(stats)
avrg_tps = calc_average_tps(stats)
current_tps = calc_current_tps(data)
last_height = int(data["range"]["end"])
print(
"min: {}, max: {}, avrg: {}, current: {}, last height: {}".format(
min_tps, max_tps, avrg_tps, current_tps, last_height
),
end="\r",
)
else:
print("Bad request")
sleep(1)
except KeyboardInterrupt:
print("Exit...")
exit(0)
if __name__ == "__main__":
main()