Change OS exporter
[bottlenecks.git] / testsuites / posca / testcase_script / posca_factor_tx_pkt_size.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 import os
12 import argparse
13 import time
14 import logging
15 import ConfigParser
16 import json
17
18 # ------------------------------------------------------
19 # parser for configuration files in each test case
20 # ------------------------------------------------------
21 parser = argparse.ArgumentParser()
22 parser.add_argument("-c",
23                     "--conf",
24                     help="configuration files for the testcase,\
25                         in yaml format",
26                     default="/home/opnfv/bottlenecks/testsuites/posca/\
27                         testcase_cfg/posca_factor_tx_pkt_size.yaml")
28 args = parser.parse_args()
29
30 cmd = "curl -i"
31 order_arg = "-H \"Content-Type: application/json\" -X POST -d \'{\"cmd\": \
32             \"start\", \"opts\":{\"output-file\": \"/tem/yardstick.out\"}, \
33             \"args\": \"../samples/netperf.yaml\"}'"
34
35 # --------------------------------------------------
36 # logging configuration
37 # --------------------------------------------------
38 logger = logging.getLogger(__name__)
39
40
41 def posca_env_check():
42     print("========== posca system bandwidth env check ===========")
43     filepath = r"/home/opnfv/bottlenecks/testsuites/posca/test_result/"
44     if os.path.exists(filepath):
45         return True
46     else:
47         os.mkdirs(r'/home/opnfv/bottlenecks/testsuites/posca/test_result/')
48
49
50 def posca_output_result(time_new, input_1, input_2, input_3,
51                         input_4, input_5, input_6):
52     save_dic = {}
53     save_dic['rx_pkt_size'] = input_1
54     save_dic['tx_cache_size'] = input_2
55     save_dic['tx_cache_size'] = input_3
56     save_dic['throughput'] = input_4
57     save_dic['latency'] = input_5
58     save_dic['cpu_load'] = input_6
59     with open("/home/opnfv/bottlenecks/testsuites/posca/\
60             test_result/factor_tx_pkt_size_%s.json" % (time_new), "a") as f:
61         f.write(json.dumps(save_dic, f))
62         f.write("\n")
63
64
65 def posca_config_read(config_str):
66     print("========== posca system bandwidth config read ===========")
67
68     con_dic = {}
69     config = ConfigParser.ConfigParser()
70     with open(config_str, "rd") as cfgfile:
71         config.readfp(cfgfile)
72         con_dic['test_ip'] = config.get("config", "test_ip")
73         con_dic['test_tool'] = config.get("config", "tool")
74         con_dic['test_time'] = config.get("config", "test_time")
75         con_dic['test_protocol'] = config.get("config", "protocol")
76         con_dic['test_tx_pkt_s'] = config.get("config", "tx pkt sizes")
77         con_dic['test_rx_pkt_s'] = config.get("config", "rx pkt sizes")
78         con_dic['test_tx_cache_s'] = config.get("config", "tx cache sizes")
79         con_dic['test_rx_cache_s'] = config.get("config", "rx cache sizes")
80         con_dic['test_cpu_load'] = config.get("config", "cpu load")
81         con_dic['test_latency'] = config.get("config", "latency")
82
83     return con_dic
84
85
86 def posca_run(con_dic):
87     print("========== run posca system bandwidth ===========")
88     test_rx_pkt_s_a = con_dic['test_rx_pkt_s'].split(',')
89     test_tx_cache_s_a = con_dic['test_tx_cache_s'].split(',')
90     test_rx_cache_s_a = con_dic['test_rx_cache_s'].split(',')
91     time_new = time.strftime('%H_%M', time.localtime(time.time()))
92     bandwidth_tmp = 1
93
94     for test_rx_cache_s_e in test_rx_cache_s_a:
95         for test_tx_cache_s_e in test_tx_cache_s_a:
96             for test_rx_pkt_s_e in test_rx_pkt_s_a:
97                 print("%s,%s,%s") % (test_rx_pkt_s_e, test_tx_cache_s_e,
98                                      test_rx_cache_s_e)
99                 order_excute = os.popen("%s %s http://%s/api/v3/yardstick/\
100                     tasks/task %s %s %s" % (cmd, order_arg, con_dic['test_ip'],
101                                             test_rx_pkt_s_e, test_rx_cache_s_e,
102                                             test_tx_cache_s_e))
103                 order_result = order_excute.read()
104                 task_id = order_result.find("task_id")
105                 time.sleep(con_dic['test_time'])
106                 cmd_excute = os.popen("%s http://%s/api/v3/yardstick/test\
107                     results?task_id=%s" % (cmd, con_dic['test_ip'], task_id))
108                 test_result = cmd_excute.read()
109                 bandwidth = test_result.find("bandwidth")
110                 cpu_load = test_result.find("cpu_load")
111                 latency = test_result.find("latency")
112                 posca_output_result(time_new, test_rx_pkt_s_e,
113                                     test_rx_cache_s_e, test_tx_cache_s_e,
114                                     bandwidth, latency, cpu_load)
115                 if (abs(bandwidth - con_dic['test_throughput']) / con_dic['test_\
116                         throughput'] > 0.05) and (latency < con_dic['test_\
117                         latency']) and (cpu_load < con_dic['test_cpu_load']):
118                     if (abs(bandwidth_tmp - bandwidth) / bandwidth < 0.05):
119                         print("%s,%s,%s,%s,%s,%s") % (test_rx_pkt_s_e,
120                                                       test_rx_cache_s_e,
121                                                       test_tx_cache_s_e,
122                                                       bandwidth,
123                                                       latency,
124                                                       cpu_load)
125                         return True
126                     else:
127                         bandwidth_tmp = bandwidth
128                 else:
129                     print("%s,%s,%s,%s,%s,%s") % (test_rx_pkt_s_e,
130                                                   test_rx_cache_s_e,
131                                                   test_tx_cache_s_e,
132                                                   bandwidth,
133                                                   latency,
134                                                   cpu_load)
135                     return False
136
137
138 def main():
139     if not (args.conf):
140         logger.error("Configuration files do not exist \
141                     for the specified testcases")
142         os.exit(-1)
143     else:
144         testcase_cfg = args.conf
145
146     con_dic = posca_config_read(testcase_cfg)
147     posca_env_check()
148     posca_run(con_dic)
149
150     time.sleep(5)
151
152 if __name__ == '__main__':
153     main()