Add 2048B file size cps rps tests in job specs for http-ldpreload-nginx-1_21_5.
[csit.git] / resources / tools / ab / ABTools.py
index 54aff19..b929b49 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2022 Intel and/or its affiliates.
+# Copyright (c) 2023 Intel and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
 
 """ab implementation into CSIT framework."""
 
-from robot.api import logger
-from resources.libraries.python.topology import NodeType
+from re import search
 from resources.libraries.python.Constants import Constants
-from resources.libraries.python.ssh import exec_cmd_no_error
+from resources.libraries.python.model.ExportResult import (
+    export_hoststack_results
+)
 from resources.libraries.python.OptionString import OptionString
+from resources.libraries.python.ssh import exec_cmd_no_error
+from resources.libraries.python.topology import NodeType
 
 
 class ABTools:
@@ -36,29 +39,29 @@ class ABTools:
         :rtype: OptionString
         """
         cmd = OptionString()
-        cmd.add(u"python3")
+        cmd.add("python3")
         dirname = f"{Constants.REMOTE_FW_DIR}/resources/tools/ab"
         cmd.add(f"{dirname}/ABFork.py")
-        cmd_options = OptionString(prefix=u"-")
+        cmd_options = OptionString(prefix="-")
         # Number of requests to perform.
-        cmd_options.add_with_value_from_dict(u"r", u"requests", kwargs)
+        cmd_options.add_with_value_from_dict("r", "requests", kwargs)
         # Server port number to use.
-        cmd_options.add_with_value_from_dict(u"p", u"port", kwargs)
+        cmd_options.add_with_value_from_dict("p", "port", kwargs)
         # Number of clients being processed at the same time.
-        cmd_options.add_with_value_from_dict(u"c", u"clients", kwargs)
+        cmd_options.add_with_value_from_dict("c", "clients", kwargs)
         # Filename to be requested from the servers.
-        cmd_options.add_with_value_from_dict(u"f", u"files", kwargs)
+        cmd_options.add_with_value_from_dict("f", "files", kwargs)
         # Server ip address.
-        cmd_options.add_with_value_from_dict(u"i", u"ip", kwargs)
+        cmd_options.add_with_value_from_dict("i", "ip", kwargs)
         # tg ip address.
-        cmd_options.add_with_value_from_dict(u"g", u"tip", kwargs)
+        cmd_options.add_with_value_from_dict("g", "tip", kwargs)
         # Specify SSL/TLS cipher suite.
-        cmd_options.add_with_value_from_dict(u"z", u"cipher", kwargs, default=0)
+        cmd_options.add_with_value_from_dict("z", "cipher", kwargs, default=0)
         # Specify SSL/TLS protocol.
-        cmd_options.add_with_value_from_dict(u"t", u"protocol", kwargs,
+        cmd_options.add_with_value_from_dict("t", "protocol", kwargs,
                                              default=0)
         # Mode: RPS or CPS.
-        cmd_options.add_with_value_from_dict(u"m", u"mode", kwargs)
+        cmd_options.add_with_value_from_dict("m", "mode", kwargs)
         return cmd.extend(cmd_options)
 
     @staticmethod
@@ -71,11 +74,11 @@ class ABTools:
             command is not available.
         """
 
-        if tg_node[u"type"] != NodeType.TG:
-            raise RuntimeError(u"Node type is not a TG!")
+        if tg_node["type"] != NodeType.TG:
+            raise RuntimeError("Node type is not a TG!")
 
-        cmd = u"command -v ab"
-        message = u"ab not installed on TG node!"
+        cmd = "command -v ab"
+        message = "ab not installed on TG node!"
         exec_cmd_no_error(tg_node, cmd, message=message)
 
     @staticmethod
