VAT-to-PAPI: Fix HTTP/TCP tests
[csit.git] / resources / tools / wrk / wrk.py
index 33cfd08..84d17ee 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2018 Cisco and/or its affiliates.
+# Copyright (c) 2019 Cisco 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:
 
 import re
 
+from copy import deepcopy
+from time import sleep
+
 from robot.api import logger
 
 from resources.libraries.python.ssh import SSH
 from resources.libraries.python.topology import NodeType
 from resources.libraries.python.CpuUtils import CpuUtils
-from resources.libraries.python.constants import Constants
+from resources.libraries.python.Constants import Constants
 
 from resources.tools.wrk.wrk_traffic_profile_parser import WrkTrafficProfile
 from resources.tools.wrk.wrk_errors import WrkError
@@ -54,13 +57,13 @@ REGEX_LATENCY_DIST = \
 REGEX_NUM = r"(\d*\.*\d*)(\D*)"
 
 
-def install_wrk(tg_node):
-    """Install wrk on the TG node.
+def check_wrk(tg_node):
+    """Check if wrk is installed on the TG node.
 
     :param tg_node: Traffic generator node.
     :type tg_node: dict
     :raises: RuntimeError if the given node is not a TG node or if the
-    installation fails.
+        command is not availble.
     """
 
     if tg_node['type'] != NodeType.TG:
@@ -71,46 +74,25 @@ def install_wrk(tg_node):
 
     ret, _, _ = ssh.exec_command(
         "sudo -E "
-        "sh -c '{0}/resources/tools/wrk/wrk_utils.sh install false'".
-        format(Constants.REMOTE_FW_DIR), timeout=1800)
+        "sh -c '{0}/resources/tools/wrk/wrk_utils.sh installed'".
+        format(Constants.REMOTE_FW_DIR))
     if int(ret) != 0:
-        raise RuntimeError('Installation of wrk on TG node failed.')
-
-
-def destroy_wrk(tg_node):
-    """Destroy wrk on the TG node.
-
-    :param tg_node: Traffic generator node.
-    :type tg_node: dict
-    :raises: RuntimeError if the given node is not a TG node or the removal of
-    wrk failed.
-    """
+        raise RuntimeError('WRK is not installed on TG node.')
 
-    if tg_node['type'] != NodeType.TG:
-        raise RuntimeError('Node type is not a TG.')
 
-    ssh = SSH()
-    ssh.connect(tg_node)
-
-    ret, _, _ = ssh.exec_command(
-        "sudo -E "
-        "sh -c '{0}/resources/tools/wrk/wrk_utils.sh destroy'".
-        format(Constants.REMOTE_FW_DIR), timeout=1800)
-    if int(ret) != 0:
-        raise RuntimeError('Removal of wrk from the TG node failed.')
-
-
-def run_wrk(tg_node, profile_name, tg_numa, test_type):
+def run_wrk(tg_node, profile_name, tg_numa, test_type, warm_up=False):
     """Send the traffic as defined in the profile.
 
     :param tg_node: Traffic generator node.
     :param profile_name: The name of wrk traffic profile.
     :param tg_numa: Numa node on which wrk will run.
     :param test_type: The type of the tests: cps, rps, bw
+    :param warm_up: If True, warm-up traffic is generated before test traffic.
     :type profile_name: str
     :type tg_node: dict
     :type tg_numa: int
     :type test_type: str
+    :type warm_up: bool
     :returns: Message with measured data.
     :rtype: str
     :raises: RuntimeError if node type is not a TG.
@@ -140,6 +122,9 @@ def run_wrk(tg_node, profile_name, tg_numa, test_type):
             str(profile["latency"]),
             "'{0}'".format(" ".join(profile["urls"]))
         ]
+        if warm_up:
+            warm_up_params = deepcopy(params)
+            warm_up_params[4] = "10s"
     elif len(profile["urls"]) == profile["cpus"]:
         params = [
             "traffic_n_urls_n_cores",
@@ -153,6 +138,9 @@ def run_wrk(tg_node, profile_name, tg_numa, test_type):
             str(profile["latency"]),
             "'{0}'".format(" ".join(profile["urls"]))
         ]
+        if warm_up:
+            warm_up_params = deepcopy(params)
+            warm_up_params[4] = "10s"
     else:
         params = [
             "traffic_n_urls_m_cores",
@@ -167,11 +155,24 @@ def run_wrk(tg_node, profile_name, tg_numa, test_type):
             str(profile["latency"]),
             "'{0}'".format(" ".join(profile["urls"]))
         ]
+        if warm_up:
+            warm_up_params = deepcopy(params)
+            warm_up_params[5] = "10s"
+
     args = " ".join(params)
 
     ssh = SSH()
     ssh.connect(tg_node)
 
+    if warm_up:
+        warm_up_args = " ".join(warm_up_params)
+        ret, _, _ = ssh.exec_command(
+            "{0}/resources/tools/wrk/wrk_utils.sh {1}".
+            format(Constants.REMOTE_FW_DIR, warm_up_args), timeout=1800)
+        if int(ret) != 0:
+            raise RuntimeError('wrk runtime error.')
+        sleep(60)
+
     ret, stdout, _ = ssh.exec_command(
         "{0}/resources/tools/wrk/wrk_utils.sh {1}".
         format(Constants.REMOTE_FW_DIR, args), timeout=1800)
@@ -190,7 +191,7 @@ def run_wrk(tg_node, profile_name, tg_numa, test_type):
         log_msg += "Requests/sec: Avg / Stdev / Max  / +/- Stdev\n"
         for item in stats["rps-stats-lst"]:
             log_msg += "{0} / {1} / {2} / {3}\n".format(*item)
-        log_msg += "Total rps: {0}cps\n".format(stats["rps-sum"])
+        log_msg += "Total rps: {0}rps\n".format(stats["rps-sum"])
     elif test_type == "bw":
         log_msg += "Transfer/sec: {0}Bps".format(stats["bw-sum"])