Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 92c2486

Browse files
committed
feat: Add MAM regression test time cost statistics
The functions in class Time_Consumption() calculate how long does each API cost. Each API is called for 100 times, and then, calculate its average time costs and time cost variance.
1 parent 0dae84d commit 92c2486

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

tests/regression/runner.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import sys
55
import subprocess
66
import unittest
7+
import numpy as np
8+
import time
79

810
if len(sys.argv) == 2:
911
raw_url = sys.argv[1]
@@ -17,6 +19,50 @@
1719
MSG_STATUS_CODE_405 = "[405] Method Not Allowed"
1820
CURL_EMPTY_REPLY = "000"
1921

22+
TIMES_TOTAL = 100
23+
24+
25+
def eval_stat(time_cost, func_name):
26+
avg = np.average(time_cost)
27+
var = np.var(time_cost)
28+
print("Average Elapsed Time of " + str(func_name) + ":" + str(avg) +
29+
" sec")
30+
print("With the range +- " + str(2 * var) +
31+
"sec, including 95% of API call time consumption")
32+
33+
34+
class Time_Consumption():
35+
def test_mam_send_msg(self):
36+
payload = "Who are we? Just a speck of dust within the galaxy?"
37+
post_data = {"message": payload}
38+
post_data_json = json.dumps(post_data)
39+
40+
time_cost = []
41+
for i in range(TIMES_TOTAL):
42+
start_time = time.time()
43+
API("/mam/", post_data=post_data_json)
44+
time_cost.append(time.time() - start_time)
45+
46+
eval_stat(time_cost, "mam send message")
47+
48+
def test_mam_recv_msg(self):
49+
payload = "Who are we? Just a speck of dust within the galaxy?"
50+
post_data = {"message": payload}
51+
post_data_json = json.dumps(post_data)
52+
response = API("/mam/", post_data=post_data_json)
53+
54+
res_split = response.split(", ")
55+
res_json = json.loads(res_split[0])
56+
bundle_hash = res_json["bundle_hash"]
57+
58+
time_cost = []
59+
for i in range(TIMES_TOTAL):
60+
start_time = time.time()
61+
API("/mam/", get_data=bundle_hash)
62+
time_cost.append(time.time() - start_time)
63+
64+
eval_stat(time_cost, "mam recv message")
65+
2066

2167
def is_addr_trytes(trytes):
2268
tryte_alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ9"
@@ -49,7 +95,6 @@ def API(get_query, get_data=None, post_data=None):
4995
stdout=subprocess.PIPE,
5096
stderr=subprocess.PIPE)
5197
out, err = p.communicate()
52-
# print("err = " + str(err))
5398
response = str(out.decode('ascii'))
5499
else:
55100
print("Wrong request method")
@@ -153,3 +198,12 @@ def test_mam_recv_msg(self):
153198
# Run all the API Test here
154199
if __name__ == '__main__':
155200
unittest.main(argv=['first-arg-is-ignored'], exit=False)
201+
202+
# Run all the Time_Consumption() tests
203+
f = Time_Consumption()
204+
public_method_names = [
205+
method for method in dir(f) if callable(getattr(f, method))
206+
if not method.startswith('_')
207+
] # 'private' methods start from _
208+
for method in public_method_names:
209+
getattr(f, method)() # call

0 commit comments

Comments
 (0)