Bottlenecks testpmd scale-up testcase.
[bottlenecks.git] / utils / infra_setup / runner / yardstick.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 contain all function about yardstick API.
11 At present, This file contain the following function:
12 1.Ask Yardstick to run testcase and get a task id.
13 2.use task id to ask yardstick for data.
14 3.Ask yardstick for InfluxDB create
15 4.how the process of task.'''
16
17 import sys
18 import time
19 import requests
20 import json
21 import utils.logger as logger
22
23 headers = {"Content-Type": "application/json"}
24 LOG = logger.Logger(__name__).getLogger()
25
26
27 def yardstick_command_parser(debug, cidr, outfile, parameter):
28     cmd = "yardstick"
29     if debug:
30         cmd += " -d"
31     cmd += " task start "
32     cmd += str(cidr)
33     cmd += " --output-file " + outfile
34     if parameter is not None:
35         cmd += " --task-args " + '"' + str(parameter) + '"'
36     return cmd
37
38
39 def Get_Reply(test_config, task_id, time_test=1):
40     reply_url = ("http://%s/yardstick/results?task_id=%s"
41                  % (test_config['yardstick_test_ip'], task_id))
42     reply_response = requests.get(reply_url)
43     reply_data = json.loads(reply_response.text)
44     LOG.info("return data is %s" % (reply_data))
45     if reply_data["status"] == 1:
46         return(reply_data["result"])
47     if reply_data["status"] == 0:
48         if time_test == 100:
49             LOG.info("yardstick time out")
50             sys.exit()
51         time.sleep(10)
52         reply_result_data = Get_Reply(
53             test_config, task_id, time_test=time_test + 1)
54         return(reply_result_data)
55     if reply_data["status"] == 2:
56         LOG.error("yardstick error exit")
57         sys.exit()
58
59
60 def Send_Data(test_dict, test_config):
61     base_url = ("http://%s/yardstick/testcases/%s/action"
62                 % (test_config['yardstick_test_ip'],
63                    test_config['yardstick_test_dir']))
64     LOG.info("test ip addr is %s" % (base_url))
65     reponse = requests.post(
66         base_url, data=json.dumps(test_dict), headers=headers)
67     ask_data = json.loads(reponse.text)
68     task_id = ask_data["result"]
69     LOG.info("yardstick task id is: %s" % (task_id))
70     return task_id
71
72
73 def Create_Incluxdb(con_dic):
74     base_url = ("http://%s/yardstick/env/action"
75                 % (con_dic['yardstick_test_ip']))
76     test_dict = {
77         "action": "createInfluxDBContainer",
78     }
79     responce = requests.post(
80         base_url, data=json.dumps(test_dict), headers=headers)
81     ask_data = json.loads(responce.text)
82     task_id = ask_data["result"]["task_id"]
83     LOG.info("waiting for creating InfluxDB")
84     time.sleep(30)
85     return task_id
86
87
88 def yardstick_env_prepare(con_dic):
89     base_url = ("http://%s/yardstick/env/action"
90                 % (con_dic['yardstick_test_ip']))
91     test_dict = {
92         "action": "prepareYardstickEnv",
93     }
94     LOG.info("waiting for yardstick environment prepare")
95     reponse = requests.post(
96         base_url, data=json.dumps(test_dict), headers=headers)
97     ask_data = json.loads(reponse.text)
98     task_id = ask_data["result"]["task_id"]
99     LOG.info("Done, yardstick environment prepare complete!")
100     return task_id
101
102
103 def find_condition(con_dic):
104     base_url = ("http://%s/yardstick/asynctask?%s"
105                 % (con_dic['yardstick_test_ip'].id))
106     requests.post(
107         base_url, headers=headers)
108     LOG.info("check for creating InfluxDB")