1 # Copyright (c) 2021 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."""
16 from robot.api import logger
17 from resources.libraries.python.topology import NodeType
18 from resources.libraries.python.Constants import Constants
19 from resources.libraries.python.ssh import exec_cmd_no_error
20 from resources.libraries.python.OptionString import OptionString
24 """This class implements:
30 def get_cmd_options(**kwargs):
31 """Create parameters options.
33 :param kwargs: Dict of cmd parameters.
35 :returns: Cmd parameters.
40 dirname = f"{Constants.REMOTE_FW_DIR}/resources/tools/ab"
41 cmd.add(f"{dirname}/ABFork.py")
42 cmd_options = OptionString(prefix=u"-")
43 # Number of requests to perform.
44 cmd_options.add_with_value_from_dict(u"r", u"requests", kwargs)
45 # Server port number to use.
46 cmd_options.add_with_value_from_dict(u"p", u"port", kwargs)
47 # Number of clients being processed at the same time.
48 cmd_options.add_with_value_from_dict(u"c", u"clients", kwargs)
49 # Filename to be requested from the servers.
50 cmd_options.add_with_value_from_dict(u"f", u"files", kwargs)
52 cmd_options.add_with_value_from_dict(u"i", u"ip", kwargs)
54 cmd_options.add_with_value_from_dict(u"g", u"tip", kwargs)
55 # Specify SSL/TLS cipher suite.
56 cmd_options.add_with_value_from_dict(u"z", u"cipher", kwargs, default=0)
57 # Specify SSL/TLS protocol.
58 cmd_options.add_with_value_from_dict(u"t", u"protocol", kwargs,
61 cmd_options.add_with_value_from_dict(u"m", u"mode", kwargs)
62 return cmd.extend(cmd_options)
65 def check_ab(tg_node):
66 """Check if ab is installed on the TG node.
68 :param tg_node: Topology node.
70 :raises: RuntimeError if the given node is not a TG node or if the
71 command is not available.
74 if tg_node[u"type"] != NodeType.TG:
75 raise RuntimeError(u"Node type is not a TG!")
77 cmd = u"command -v ab"
78 message = u"ab not installed on TG node!"
79 exec_cmd_no_error(tg_node, cmd, message=message)
82 def run_ab(tg_node, ip_addr, tg_addr, tls_tcp, cipher, files_num, rps_cps,
83 r_total, c_total, port, protocol=u"TLS1.3"):
86 :param tg_node: Topology node.
87 :param ip_addr: Sut ip address.
88 :param tg_addr: Tg ip address.
89 :param tls_tcp: TLS or TCP.
90 :param cipher: Specify SSL/TLS cipher suite.
91 :param files_num: Filename to be requested from the servers.
92 The file is named after the file size.
93 :param rps_cps: RPS or CPS.
94 :param r_total: Requests total.
95 :param r_total: Clients total.
96 :param port: Server listen port.
97 :param protocol: TLS Protocol.
109 :returns: Message with measured data.
111 :raises: RuntimeError if node type is not a TG.
115 elif files_num >= 1024:
116 files = f"{int(files_num / 1024)}KB.json"
118 files = f"{files_num}B.json"
120 cmd = ABTools.get_cmd_options(
131 stdout, _ = exec_cmd_no_error(tg_node, cmd, timeout=180, sudo=True,
132 message=u"ab runtime error!")
133 log_msg = ABTools._parse_ab_output(stdout, rps_cps, tls_tcp)
140 def _parse_ab_output(msg, rps_cps, tls_tcp):
141 """Parse the ab stdout with the results.
143 :param msg: Ab Stdout.
144 :param rps_cps: RPS or CPS.
145 :param tls_tcp: TLS or TCP.
149 :return: Message with measured data.
153 msg_lst = msg.splitlines(keepends=False)
163 if tls_tcp == u"tls":
164 log_msg = u"\nMeasured HTTPS values:\n"
166 log_msg = u"\nMeasured HTTP values:\n"
169 if f"Connection {rps_cps} rate:" in line:
171 total_cps = line + u"\n"
172 elif u"Transfer Rate:" in line:
175 elif u"Latency:" in line:
177 latency = line + u"\n"
178 elif u"Total data transferred" in line:
179 total_bytes = line + u"\n"
180 elif u"Completed requests" in line:
181 complete_req = line + u"\n"
182 elif u"Failed requests" in line:
183 failed_req = line + u"\n"
187 log_msg += processing
188 log_msg += complete_req
189 log_msg += failed_req
190 log_msg += total_bytes