1 # Copyright (c) 2016-2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from __future__ import absolute_import
16 from __future__ import print_function
22 # ixload uses its own py2. So importing jsonutils fails. So adding below
23 # workaround to support call from yardstick
25 from oslo_serialization import jsonutils
27 import json as jsonutils
29 from yardstick.common.utils import join_non_strings
30 from yardstick.common.utils import ErrorClass
33 from IxLoad import IxLoad, StatCollectorUtils
36 StatCollectorUtils = ErrorClass
38 LOG = logging.getLogger(__name__)
39 CSV_FILEPATH_NAME = 'IxL_statResults.csv'
44 'L2-3 Stats for Client Ports.csv',
45 'L2-3 Stats for Server Ports.csv',
46 'IxLoad Detailed Report.html',
47 'IxLoad Detailed Report.pdf'
51 ["HTTP Client", "TCP Connections Established", "kSum"],
52 ["HTTP Client", "TCP Connection Requests Failed", "kSum"],
53 ["HTTP Client", "HTTP Simulated Users", "kSum"],
54 ["HTTP Client", "HTTP Concurrent Connections", "kSum"],
55 ["HTTP Client", "HTTP Connections", "kSum"],
56 ["HTTP Client", "HTTP Transactions", "kSum"],
57 ["HTTP Client", "HTTP Connection Attempts", "kSum"]
61 ["HTTP Server", "TCP Connections Established", "kSum"],
62 ["HTTP Server", "TCP Connection Requests Failed", "kSum"]
66 INCOMING_STAT_RECORD_TEMPLATE = """
67 =====================================
68 INCOMING STAT RECORD >>> %s
72 =====================================
75 INCOMING_STAT_INTERVAL_TEMPLATE = """
76 =====================================
77 Incoming stats: Time interval: %s
78 Incoming stats: Time interval: %s
79 =====================================
83 class IXLOADHttpTest(object):
85 def __init__(self, test_input):
86 self.test_input = jsonutils.loads(test_input)
89 self.stat_utils = None
90 self.remote_server = None
91 self.config_file = None
92 self.results_on_windows = None
93 self.result_dir = None
96 self.ports_to_reassign = None
99 def format_ports_for_reassignment(ports):
100 formatted = [join_non_strings(';', p) for p in ports if len(p) == 3]
101 LOG.debug('for client ports:%s', os.linesep.join(formatted))
104 def reassign_ports(self, test, repository, ports_to_reassign):
105 LOG.debug('ReassignPorts: %s %s', test, repository)
107 chassis_chain = repository.cget('chassisChain')
108 LOG.debug('chassischain: %s', chassis_chain)
109 client_ports = ports_to_reassign[0::2]
110 server_ports = ports_to_reassign[1::2]
112 client_ports = self.format_ports_for_reassignment(client_ports)
113 LOG.debug('Reassigning client ports: %s', client_ports)
114 server_ports = self.format_ports_for_reassignment(server_ports)
115 LOG.debug('Reassigning server ports: %s', server_ports)
116 ports_to_set = client_ports + server_ports
119 LOG.debug('Reassigning ports: %s', ports_to_set)
120 test.setPorts(ports_to_set)
122 LOG.error('Error: Could not remap port assignment for: %s',
124 self.ix_load.delete(repository)
125 self.ix_load.disconnect()
129 def stat_collector(*args):
130 LOG.debug(INCOMING_STAT_RECORD_TEMPLATE, args, len(args), args[0], args[1])
133 def IxL_StatCollectorCommand(*args):
135 timestamp = args[1][1]
136 LOG.debug(INCOMING_STAT_INTERVAL_TEMPLATE, timestamp, stats)
139 def set_results_dir(test_controller, results_on_windows):
141 If the folder doesn't exists on the Windows Client PC,
142 IxLoad will automatically create it.
145 test_controller.setResultDir(results_on_windows)
147 LOG.error('Error creating results dir on Win: %s',
151 def load_config_file(self, config_file):
153 LOG.debug(config_file)
154 repository = self.ix_load.new("ixRepository", name=config_file)
157 LOG.error('Error: IxLoad config file not found: %s', config_file)
160 def start_http_test(self):
161 self.ix_load = IxLoad()
163 LOG.debug('--- ixLoad obj: %s', self.ix_load)
165 self.ix_load.connect(self.remote_server)
169 log_tag = "IxLoad-api"
171 logger = self.ix_load.new("ixLogger", log_tag, 1)
172 log_engine = logger.getEngine()
173 log_engine.setLevels(self.ix_load.ixLogger.kLevelDebug,
174 self.ix_load.ixLogger.kLevelInfo)
175 log_engine.setFile(log_name, 2, 256, 1)
177 # Initialize stat collection utilities
178 self.stat_utils = StatCollectorUtils()
180 test_controller = self.ix_load.new("ixTestController", outputDir=1)
182 repository = self.load_config_file(self.config_file)
184 # Get the first test on the testList
185 test_name = repository.testList[0].cget("name")
186 test = repository.testList.getItem(test_name)
188 self.set_results_dir(test_controller, self.results_on_windows)
190 test.config(statsRequired=1, enableResetPorts=1, csvInterval=2,
191 enableForceOwnership=True)
193 # ---- Remap ports ----
195 self.reassign_ports(test, repository, self.ports_to_reassign)
197 LOG.exception("Exception occurred during reassign_ports")
199 # -----------------------------------------------------------------------
200 # Set up stat Collection
201 # -----------------------------------------------------------------------
202 test_server_handle = test_controller.getTestServerHandle()
203 self.stat_utils.Initialize(test_server_handle)
205 # Clear any stats that may have been registered previously
206 self.stat_utils.ClearStats()
208 # Define the stats we would like to collect
209 self.stat_utils.AddStat(caption="Watch_Stat_1",
210 statSourceType="HTTP Client",
211 statName="TCP Connections Established",
212 aggregationType="kSum",
215 self.stat_utils.AddStat(caption="Watch_Stat_2",
216 statSourceType="HTTP Client",
217 statName="TCP Connection Requests Failed",
218 aggregationType="kSum",
221 self.stat_utils.AddStat(caption="Watch_Stat_3",
222 statSourceType="HTTP Server",
223 statName="TCP Connections Established",
224 aggregationType="kSum",
227 self.stat_utils.AddStat(caption="Watch_Stat_4",
228 statSourceType="HTTP Server",
229 statName="TCP Connection Requests Failed",
230 aggregationType="kSum",
233 self.stat_utils.StartCollector(self.IxL_StatCollectorCommand)
235 test_controller.run(test)
236 self.ix_load.waitForTestFinish()
238 test_controller.releaseConfigWaitFinish()
240 # Stop the collector (running in the tcl event loop)
241 self.stat_utils.StopCollector()
244 test_controller.generateReport(detailedReport=1, format="PDF;HTML")
245 test_controller.releaseConfigWaitFinish()
247 self.ix_load.delete(test)
248 self.ix_load.delete(test_controller)
249 self.ix_load.delete(logger)
250 self.ix_load.delete(log_engine)
252 LOG.debug('Retrieving CSV stats from Windows Client PC ...')
253 for stat_file in STATS_TO_GET:
254 enhanced_stat_file = stat_file.replace('-', '')
255 enhanced_stat_file = enhanced_stat_file.replace(' ', '_')
256 enhanced_stat_file = enhanced_stat_file.replace('__', '_')
258 LOG.debug('Getting csv stat file: %s', stat_file)
259 src_file = os.path.join(self.results_on_windows, stat_file)
260 dst_file = os.path.join(self.result_dir, '_'.join(['ixLoad', enhanced_stat_file]))
261 self.ix_load.retrieveFileCopy(src_file, dst_file)
263 self.ix_load.disconnect()
265 def parse_run_test(self):
266 self.remote_server = self.test_input["remote_server"]
267 LOG.debug("remote tcl server: %s", self.remote_server)
269 self.config_file = self.test_input["ixload_cfg"]
270 LOG.debug("ixload config: %s", self.remote_server)
272 self.results_on_windows = 'C:/Results'
273 self.result_dir = self.test_input["result_dir"]
274 self.chassis = self.test_input["ixia_chassis"]
275 LOG.debug("remote ixia chassis: %s", self.chassis)
277 self.card = self.test_input["IXIA"]["card"]
278 self.ports_to_reassign = [
279 [self.chassis, self.card, port] for port in
280 self.test_input["IXIA"]["ports"]
283 LOG.debug("Ports to be reassigned: %s", self.ports_to_reassign)
287 # Get the args from cmdline and parse and run the test
288 test_input = "".join(args[1:])
290 ixload_obj = IXLOADHttpTest(test_input)
291 ixload_obj.start_http_test()
293 if __name__ == '__main__':