Add virtual enviroment support for cli
[bottlenecks.git] / testsuites / posca / testcase_script / posca_factor_system_bandwidth.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", "--conf",
23                     help="configuration files for the testcase,\
24                         in yaml format",
25                     default="/home/opnfv/bottlenecks/testsuites/posca/\
26                         testcase_cfg/posca_factor_tx_pkt_size.yaml")
27 args = parser.parse_args()
28
29 cmd = "curl -i"
30 order_arg = "-H \"Content-Type: application/json\" -X POST -d \'{\"cmd\": \
31             \"start\", \"opts\":{\"output-file\": \"/tem/yardstick.out\"}, \
32             \"args\": \"../samples/netperf.yaml\"}'"
33
34 # --------------------------------------------------
35 # logging configuration
36 # --------------------------------------------------
37 logger = logging.getLogger(__name__)
38
39
40 def posca_env_check():
41     print("========== posca system bandwidth env check ===========")
42     filepath = r"/home/opnfv/bottlenecks/testsuites/posca/test_result/"
43     if os.path.exists(filepath):
44         return True
45     else:
46         os.mkdirs(r'/home/opnfv/bottlenecks/testsuites/posca/test_result/')
47
48
49 def posca_output_result(time_new, input_1, input_2, input_3,
50                         input_4, input_5, input_6):
51     save_dic = {}
52     save_dic['tx_pkt_size'] = input_1
53     save_dic['rx_cache_size'] = input_2
54     save_dic['tx_cache_size'] = input_3
55     save_dic['throughput '] = input_4
56     save_dic['latency'] = input_5
57     save_dic['cpu_load'] = input_6
58     with open("/home/opnfv/bottlenecks/testsuites/posca/test_result/\
59             factor_tx_cache_size_%s.json" % (time_new), "a") as f:
60         f.write(json.dumps(save_dic, f))
61         f.write("\n")
62
63
64 def posca_config_read(config_str):
65     print("========== posca system bandwidth config read ===========")
66     con_dic = {}
67     config = ConfigParser.ConfigParser()
68     with open(config_str, "rd") as cfgfile:
69         config.readfp(cfgfile)
70         con_dic['test_ip'] = config.get("config", "test_ip")
71         con_dic['test_tool'] = config.get("config", "tool")
72         con_dic['test_time'] = config.get("config", "test_time")
73         con_dic['test_protocol'] = config.get("config", "protocol")
74         con_dic['test_tx_pkt_s'] = config.get("config", "tx pkt sizes")
75         con_dic['test_rx_pkt_s'] = config.get("config", "rx pkt sizes")
76         con_dic['test_tx_cache_s'] = config.get("config", "tx cache sizes")
77         con_dic['test_rx_cache_s'] = config.get("config", "rx cache sizes")
78         con_dic['test_cpu_load'] = config.get("config", "cpu load")
79         con_dic['test_latency'] = config.get("config", "latency")
80
81     return con_dic
82
83
84 def posca_run(con_dic):
85     print("========== run posca system bandwidth ===========")
86
87     test_rx_pkt_s_e = 87380
88     test_tx_pkt_s_a = con_dic['test_tx_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_tx_pkt_s_e in test_tx_pkt_s_a:
97                 print("%s,%s,%s") % (test_tx_pkt_s_e, test_rx_cache_s_e,
98                                      test_tx_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/testre\
107                     sults?task_id=%s" % (cmd, con_dic['test_ip'], task_id))
108                 bandwidth = cmd_excute.find("bandwidth")
109                 cpu_load = cmd_excute.find("cpu_load")
110                 latency = cmd_excute.find("latency")
111                 posca_output_result(time_new, test_rx_pkt_s_e,
112                                     test_rx_cache_s_e,
113                                     bandwidth, latency, cpu_load)
114                 if (cpu_load < con_dic['test_cpu_load\
115                         ']) and (latency < con_dic['test_latency']):
116                     if (abs(bandwidth_tmp-bandwidth)/bandwidth < 0.05):
117                         print("%s,%s,%s,%s,%s,%s") % (test_rx_pkt_s_e,
118                                                       test_rx_cache_s_e,
119                                                       test_tx_cache_s_e,
120                                                       bandwidth,
121                                                       latency,
122                                                       cpu_load)
123                         return True
124                     else:
125                         bandwidth_tmp = bandwidth
126                 else:
127                     print("%s,%s,%s,%s,%s,%s") % (test_rx_pkt_s_e,
128                                                   test_rx_cache_s_e,
129                                                   test_tx_cache_s_e,
130                                                   bandwidth,
131                                                   latency,
132                                                   cpu_load)
133                     return True
134
135
136 def main():
137     if not (args.conf):
138         logger.error("Configuration files do not exist for \
139                     the specified testcases")
140         os.exit(-1)
141     else:
142         testcase_cfg = args.conf
143
144     con_dic = posca_config_read(testcase_cfg)
145     posca_env_check()
146     posca_run(con_dic)
147
148     time.sleep(5)
149
150 if __name__ == '__main__':
151     main()