Modify POSCA code into PEP8 style
[bottlenecks.git] / testsuites / posca / testcase_script / posca_factor_cpu_burden.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 # logging configuration
30 # --------------------------------------------------
31 logger = logging.getLogger(__name__)
32 cmd = "curl -i"
33 order_arg = "-H \"Content-Type: application/json\" -X POST -d \'{\"cmd\": \
34             \"start\", \"opts\":{\"output-file\": \"/tem/yardstick.out\"}, \
35             \"args\": \"../samples/netperf.yaml\"}'"
36
37
38 def posca_env_check():
39     print("========== posca system bandwidth env check ===========")
40     filepath = r"/home/opnfv/bottlenecks/testsuites/posca/test_result/"
41     if os.path.exists(filepath):
42         return True
43     else:
44         os.mkdirs(r'/home/opnfv/bottlenecks/testsuites/posca/test_result/')
45
46
47 def posca_output_result(time_new, input_1, input_2, input_3,
48                         input_4, input_5, input_6):
49     save_dic = {}
50     save_dic['tx_pkt_size'] = input_1
51     save_dic['rx_cache_size'] = input_2
52     save_dic['tx_cache_size'] = input_3
53     save_dic['throughput '] = input_4
54     save_dic['latency'] = input_5
55     save_dic['cpu_load'] = input_6
56     with open("/home/opnfv/bottlenecks/testsuites/posca/test_result/\
57             factor_tx_cache_size_%s.json" % (time_new), "a") as f:
58         f.write(json.dumps(save_dic, f))
59         f.write("\n")
60
61
62 def posca_config_read(config_str):
63     print("========== posca system bandwidth config read ===========")
64
65     con_dic = {}
66     config = ConfigParser.ConfigParser()
67     with open(config_str, "rd") as cfgfile:
68         config.readfp(cfgfile)
69         con_dic['test_ip'] = config.get("config", "test_ip")
70         con_dic['test_throughput'] = config.get("config", "throughput")
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_pkt_s'] = config.get("config", "pkt sizes")
75         con_dic['test_tx_cache_s'] = config.get("config", "tx cache sizes")
76         con_dic['test_rx_cache_s'] = config.get("config", "rx cache sizes")
77         con_dic['test_cpu_load'] = config.get("config", "cpu load")
78         con_dic['test_latency'] = config.get("config", "latency")
79         con_dic['test_rx_flavor'] = config.get("flavor_config", "rx_flavor")
80         con_dic['test_tx_flavor'] = config.get("flavor_config", "tx_flavor")
81     return con_dic
82
83
84 def posca_run(con_dic):
85     print("========== run posca system bandwidth ===========")
86
87     test_pkt_s_a = con_dic['test_pkt_s'].split(',')
88     test_rx_cache_s_e = con_dic['test_rx_cache_s'].split(',')
89     test_tx_cache_s_e = con_dic['test_tx_cache_s'].split(',')
90     time_new = time.strftime('%H_%M', time.localtime(time.time()))
91
92     for test_pkt_s_e in test_pkt_s_a:
93         print("Package size %s") % (test_pkt_s_e)
94         order_excute = os.popen("%s %s http://%s/api/v3/yardstick/\
95                     tasks/task %s %s %s" % (cmd, order_arg, con_dic['test_ip'],
96                                             test_pkt_s_e, test_rx_cache_s_e,
97                                             test_tx_cache_s_e))
98         order_result = order_excute.read()
99         task_id = order_result.find("task_id")
100         time.sleep(con_dic['test_time'])
101         cmd_excute = os.popen("%s http://%s/api/v3/yardstick/testre\
102             sults?task_id=%s" % (cmd, con_dic['test_ip'], task_id))
103         test_result = cmd_excute.read()
104         bandwidth = test_result.find("bandwidth")
105         cpu_load = test_result.find("cpu_load")
106         latency = test_result.find("latency")
107         posca_output_result(time_new, test_pkt_s_e, con_dic['test_rx_cache_s'],
108                             con_dic['test_tx_cache_s'])
109         if (bandwidth < con_dic['test_throughput\
110              ']) and (latency < con_dic['test_latency']):
111             if cpu_load > con_dic['test_cpu_load']:
112                 return True
113             else:
114                 print("%s,%s,%s") % (bandwidth, latency, cpu_load)
115         else:
116             print("%s,%s,%s") % (bandwidth, latency, cpu_load)
117             return False
118
119
120 def main():
121     if not (args.conf):
122         logger.error("Configuration files do not exist for \
123                     the specified testcases")
124         os.exit(-1)
125     else:
126         testcase_cfg = args.conf
127
128     con_dic = posca_config_read(testcase_cfg)
129     posca_env_check()
130     posca_run(con_dic)
131
132     time.sleep(5)
133
134 if __name__ == '__main__':
135     main()