Report: Add vsap
[csit.git] / resources / tools / ab / ABTools.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """ab implementation into CSIT framework."""
15
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
21
22
23 class ABTools:
24     """This class implements:
25     - Get ab command.
26     - Check ab version.
27     """
28
29     @staticmethod
30     def get_cmd_options(**kwargs):
31         """Create  parameters options.
32
33         :param kwargs: Dict of cmd parameters.
34         :type kwargs: dict
35         :returns: Cmd parameters.
36         :rtype: OptionString
37         """
38         cmd = OptionString()
39         cmd.add(u"python3")
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)
51         # Server ip address.
52         cmd_options.add_with_value_from_dict(u"i", u"ip", kwargs)
53         # tg ip address.
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,
59                                              default=0)
60         # Mode: RPS or CPS.
61         cmd_options.add_with_value_from_dict(u"m", u"mode", kwargs)
62         return cmd.extend(cmd_options)
63
64     @staticmethod
65     def check_ab(tg_node):
66         """Check if ab is installed on the TG node.
67
68         :param tg_node: Topology node.
69         :type tg_node: dict
70         :raises: RuntimeError if the given node is not a TG node or if the
71             command is not available.
72         """
73
74         if tg_node[u"type"] != NodeType.TG:
75             raise RuntimeError(u"Node type is not a TG!")
76
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)
80
81     @staticmethod
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"):
84         """ Run ab test.
85
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.
98         :type tg_node: dict
99         :type ip_addr: str
100         :type tg_addr: str
101         :type tls_tcp: str
102         :type cipher: str
103         :type files_num: int
104         :type rps_cps: str
105         :type r_total: int
106         :type c_total: int
107         :type port: int
108         :type protocol: str
109         :returns: Message with measured data.
110         :rtype: str
111         :raises: RuntimeError if node type is not a TG.
112         """
113         if files_num == 0:
114             files = u"return"
115         elif files_num >= 1024:
116             files = f"{int(files_num / 1024)}KB.json"
117         else:
118             files = f"{files_num}B.json"
119
120         cmd = ABTools.get_cmd_options(
121             requests=r_total,
122             clients=c_total,
123             ip=ip_addr,
124             tip=tg_addr,
125             files=files,
126             cipher=cipher,
127             protocol=protocol,
128             port=port,
129             mode=rps_cps,
130         )
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)
134
135         logger.info(log_msg)
136
137         return log_msg
138
139     @staticmethod
140     def _parse_ab_output(msg, rps_cps, tls_tcp):
141         """Parse the ab stdout with the results.
142
143         :param msg: Ab Stdout.
144         :param rps_cps: RPS or CPS.
145         :param tls_tcp: TLS or TCP.
146         :type msg: str
147         :type rps_cps: str
148         :type tls_tcp: str
149         :return: Message with measured data.
150         :rtype: str
151         """
152
153         msg_lst = msg.splitlines(keepends=False)
154
155         total_cps = u""
156         latency = u""
157         processing = u""
158         complete_req = u""
159         failed_req = u""
160         total_bytes = u""
161         rate = u""
162
163         if tls_tcp == u"tls":
164             log_msg = u"\nMeasured HTTPS values:\n"
165         else:
166             log_msg = u"\nMeasured HTTP values:\n"
167
168         for line in msg_lst:
169             if f"Connection {rps_cps} rate:" in line:
170                 # rps (cps)
171                 total_cps = line + u"\n"
172             elif u"Transfer Rate:" in line:
173                 # Rate
174                 rate = line + u"\n"
175             elif u"Latency:" in line:
176                 # Latency
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"
184
185         log_msg += rate
186         log_msg += latency
187         log_msg += processing
188         log_msg += complete_req
189         log_msg += failed_req
190         log_msg += total_bytes
191         log_msg += total_cps
192
193         return log_msg