Auto-generated patch by python-black
[integration/test.git] / tools / mdsal_benchmark / rpcbenchmark.py
index 9c32ae48d9531f84dc5d0019e83f8e8d25dd4ce8..563ac95caf5de5ffcbc243741681d9ba3094df65 100755 (executable)
@@ -36,9 +36,9 @@ def send_test_request(operation, clients, servers, payload_size, iterations):
     :return: Result from the test request REST call (json)
     """
     url = BASE_URL + "operations/rpcbenchmark:start-test"
-    postheaders = {'content-type': 'application/json', 'Accept': 'application/json'}
+    postheaders = {"content-type": "application/json", "Accept": "application/json"}
 
-    test_request_template = '''{
+    test_request_template = """{
         "input": {
             "operation": "%s",
             "num-clients": "%s",
@@ -46,14 +46,22 @@ def send_test_request(operation, clients, servers, payload_size, iterations):
             "payload-size": "%s",
             "iterations": "%s"
         }
-    }'''
-    data = test_request_template % (operation, clients, servers, payload_size, iterations)
-    r = requests.post(url, data, headers=postheaders, stream=False, auth=('admin', 'admin'))
-    result = {u'http-status': r.status_code}
+    }"""
+    data = test_request_template % (
+        operation,
+        clients,
+        servers,
+        payload_size,
+        iterations,
+    )
+    r = requests.post(
+        url, data, headers=postheaders, stream=False, auth=("admin", "admin")
+    )
+    result = {u"http-status": r.status_code}
     if r.status_code == 200:
-        result = dict(result.items() + json.loads(r.content)['output'].items())
+        result = dict(result.items() + json.loads(r.content)["output"].items())
     else:
-        print('Error %s, %s' % (r.status_code, r.content))
+        print("Error %s, %s" % (r.status_code, r.content))
     return result
 
 
@@ -67,12 +75,22 @@ def print_results(run_type, idx, res):
                 test run
     :return: None
     """
-    print('%s #%d: Ok: %d, Error: %d, Rate: %d, Exec time: %d' %
-          (run_type, idx,
-           res[u'global-rtc-client-ok'], res[u'global-rtc-client-error'], res[u'rate'], res[u'exec-time']))
-
-
-def run_test(warmup_runs, test_runs, operation, clients, servers, payload_size, iterations):
+    print(
+        "%s #%d: Ok: %d, Error: %d, Rate: %d, Exec time: %d"
+        % (
+            run_type,
+            idx,
+            res[u"global-rtc-client-ok"],
+            res[u"global-rtc-client-error"],
+            res[u"rate"],
+            res[u"exec-time"],
+        )
+    )
+
+
+def run_test(
+    warmup_runs, test_runs, operation, clients, servers, payload_size, iterations
+):
     """
     Execute a benchmark test. Performs the JVM 'wamrup' before the test, runs
     the specified number of dsbenchmark test runs and computes the average time
@@ -92,67 +110,112 @@ def run_test(warmup_runs, test_runs, operation, clients, servers, payload_size,
 
     for idx in range(warmup_runs):
         res = send_test_request(operation, clients, servers, payload_size, iterations)
-        print_results('WARM-UP', idx, res)
+        print_results("WARM-UP", idx, res)
 
     for idx in range(test_runs):
         res = send_test_request(operation, clients, servers, payload_size, iterations)
-        print_results('TEST', idx, res)
-        total_exec_time += res['exec-time']
-        total_rate += res['rate']
+        print_results("TEST", idx, res)
+        total_exec_time += res["exec-time"]
+        total_rate += res["rate"]
 
     return total_exec_time / test_runs, total_rate / test_runs
 
 
 if __name__ == "__main__":
-    parser = argparse.ArgumentParser(description='RPC Benchmarking')
+    parser = argparse.ArgumentParser(description="RPC Benchmarking")
 
     # Host Config
-    parser.add_argument("--host", default="localhost", help="IP of the target host where benchmarks will be run.")
-    parser.add_argument("--port", type=int, default=8181, help="The port number of target host.")
+    parser.add_argument(
+        "--host",
+        default="localhost",
+        help="IP of the target host where benchmarks will be run.",
+    )
+    parser.add_argument(
+        "--port", type=int, default=8181, help="The port number of target host."
+    )
 
     # Test Parameters
-    parser.add_argument("--operation", choices=["GLOBAL-RTC", "ROUTED-RTC"], default='GLOBAL-RTC',
-                        help='RPC and client type. RPC can be global or routcan be run-to-completion (RTC).'
-                             '(default: GLOBAL-RTC - Global RPC, Run-to-completion client)')
-    parser.add_argument("--warm", type=int, default=10, help='The number of warm-up runs before the measured test runs'
-                                                             '(Default 10)')
-    parser.add_argument("--run", type=int, default=10,
-                        help='The number of measured test runs. Reported results are based on these average of all'
-                             " measured runs. (Default 10)")
-    parser.add_argument("--clients", type=int, nargs='+', default=[1, 2, 4, 8, 16, 32, 64],
-                        help='The number of test RPC clients to start. (Default 10)')
-    parser.add_argument("--servers", type=int, nargs='+', default=[1, 2, 4, 8, 16, 32, 64],
-                        help='The number of routed RPC servers to start in the routed RPC test. Ignored in the global '
-                             'RPC test. (Default 10)')
-    parser.add_argument("--iterations", type=int, default=10, help='The number requests that each RPC client issues '
-                                                                   'during the test run. (Default 10)')
-    parser.add_argument("--payload", type=int, default=10, help='Payload size for the RPC - number of elements in a '
-                                                                'simple integer list. (Default 10)')
+    parser.add_argument(
+        "--operation",
+        choices=["GLOBAL-RTC", "ROUTED-RTC"],
+        default="GLOBAL-RTC",
+        help="RPC and client type. RPC can be global or routcan be run-to-completion (RTC)."
+        "(default: GLOBAL-RTC - Global RPC, Run-to-completion client)",
+    )
+    parser.add_argument(
+        "--warm",
+        type=int,
+        default=10,
+        help="The number of warm-up runs before the measured test runs" "(Default 10)",
+    )
+    parser.add_argument(
+        "--run",
+        type=int,
+        default=10,
+        help="The number of measured test runs. Reported results are based on these average of all"
+        " measured runs. (Default 10)",
+    )
+    parser.add_argument(
+        "--clients",
+        type=int,
+        nargs="+",
+        default=[1, 2, 4, 8, 16, 32, 64],
+        help="The number of test RPC clients to start. (Default 10)",
+    )
+    parser.add_argument(
+        "--servers",
+        type=int,
+        nargs="+",
+        default=[1, 2, 4, 8, 16, 32, 64],
+        help="The number of routed RPC servers to start in the routed RPC test. Ignored in the global "
+        "RPC test. (Default 10)",
+    )
+    parser.add_argument(
+        "--iterations",
+        type=int,
+        default=10,
+        help="The number requests that each RPC client issues "
+        "during the test run. (Default 10)",
+    )
+    parser.add_argument(
+        "--payload",
+        type=int,
+        default=10,
+        help="Payload size for the RPC - number of elements in a "
+        "simple integer list. (Default 10)",
+    )
 
     args = parser.parse_args()
     BASE_URL = "http://%s:%d/restconf/" % (args.host, args.port)
 
-    if args.operation == 'GLOBAL-RTC':
+    if args.operation == "GLOBAL-RTC":
         servers = [1]
     else:
         servers = args.servers
 
     # Run the benchmark tests and collect data in a csv file for import into a graphing software
-    f = open('test.csv', 'wt')
+    f = open("test.csv", "wt")
     try:
         writer = csv.writer(f)
         rate_matrix = []
 
         for svr in servers:
-            rate_row = ['']
+            rate_row = [""]
             for client in args.clients:
-                exec_time, rate = \
-                    run_test(args.warm, args.run, args.operation, client, svr, args.payload, args.iterations)
+                exec_time, rate = run_test(
+                    args.warm,
+                    args.run,
+                    args.operation,
+                    client,
+                    svr,
+                    args.payload,
+                    args.iterations,
+                )
                 rate_row.append(rate)
             rate_matrix.append(rate_row)
         print(rate_matrix)
 
-        writer.writerow(('RPC Rates:', ''))
+        writer.writerow(("RPC Rates:", ""))
         writer.writerows(rate_matrix)
     finally:
         f.close()