f6e66f42e406b15e635a8a0a6b031770416f1cb4
[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 common_script
17 import datetime
18 import subprocess
19
20 # ------------------------------------------------------
21 # parser for configuration files in each test case
22 # ------------------------------------------------------
23 parser = argparse.ArgumentParser()
24 parser.add_argument("-c", "--conf",
25                     help="configuration files for the testcase,\
26                         in yaml format",
27                     default="/home/opnfv/bottlenecks/testsuites/posca\
28 /testcase_cfg/posca_factor_system_bandwidth.yaml")
29 args = parser.parse_args()
30 headers = {"Content-Type": "application/json"}
31 INTERPRETER = "/usr/bin/python"
32
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.mkdir(r'/home/opnfv/bottlenecks/testsuites/posca/test_result/')
47
48
49 def system_pkt_bandwidth(test_id, data, file_config, con_dic):
50     date_id = test_id
51     print("package test is is begin from %d" % test_id)
52     cur_role_result = 1
53     pre_role_result = 1
54     pre_reply = {}
55     data_return = {}
56     data_max = {}
57     data_return["throughput"] = 1
58     for test_x in data["tx_pkt_sizes"]:
59         data_max["throughput"] = 1
60         bandwidth_tmp = 1
61         for test_y in data["rx_pkt_sizes"]:
62             test_config = {
63                         "tx_msg_size": float(test_x),
64                         "rx_msg_size": float(test_y),
65                     }
66             date_id = date_id + 1
67             file_config["test_id"] = date_id
68             data_reply = common_script.posca_send_data(
69                    con_dic, test_config, file_config)
70             bandwidth = data_reply["throughput"]
71             if (data_max["throughput"] < bandwidth):
72                 data_max = data_reply
73             if (abs(bandwidth_tmp - bandwidth)/bandwidth_tmp < 0.025):
74                 print(pre_reply)
75                 break
76             else:
77                 pre_reply = data_reply
78                 bandwidth_tmp = bandwidth
79         cur_role_result = pre_reply["throughput"]
80         if (abs(pre_role_result - cur_role_result)/pre_role_result < 0.025):
81             print("date_id is %d,package return at line 111\n" % date_id)
82             # return data_return
83         if data_return["throughput"] < data_max["throughput"]:
84             data_return = data_max
85         pre_role_result = cur_role_result
86     print("date_id is %d,id return success\n" % date_id)
87     return data_return
88
89
90 def posca_run(con_dic):
91     print("========== run posca system bandwidth ===========")
92     test_con_id = 0
93     file_config = {}
94     data = {}
95     rx_pkt_s_a = con_dic['rx_pkt_sizes'].split(',')
96     tx_pkt_s_a = con_dic['tx_pkt_sizes'].split(',')
97     time_new = time.strftime('%H_%M', time.localtime(time.time()))
98     file_config["file_path"] = "/home/opnfv/bottlenecks/testsuites/posca/\
99 test_result/factor_system_system_bandwidth_%s.json" % (time_new)
100     file_config["test_type"] = "system_bandwidth_biggest"
101     data["rx_pkt_sizes"] = rx_pkt_s_a
102     data["tx_pkt_sizes"] = tx_pkt_s_a
103     print("######test package begin######")
104     pkt_reply = system_pkt_bandwidth(
105         test_con_id, data, file_config, con_dic)
106
107     print("######find system bandwidth######")
108     print("rx_msg_size:%d  tx_msg_size:%d\n" %
109           (pkt_reply["rx_msg_size"], pkt_reply["tx_msg_size"]))
110     date_tran = common_script.posca_tran_data(
111                             con_dic['ES_ip'], file_config["file_path"])
112     return True
113
114
115 def main():
116     if not (args.conf):
117         logger.error("Configuration files do not exist for \
118                     the specified testcases")
119         os.exit(-1)
120     else:
121         testcase_cfg = args.conf
122
123     con_str = [
124             'test_ip', 'tool', 'test_time', 'protocol',
125             'tx_pkt_sizes', 'rx_pkt_sizes', 'cpu_load',
126             'latency', 'ES_ip', 'dashboard'
127     ]
128     starttime = datetime.datetime.now()
129     config = ConfigParser.ConfigParser()
130     con_dic = common_script.posca_config_read(testcase_cfg, con_str, config)
131     common_script.posca_create_incluxdb(con_dic)
132     time.sleep(30)
133     posca_env_check()
134     posca_run(con_dic)
135     endtime = datetime.datetime.now()
136     if con_dic["dashboard"] == "y":
137         cmd = '/home/opnfv/bottlenecks/testsuites/posca/testcase_dashboard/\
138 system_bandwidth.py'
139         pargs = [INTERPRETER, cmd]
140         print("Begin to establish dashboard, False means already exist.\n")
141         sub_result = subprocess.Popen(pargs)
142         sub_result.wait()
143     print("System Bandwidth testing time : %s" %(endtime - starttime))
144     time.sleep(5)
145
146 if __name__ == '__main__':
147     main()
148