@@ -99,13 +102,13 @@ class ABTools:
         :rtype: str
         """
         command = f"ab -V | head -1 | cut -d',' -f2"
-        message = u"Get AB version failed!"
+        message = "Get AB version failed!"
         stdout, _ = exec_cmd_no_error(node, command, message=message)
         return stdout.strip()
 
     @staticmethod
     def run_ab(tg_node, ip_addr, tg_addr, tls_tcp, cipher, files_num, rps_cps,
-               r_total, c_total, port, protocol=u"TLS1.3"):
+               r_total, c_total, port, protocol="TLS1.3"):
         """ Run ab test.
 
         :param tg_node: Topology node.
@@ -136,7 +139,7 @@ class ABTools:
         :raises: RuntimeError if node type is not a TG.
         """
         if files_num == 0:
-            files = u"return"
+            files = "return"
         elif files_num >= 1024:
             files = f"{int(files_num / 1024)}KB.json"
         else:
@@ -153,66 +156,32 @@ class ABTools:
             port=port,
             mode=rps_cps,
         )
-        stdout, _ = exec_cmd_no_error(tg_node, cmd, timeout=180, sudo=True,
-                                      message=u"ab runtime error!")
-        log_msg = ABTools._parse_ab_output(stdout, rps_cps, tls_tcp)
-
-        logger.info(log_msg)
-
-        return log_msg
-
-    @staticmethod
-    def _parse_ab_output(msg, rps_cps, tls_tcp):
-        """Parse the ab stdout with the results.
-
-        :param msg: Ab Stdout.
-        :param rps_cps: RPS or CPS.
-        :param tls_tcp: TLS or TCP.
-        :type msg: str
-        :type rps_cps: str
-        :type tls_tcp: str
-        :return: Message with measured data.
-        :rtype: str
-        """
-
-        msg_lst = msg.splitlines(keepends=False)
-
-        total_cps = u""
-        latency = u""
-        processing = u""
-        complete_req = u""
-        failed_req = u""
-        total_bytes = u""
-        rate = u""
-
-        if tls_tcp == u"tls":
-            log_msg = u"\nMeasured HTTPS values:\n"
-        else:
-            log_msg = u"\nMeasured HTTP values:\n"
+        stdout, _ = exec_cmd_no_error(
+            tg_node, cmd, timeout=180, sudo=True, message="ab runtime error!"
+        )
 
-        for line in msg_lst:
+        rate_unit = rps_cps
+        rate = None
+        bandwidth = None
+        latency = None
+        completed_requests = None
+        failed_requests = None
+        for line in stdout.splitlines():
             if f"Connection {rps_cps} rate:" in line:
-                # rps (cps)
-                total_cps = line + u"\n"
-            elif u"Transfer Rate:" in line:
-                # Rate
-                rate = line + u"\n"
-            elif u"Latency:" in line:
-                # Latency
-                latency = line + u"\n"
-            elif u"Total data transferred" in line:
-                total_bytes = line + u"\n"
-            elif u"Completed requests" in line:
-                complete_req = line + u"\n"
-            elif u"Failed requests" in line:
-                failed_req = line + u"\n"
-
-        log_msg += rate
-        log_msg += latency
-        log_msg += processing
-        log_msg += complete_req
-        log_msg += failed_req
-        log_msg += total_bytes
-        log_msg += total_cps
-
-        return log_msg
+                rate = float(search(r":\s*(\d+\.?\d+)", line).group(1))
+            elif "Transfer Rate:" in line:
+                bandwidth = \
+                    float(search(r":\s*(\d+\.?\d+)", line).group(1)) * 8000
+            elif "Latency:" in line:
+                latency = float(search(r":\s*(\d+\.?\d+)", line).group(1))
+            elif "Completed requests:" in line:
+                completed_requests = int(search(r":\s*(\d+)", line).group(1))
+            elif "Failed requests" in line:
+                failed_requests = int(search(r":\s*(\d+)", line).group(1))
+
+        export_hoststack_results(
+            bandwidth, rate, rate_unit, latency, failed_requests,
+            completed_requests
+        )
+
+        return stdout