|
| 1 | +import sys |
| 2 | +import json |
| 3 | +import random |
| 4 | +import logging |
| 5 | +import requests |
| 6 | +import statistics |
| 7 | +import subprocess |
| 8 | + |
| 9 | +DEBUG_FLAG = False |
| 10 | +TIMES_TOTAL = 100 |
| 11 | +TIMEOUT = 100 # [sec] |
| 12 | +STATUS_CODE_500 = "500" |
| 13 | +STATUS_CODE_405 = "405" |
| 14 | +STATUS_CODE_404 = "404" |
| 15 | +STATUS_CODE_400 = "400" |
| 16 | +STATUS_CODE_200 = "200" |
| 17 | +EMPTY_REPLY = "000" |
| 18 | +LEN_TAG = 27 |
| 19 | +LEN_ADDR = 81 |
| 20 | +LEN_MSG_SIGN = 2187 |
| 21 | +TRYTE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ9" |
| 22 | +URL = "" |
| 23 | + |
| 24 | + |
| 25 | +def parse_cli_arg(): |
| 26 | + global URL |
| 27 | + if len(sys.argv) == 2: |
| 28 | + raw_url = sys.argv[1] |
| 29 | + elif len(sys.argv) == 4: |
| 30 | + raw_url = sys.argv[1] |
| 31 | + if sys.argv[2] == 'Y': |
| 32 | + DEBUG_FLAG = True |
| 33 | + |
| 34 | + # the 3rd arg is the option which determine if use the debugging mode of statistical tests |
| 35 | + if sys.argv[3] == 'Y': |
| 36 | + TIMES_TOTAL = 2 |
| 37 | + else: |
| 38 | + raw_url = "localhost:8000" |
| 39 | + URL = "http://" + raw_url |
| 40 | + |
| 41 | + |
| 42 | +def eval_stat(time_cost, func_name): |
| 43 | + avg = statistics.mean(time_cost) |
| 44 | + var = statistics.variance(time_cost) |
| 45 | + print("Average Elapsed Time of `" + str(func_name) + "`:" + str(avg) + |
| 46 | + " sec") |
| 47 | + print("With the range +- " + str(2 * var) + |
| 48 | + "sec, including 95% of API call time consumption") |
| 49 | + |
| 50 | + |
| 51 | +def fill_nines(trytes, output_len): |
| 52 | + out_str = trytes + "9" * (output_len - len(trytes)) |
| 53 | + |
| 54 | + return out_str |
| 55 | + |
| 56 | + |
| 57 | +def gen_rand_trytes(tryte_len): |
| 58 | + trytes = "" |
| 59 | + for i in range(tryte_len): |
| 60 | + trytes = trytes + TRYTE_ALPHABET[random.randint(0, 26)] |
| 61 | + return trytes |
| 62 | + |
| 63 | + |
| 64 | +def valid_trytes(trytes, trytes_len): |
| 65 | + if len(trytes) != trytes_len: |
| 66 | + return False |
| 67 | + |
| 68 | + for char in trytes: |
| 69 | + if char not in TRYTE_ALPHABET: |
| 70 | + return False |
| 71 | + |
| 72 | + return True |
| 73 | + |
| 74 | + |
| 75 | +def map_field(key, value): |
| 76 | + ret = {} |
| 77 | + for k, v in zip(key, value): |
| 78 | + ret.update({k: v}) |
| 79 | + return json.dumps(ret) |
| 80 | + |
| 81 | + |
| 82 | +def API(get_query, get_data=None, post_data=None): |
| 83 | + global URL |
| 84 | + try: |
| 85 | + response = {} |
| 86 | + if get_data is not None: |
| 87 | + r = requests.get(str(URL + get_query + get_data), timeout=TIMEOUT) |
| 88 | + response = {"content": r.text, "status_code": str(r.status_code)} |
| 89 | + |
| 90 | + elif post_data is not None: |
| 91 | + command = "curl " + str( |
| 92 | + URL + get_query |
| 93 | + ) + " -X POST -H 'Content-Type: application/json' -w \", %{http_code}\" -d '" + str( |
| 94 | + post_data) + "'" |
| 95 | + logging.debug("curl command = " + command) |
| 96 | + p = subprocess.Popen(command, |
| 97 | + shell=True, |
| 98 | + stdout=subprocess.PIPE, |
| 99 | + stderr=subprocess.PIPE) |
| 100 | + out, err = p.communicate() |
| 101 | + curl_response = str(out.decode('ascii')) |
| 102 | + response = { |
| 103 | + "content": curl_response.split(", ")[0], |
| 104 | + "status_code": curl_response.split(", ")[1] |
| 105 | + } |
| 106 | + else: |
| 107 | + logging.error("Wrong request method") |
| 108 | + response = None |
| 109 | + |
| 110 | + except BaseException: |
| 111 | + logging.error(URL, "Timeout!") |
| 112 | + logging.error('\n ' + repr(sys.exc_info())) |
| 113 | + return None |
| 114 | + if not response: |
| 115 | + response = None |
| 116 | + |
| 117 | + return response |
0 commit comments