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

Commit

Permalink
feat: Add MAM regression test time cost statistics
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
howjmay committed Apr 21, 2019
1 parent 0dae84d commit 92c2486
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion tests/regression/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
import subprocess
import unittest
import numpy as np
import time

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

TIMES_TOTAL = 100


def eval_stat(time_cost, func_name):
avg = np.average(time_cost)
var = np.var(time_cost)
print("Average Elapsed Time of " + str(func_name) + ":" + str(avg) +
" sec")
print("With the range +- " + str(2 * var) +
"sec, including 95% of API call time consumption")


class Time_Consumption():
def test_mam_send_msg(self):
payload = "Who are we? Just a speck of dust within the galaxy?"
post_data = {"message": payload}
post_data_json = json.dumps(post_data)

time_cost = []
for i in range(TIMES_TOTAL):
start_time = time.time()
API("/mam/", post_data=post_data_json)
time_cost.append(time.time() - start_time)

eval_stat(time_cost, "mam send message")

def test_mam_recv_msg(self):
payload = "Who are we? Just a speck of dust within the galaxy?"
post_data = {"message": payload}
post_data_json = json.dumps(post_data)
response = API("/mam/", post_data=post_data_json)

res_split = response.split(", ")
res_json = json.loads(res_split[0])
bundle_hash = res_json["bundle_hash"]

time_cost = []
for i in range(TIMES_TOTAL):
start_time = time.time()
API("/mam/", get_data=bundle_hash)
time_cost.append(time.time() - start_time)

eval_stat(time_cost, "mam recv message")


def is_addr_trytes(trytes):
tryte_alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ9"
Expand Down Expand Up @@ -49,7 +95,6 @@ def API(get_query, get_data=None, post_data=None):
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
# print("err = " + str(err))
response = str(out.decode('ascii'))
else:
print("Wrong request method")
Expand Down Expand Up @@ -153,3 +198,12 @@ def test_mam_recv_msg(self):
# Run all the API Test here
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)

# Run all the Time_Consumption() tests
f = Time_Consumption()
public_method_names = [
method for method in dir(f) if callable(getattr(f, method))
if not method.startswith('_')
] # 'private' methods start from _
for method in public_method_names:
getattr(f, method)() # call

0 comments on commit 92c2486

Please sign in to comment.