-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls-bench.py
executable file
·208 lines (181 loc) · 7.72 KB
/
ls-bench.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
#
import sys
import os
import argparse
import time
from datetime import datetime
from zoneinfo import ZoneInfo
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from Classes.AppConfig import AppConfig
from Classes.Benchmarks.LiteServer.GetAccountThread import GetAccountThread
from Classes.Benchmarks.HttpApi.ShardsThread import ShardsThread
from Classes.Benchmarks.HttpApi.GetBlockTransactionsThread import GetBlockTransactions
from queue import Queue
import pandas as pd
def run():
description = 'Executes benchmark of HTTP API instance.'
parser = argparse.ArgumentParser(formatter_class = argparse.RawDescriptionHelpFormatter,
description = description)
parser.add_argument('-b', '--benchmark',
required=True,
type=str,
default=None,
dest='benchmark',
action='store',
help='Benchmark definition json file, REQUIRED')
parser.add_argument('-a', '--addr',
required=True,
type=str,
dest='ls_addr',
action='store',
help='LiteServer address:port - REQUIRED')
parser.add_argument('-B', '--b64',
required=True,
type=str,
dest='ls_key',
action='store',
help='LiteServer base64 key as encoded in network config - REQUIRED')
parser.add_argument('-R', '--runtime',
required=False,
type=int,
default=None,
dest='max_runtime',
action='store',
help='Run benchmark for set time in seconds, OPTIONAL, default: unlimited')
parser.add_argument('-v', '--verbosity',
required=False,
type=int,
default=0,
dest='verbosity',
action='store',
help='Verbosity 0 - 3 - OPTIONAL, default: 0')
print("Initializing and loading databases, please wait...")
cfg = AppConfig(parser.parse_args())
start_timestamp = time.time()
queues = {}
th_db = []
for element in cfg.config["benchmarks"]:
e_id = "{}:{}".format(element["method"],element["id"])
queues[e_id] = {
'success': Queue(),
'error': Queue()
}
cfg.log.log(os.path.basename(__file__), 3, "Configuring benchmark {} with {} thread(s)".format(e_id,element["threads"]))
if element["method"] == 'getAccount':
for idx in range(element["threads"]):
th_db.append(
GetAccountThread(
id = idx,
config=cfg.config,
ls_addr=cfg.args.ls_addr,
ls_key=cfg.args.ls_key,
data=cfg.data,
log=cfg.log,
gk=cfg.gk,
queues=queues[e_id],
params=element["params"],
max_rps=element["thread_max_rps"]
)
)
else:
cfg.log.log(os.path.basename(__file__), 1, "Unknown benchmark method {}".format(element["method"]))
sys.exit(1)
cfg.log.log(os.path.basename(__file__), 3, "Starting {} threads".format(len(th_db)))
for element in th_db:
element.start()
stats = {
'start_timestamp': time.time(),
'threads_count': len(th_db),
'benchmarks': {},
'errors': {}
}
while True:
if cfg.gk.kill_now:
cfg.log.log(os.path.basename(__file__), 3, "Exiting main loop")
break
for benchmark_id, benchmark_queues in queues.items():
if benchmark_id not in stats['benchmarks']:
stats['benchmarks'][benchmark_id] = {
'requests': {
'success': 0,
'error': 0
},
'latency': {
'min': 9999999,
'max': 0,
'sum': 0
},
'errors': {}
}
for idx in range(benchmark_queues['success'].qsize()):
result = benchmark_queues['success'].get()
stats['benchmarks'][benchmark_id]['requests']['success'] += 1
stats['benchmarks'][benchmark_id]['latency']['sum'] += result
if result < stats['benchmarks'][benchmark_id]['latency']['min']:
stats['benchmarks'][benchmark_id]['latency']['min'] = result
elif result > stats['benchmarks'][benchmark_id]['latency']['max']:
stats['benchmarks'][benchmark_id]['latency']['max'] = result
for idx in range(benchmark_queues['error'].qsize()):
result = benchmark_queues['error'].get()
stats['benchmarks'][benchmark_id]['requests']['error'] += 1
if result['error'] not in stats['errors']:
stats['errors'][result['error']] = 0
stats['errors'][result['error']] +=1
print_stats(cfg, stats)
time.sleep(1)
if cfg.args.max_runtime and (time.time() - stats['start_timestamp']) > cfg.args.max_runtime:
cfg.gk.kill_now = True
sys.exit(0)
def print_stats(cfg, stats):
os.system("clear")
runtime = round(time.time()-stats['start_timestamp'])
print("Benchmark Statistics")
print("-"*100)
print("Remote : {}".format(cfg.args.ls_addr))
print("Start Time : {}".format(datetime.fromtimestamp(stats['start_timestamp'], tz=ZoneInfo("UTC"))))
print("Current Time : {}".format(datetime.now(tz=ZoneInfo("UTC"))))
if cfg.args.max_runtime:
print("Runtime : {} of {} seconds".format(runtime, cfg.args.max_runtime))
else:
print("Runtime : {} seconds".format(runtime))
print("Total threads: {}".format(stats['threads_count']))
index = []
rows = []
print("-"*100)
for benchmark_id, benchmark_data in stats['benchmarks'].items():
index.append("{}".format(benchmark_id))
requests_count = benchmark_data['requests']['success'] + benchmark_data['requests']['error']
if requests_count and runtime:
data = [
round(requests_count / runtime, 2),
"{}ms".format(round(benchmark_data['latency']['min'])),
"{}ms".format(round(benchmark_data['latency']['sum']/requests_count)),
"{}ms".format(round(benchmark_data['latency']['max'])),
benchmark_data['requests']['success'],
benchmark_data['requests']['error'],
"{}%".format(round((benchmark_data['requests']['error'] / requests_count) * 100))
]
else:
data = [0,"0ms","0ms","0ms",0,0,"0%"]
rows.append(data)
pd.set_option('display.max_rows', 10000)
table = pd.DataFrame(rows, columns=['RPS', 'L.Min', 'L.Avg','L.Max','Success', 'Failure', 'F.Rate'], index=index)
print(table)
print("-"*100)
print("\n")
print("Errors")
print("-"*100)
if not stats['errors']:
print("None")
else:
index = []
rows = []
for error, count in stats['errors'].items():
index.append(error)
rows.append([count])
pd.set_option('display.max_rows', 10000)
table = pd.DataFrame(rows, columns=['Count'], index=index)
print(table)
if __name__ == '__main__':
run()