Dashboard and output file code reconstruction
[bottlenecks.git] / testsuites / posca / testcase_script / posca_factor_system_bandwidth.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2017 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 '''This file realize the function of run systembandwidth script.
11 for example this contain two part first run_script,
12 second is algorithm, this part is about how to judge the bottlenecks.
13 This test is using yardstick as a tool to begin test.'''
14
15 import os
16 import time
17 import utils.logger as log
18 import utils.infra_setup.runner.yardstick as Runner
19 from utils.parser import Parser as conf_parser
20 import testsuites.posca.testcase_dashboard.system_bandwidth as DashBoard
21 # --------------------------------------------------
22 # logging configuration
23 # --------------------------------------------------
24 LOG = log.Logger(__name__).getLogger()
25
26 test_dict = {
27     "action": "runTestCase",
28     "args": {
29         "opts": {
30             "task-args": {}
31         },
32         "testcase": "netperf_bottlenecks"
33     }
34 }
35 testfile = os.path.basename(__file__)
36 testcase, file_format = os.path.splitext(testfile)
37
38
39 def env_pre(con_dic):
40     Runner.Create_Incluxdb(con_dic['runner_config'])
41
42
43 def config_to_result(test_config, test_result):
44     testdata = {}
45     test_result["throughput"] = float(test_result["throughput"])
46     test_result.update(test_config)
47     testdata["data_body"] = test_result
48     testdata["testcase"] = testcase
49     return testdata
50
51
52 def do_test(test_config, con_dic):
53     test_dict['args']['opts']['task-args'] = test_config
54     Task_id = Runner.Send_Data(test_dict, con_dic['runner_config'])
55     time.sleep(con_dic['test_config']['test_time'])
56     Data_Reply = Runner.Get_Reply(con_dic['runner_config'], Task_id)
57     try:
58         test_date =\
59             Data_Reply[con_dic['runner_config']['yardstick_testcase']][0]
60     except IndexError:
61         test_date = do_test(test_config, con_dic)
62
63     save_data = config_to_result(test_config, test_date)
64     if con_dic['runner_config']['dashboard'] == 'y':
65         DashBoard.dashboard_send_data(con_dic['runner_config'], save_data)
66
67     return save_data["data_body"]
68
69
70 def run(con_dic):
71     data = {}
72     rx_pkt_a = con_dic['test_config']['rx_pkt_sizes'].split(',')
73     tx_pkt_a = con_dic['test_config']['tx_pkt_sizes'].split(',')
74     data["rx_pkt_sizes"] = rx_pkt_a
75     data["tx_pkt_sizes"] = tx_pkt_a
76     con_dic["result_file"] = os.path.dirname(
77         os.path.abspath(__file__)) + "/test_case/result"
78     cur_role_result = 1
79     pre_role_result = 1
80     pre_reply = {}
81     data_return = {}
82     data_max = {}
83     data_return["throughput"] = 1
84
85     if con_dic["runner_config"]["yardstick_test_ip"] is None:
86         con_dic["runner_config"]["yardstick_test_ip"] =\
87             conf_parser.ip_parser("yardstick_test_ip")
88
89     env_pre(con_dic)
90
91     if con_dic["runner_config"]["dashboard"] == 'y':
92         if con_dic["runner_config"]["dashboard_ip"] is None:
93             con_dic["runner_config"]["dashboard_ip"] =\
94                 conf_parser.ip_parser("dashboard")
95         LOG.info("Create Dashboard data")
96         DashBoard.dashboard_system_bandwidth(con_dic["runner_config"])
97
98     for test_x in data["tx_pkt_sizes"]:
99         data_max["throughput"] = 1
100         bandwidth_tmp = 1
101         for test_y in data["rx_pkt_sizes"]:
102             test_config = {
103                 "tx_msg_size": float(test_x),
104                 "rx_msg_size": float(test_y),
105                 "test_time": con_dic['test_config']['test_time']
106             }
107             data_reply = do_test(test_config, con_dic)
108             conf_parser.result_to_file(data_reply, con_dic["out_file"])
109             bandwidth = data_reply["throughput"]
110             if (data_max["throughput"] < bandwidth):
111                 data_max = data_reply
112             if (abs(bandwidth_tmp - bandwidth) / bandwidth_tmp < 0.025):
113                 LOG.info("this group of data has reached top output")
114                 break
115             else:
116                 pre_reply = data_reply
117                 bandwidth_tmp = bandwidth
118         cur_role_result = float(pre_reply["throughput"])
119         if (abs(pre_role_result - cur_role_result) / pre_role_result < 0.025):
120             LOG.info("The performance increases slowly")
121         if data_return["throughput"] < data_max["throughput"]:
122             data_return = data_max
123         pre_role_result = cur_role_result
124     LOG.info("Find bottlenecks of this config")
125     LOG.info("The max data is %d", data_return["throughput"])
126     return data_return