|
4 | 4 | import sys
|
5 | 5 | import subprocess
|
6 | 6 | import unittest
|
| 7 | +import numpy as np |
| 8 | +import time |
7 | 9 |
|
8 | 10 | if len(sys.argv) == 2:
|
9 | 11 | raw_url = sys.argv[1]
|
|
17 | 19 | MSG_STATUS_CODE_405 = "[405] Method Not Allowed"
|
18 | 20 | CURL_EMPTY_REPLY = "000"
|
19 | 21 |
|
| 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 | + |
20 | 66 |
|
21 | 67 | def is_addr_trytes(trytes):
|
22 | 68 | tryte_alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ9"
|
@@ -49,7 +95,6 @@ def API(get_query, get_data=None, post_data=None):
|
49 | 95 | stdout=subprocess.PIPE,
|
50 | 96 | stderr=subprocess.PIPE)
|
51 | 97 | out, err = p.communicate()
|
52 |
| - # print("err = " + str(err)) |
53 | 98 | response = str(out.decode('ascii'))
|
54 | 99 | else:
|
55 | 100 | print("Wrong request method")
|
@@ -153,3 +198,12 @@ def test_mam_recv_msg(self):
|
153 | 198 | # Run all the API Test here
|
154 | 199 | if __name__ == '__main__':
|
155 | 200 | 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