1 # Copyright (c) 2023 Intel and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """ab implementation into CSIT framework."""
17 from resources.libraries.python.Constants import Constants
18 from resources.libraries.python.model.ExportResult import (
19 export_hoststack_results
21 from resources.libraries.python.OptionString import OptionString
22 from resources.libraries.python.ssh import exec_cmd_no_error
23 from resources.libraries.python.topology import NodeType
27 """This class implements:
33 def get_cmd_options(**kwargs):
34 """Create parameters options.
36 :param kwargs: Dict of cmd parameters.
38 :returns: Cmd parameters.
43 dirname = f"{Constants.REMOTE_FW_DIR}/resources/tools/ab"
44 cmd.add(f"{dirname}/ABFork.py")
45 cmd_options = OptionString(prefix="-")
46 # Number of requests to perform.
47 cmd_options.add_with_value_from_dict("r", "requests", kwargs)
48 # Server port number to use.
49 cmd_options.add_with_value_from_dict("p", "port", kwargs)
50 # Number of clients being processed at the same time.
51 cmd_options.add_with_value_from_dict("c", "clients", kwargs)
52 # Filename to be requested from the servers.
53 cmd_options.add_with_value_from_dict("f", "files", kwargs)
55 cmd_options.add_with_value_from_dict("i", "ip", kwargs)
57 cmd_options.add_with_value_from_dict("g", "tip", kwargs)
58 # Specify SSL/TLS cipher suite.
59 cmd_options.add_with_value_from_dict("z", "cipher", kwargs, default=0)
60 # Specify SSL/TLS protocol.
61 cmd_options.add_with_value_from_dict("t", "protocol", kwargs,
64 cmd_options.add_with_value_from_dict("m", "mode", kwargs)
65 return cmd.extend(cmd_options)
68 def check_ab(tg_node):
69 """Check if ab is installed on the TG node.
71 :param tg_node: Topology node.
73 :raises: RuntimeError if the given node is not a TG node or if the
74 command is not available.
77 if tg_node["type"] != NodeType.TG:
78 raise RuntimeError("Node type is not a TG!")
81 message = "ab not installed on TG node!"
82 exec_cmd_no_error(tg_node, cmd, message=message)
85 def get_ab_type(node):
86 """Log and return the installed traffic generator type.
88 :param node: Node from topology file.
90 :returns: Traffic generator type string.
96 def get_ab_version(node):
97 """Log and return the installed traffic generator version.
99 :param node: Node from topology file.
101 :returns: Traffic generator version string.
104 command = f"ab -V | head -1 | cut -d',' -f2"
105 message = "Get AB version failed!"
106 stdout, _ = exec_cmd_no_error(node, command, message=message)
107 return stdout.strip()
110 def run_ab(tg_node, ip_addr, tg_addr, tls_tcp, cipher, files_num, rps_cps,
111 r_total, c_total, port, protocol="TLS1.3"):
114 :param tg_node: Topology node.
115 :param ip_addr: Sut ip address.
116 :param tg_addr: Tg ip address.
117 :param tls_tcp: TLS or TCP.
118 :param cipher: Specify SSL/TLS cipher suite.
119 :param files_num: Filename to be requested from the servers.
120 The file is named after the file size.
121 :param rps_cps: RPS or CPS.
122 :param r_total: Requests total.
123 :param r_total: Clients total.
124 :param port: Server listen port.
125 :param protocol: TLS Protocol.
137 :returns: Message with measured data.
139 :raises: RuntimeError if node type is not a TG.
143 elif files_num >= 1024:
144 files = f"{int(files_num / 1024)}KB.json"
146 files = f"{files_num}B.json"
148 cmd = ABTools.get_cmd_options(
159 stdout, _ = exec_cmd_no_error(
160 tg_node, cmd, timeout=180, sudo=True, message="ab runtime error!"
167 completed_requests = None
168 failed_requests = None
169 for line in stdout.splitlines():
170 if f"Connection {rps_cps} rate:" in line:
171 rate = float(search(r":\s*(\d+\.?\d+)", line).group(1))
172 elif "Transfer Rate:" in line:
174 float(search(r":\s*(\d+\.?\d+)", line).group(1)) * 8000
175 elif "Latency:" in line:
176 latency = float(search(r":\s*(\d+\.?\d+)", line).group(1))
177 elif "Completed requests:" in line:
178 completed_requests = int(search(r":\s*(\d+)", line).group(1))
179 elif "Failed requests" in line:
180 failed_requests = int(search(r":\s*(\d+)", line).group(1))
182 export_hoststack_results(
183 bandwidth, rate, rate_unit, latency, failed_requests,