2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
14 import subprocess as sub
15 from pyroute2 import IPDB
18 headers = {"Content-Type": "application/json"}
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'])
31 print("ERROR: %6s lines have errors, total cost %d ms."
32 % (len(ret_dict['items']), ret_dict['took']))
35 def posca_config_read(config_str, con_str, config):
36 print("========== posca system bandwidth config read ===========")
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]))
47 GATEWAY_IP = ip.routes['default'].gateway
48 if str(con_dic["test_ip"]) is "":
49 con_dic["test_ip"] = GATEWAY_IP+":3333"
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"]))
57 def posca_output_result(file_config, data_reply):
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"]
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))
73 f.write(json.dumps(data_reply, f))
78 def posca_get_reply(con_dic, task_id, time_test=1):
79 reply_url = "http://%s/yardstick/result/action?action=getResult&task_id=%s\
80 &measurement=tc100" % (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)
85 if reply_data["status"] == 1:
86 return(reply_data["result"][0])
87 if reply_data["status"] == 0:
89 print("yardstick time out")
91 posca_get_reply(con_dic, task_id, time_test=time_test+1)
92 if reply_data["status"] == 2:
93 print("yardstick error exit")
96 def posca_send_data(con_dic, test_config, file_config):
97 base_url = "http://%s/yardstick/test/action" % (con_dic['test_ip'])
98 print(con_dic["test_ip"])
100 "action":"runTestCase",
104 'tx_msg_size': '%s' % str(test_config["tx_msg_size"]),
105 'rx_msg_size': '%s' % str(test_config["rx_msg_size"]),
106 'test_time': '%s' % str(int(con_dic["test_time"]) - 20),
115 reponse = requests.post(
116 base_url, data=json.dumps(test_dict), headers=headers)
117 ask_data = json.loads(reponse.text)
118 task_id = ask_data["task_id"]
119 data_reply = posca_get_reply(con_dic, task_id)
120 data_reply.update(test_config)
121 posca_output_result(file_config, data_reply)