stress_test_dashboard_code
[bottlenecks.git] / testsuites / posca / testcase_script / common_script.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 json
12 import requests
13 import time
14 import subprocess as sub
15 from pyroute2 import IPDB
16 import sys
17
18 headers = {"Content-Type": "application/json"}
19
20
21 def posca_tran_data(ES_ip, file_name):
22     p = sub.Popen(['curl', '-s', '-XPOST', "%s/_bulk" % ES_ip,
23                    '--data-binary', "@" + file_name], stdout=sub.PIPE)
24     for line in iter(p.stdout.readline, b''):
25         ret_dict = json.loads(line)
26         if not ret_dict['errors']:
27             print("INFO: %6s lines no errors, total cost %d ms."
28                   % (len(ret_dict['items']), ret_dict['took']))
29             return len(ret_dict['items'])
30         else:
31             print("ERROR: %6s lines have errors, total cost %d ms."
32                   % (len(ret_dict['items']), ret_dict['took']))
33
34
35 def posca_config_read(config_str, con_str, config):
36     print("========== posca system bandwidth config read ===========")
37     con_dic = {}
38     print("Configuration file is %s" % (config_str))
39     idx = 0
40     with open(config_str, "rd") as cfgfile:
41         config.readfp(cfgfile)
42         while idx < len(con_str):
43             con_dic[str(con_str[idx])] = \
44                 config.get("config", str(con_str[idx]))
45             idx += 1
46     with IPDB() as ip:
47         GATEWAY_IP = ip.routes['default'].gateway
48     if str(con_dic["test_ip"]) is "":
49         con_dic["test_ip"] = GATEWAY_IP + ":8888"
50         print("test_ip is null get local ip is %s" % (con_dic["test_ip"]))
51     if con_dic["ES_ip"] is "":
52         con_dic["ES_ip"] = GATEWAY_IP + ":9200"
53         print("ES_ip is null get local ip is %s" % (con_dic["ES_ip"]))
54     return con_dic
55
56
57 def posca_output_result(file_config, data_reply):
58     data_head = {}
59     data_head["index"] = {}
60     data_head["index"]["_index"] = "bottlenecks"
61     data_head["index"]["_type"] = file_config["test_type"]
62     data_head["index"]["_id"] = file_config["test_id"]
63
64     data_reply["throughput"] = float(data_reply["throughput"])
65     data_reply["mean_latency"] = float(data_reply["mean_latency"])
66     data_reply["remote_cpu_util"] = float(data_reply["remote_cpu_util"])
67     data_reply["local_cpu_util"] = float(data_reply["local_cpu_util"])
68     data_reply["local_transport_retrans"] =\
69         float(data_reply["local_transport_retrans"])
70     with open(file_config["file_path"], "a") as f:
71         f.write(json.dumps(data_head, f))
72         f.write("\n")
73         f.write(json.dumps(data_reply, f))
74         f.write("\n")
75         f.close()
76
77
78 def posca_get_reply(con_dic, task_id, time_test=1):
79     reply_url = "http://%s/yardstick/results?action=getResult&task_id=%s\
80 &measurement=netperf_bottlenecks" % (con_dic["test_ip"], task_id)
81     time.sleep(float(con_dic["test_time"]))
82     reply_response = requests.get(reply_url)
83     reply_data = json.loads(reply_response.text)
84     print("return data is %s" % (reply_data))
85     if reply_data["status"] == 1:
86         return(reply_data["result"][0])
87     if reply_data["status"] == 0:
88         if time_test == 10:
89             print("yardstick time out")
90             sys.exit()
91         reply_result_data = posca_get_reply(
92             con_dic, task_id, time_test=time_test + 1)
93         return(reply_result_data)
94         posca_get_reply(con_dic, task_id, time_test=time_test + 1)
95     if reply_data["status"] == 2:
96         print("yardstick error exit")
97         sys.exit()
98
99
100 def posca_send_data(con_dic, test_config, file_config):
101     base_url = "http://%s/yardstick/testcases/samples/action" % (con_dic[
102                                                                  'test_ip'])
103     print("test ip addr is %s" % (base_url))
104     test_dict = {
105         "action": "runTestCase",
106         "args": {
107             "opts": {
108                 "task-args": {
109                     'tx_msg_size': '%s' % str(test_config["tx_msg_size"]),
110                     'rx_msg_size': '%s' % str(test_config["rx_msg_size"]),
111                     'test_time': '%s' % str(int(con_dic["test_time"]) - 20),
112                     'host': 'node3.LF',
113                     'target': 'node4.LF'
114                 }
115             },
116             "testcase": "netperf_bottlenecks"
117         }
118     }
119     reponse = requests.post(
120         base_url, data=json.dumps(test_dict), headers=headers)
121     ask_data = json.loads(reponse.text)
122     task_id = ask_data["result"]
123     print("yardstick task id is: %s" % (task_id))
124     data_reply = posca_get_reply(con_dic, task_id)
125     data_reply.update(test_config)
126     posca_output_result(file_config, data_reply)
127     return data_reply
128
129
130 def posca_create_incluxdb(con_dic):
131     base_url = "http://%s/yardstick/env/action" % (con_dic['test_ip'])
132     test_dict = {
133         "action": "createInfluxDBContainer",
134     }
135     requests.post(
136         base_url, data=json.dumps(test_dict), headers=headers)
137     print("waiting for creating InfluxDB")
138     time.sleep(30)
139     print("Done, creating InflxDB Container")