Testcase to find storage bottlenecks using Yardstick for Multistack
[bottlenecks.git] / testsuites / posca / testcase_script / posca_factor_multistack_storage_parallel.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 posca multistack storage stress test
11 This file contain several part:
12 First is create a script to realize several threading run'''
13
14 import utils.logger as log
15 import uuid
16 import json
17 import os
18 import time
19 from utils.parser import Parser as conf_parser
20 import utils.env_prepare.quota_prepare as quota_prepare
21 import utils.env_prepare.stack_prepare as stack_prepare
22 import utils.infra_setup.runner.yardstick as yardstick_task
23
24 import utils.infra_setup.runner.docker_env as docker_env
25
26 # --------------------------------------------------
27 # logging configuration
28 # --------------------------------------------------
29 LOG = log.Logger(__name__).getLogger()
30
31 test_dict = {
32     "action": "runTestCase",
33     "args": {
34         "opts": {
35             "task-args": {}
36         },
37         "testcase": "multistack_storage_bottlenecks_parallel"
38     }
39 }
40 testfile = os.path.basename(__file__)
41 testcase, file_format = os.path.splitext(testfile)
42 cidr = "/home/opnfv/repos/yardstick/samples/storage_bottlenecks.yaml"
43 runner_DEBUG = True
44
45
46 def env_pre(test_config):
47     test_yardstick = False
48     if "yardstick" in test_config["contexts"].keys():
49         test_yardstick = True
50     stack_prepare._prepare_env_daemon(test_yardstick)
51     quota_prepare.quota_env_prepare()
52     if(test_config["contexts"]['yardstick_envpre']):
53         cmd = ('yardstick env prepare')
54         LOG.info("yardstick environment prepare!")
55         yardstick_container = docker_env.yardstick_info['container']
56         stdout = docker_env.docker_exec_cmd(yardstick_container, cmd)
57         LOG.debug(stdout)
58
59
60 def testcase_parser(out_file="yardstick.out", **parameter_info):
61     cmd = yardstick_task.yardstick_command_parser(debug=runner_DEBUG,
62                                                   cidr=cidr,
63                                                   outfile=out_file,
64                                                   parameter=parameter_info)
65     return cmd
66
67
68 def do_test(test_config):
69     out_file = ("/tmp/yardstick_" + str(uuid.uuid4()) + ".out")
70     yardstick_container = docker_env.yardstick_info['container']
71     cmd = testcase_parser(out_file=out_file, **test_config)
72     print(cmd)
73     stdout = docker_env.docker_exec_cmd(yardstick_container, cmd)
74     LOG.info(stdout)
75     loop_value = 0
76     while loop_value < 60:
77         time.sleep(2)
78         loop_value = loop_value + 1
79         with open(out_file) as f:
80             data = json.load(f)
81             if data["status"] == 1:
82                 LOG.info("yardstick run success")
83                 LOG.info("%s" % data["result"]["testcases"])
84                 break
85             elif data["status"] == 2:
86                 LOG.error("yardstick error exit")
87                 break
88
89     save_data = config_to_result(test_config, data)
90     LOG.info(save_data)
91     return save_data
92
93
94 def config_to_result(test_config, test_result):
95     print(test_result)
96     out_data = test_result["result"]["testcases"]
97     test_data = out_data["storage_bottlenecks"]["tc_data"]
98     testdata = {}
99     testdata["read_iops"] = 0
100     testdata["read_bw"] = 0
101     testdata["read_lat"] = 0
102     testdata["write_iops"] = 0
103     testdata["write_bw"] = 0
104     testdata["write_lat"] = 0
105     print(testdata["read_iops"])
106     for result in test_data:
107         testdata["read_iops"] += result["data"]["read_iops"]
108         testdata["read_bw"] += result["data"]["read_bw"]
109         if testdata["read_lat"] is 0:
110             testdata["read_lat"] = result["data"]["read_lat"]
111         else:
112             testdata["read_lat"] += result["data"]["read_lat"]
113             testdata["read_lat"] = testdata["read_lat"]/2
114         testdata["write_iops"] += result["data"]["write_iops"]
115         testdata["write_bw"] += result["data"]["write_bw"]
116         if testdata["write_lat"] is 0:
117             testdata["write_lat"] = result["data"]["write_lat"]
118         else:
119             testdata["write_lat"] += result["data"]["write_lat"]
120             testdata["write_lat"] = testdata["write_lat"]/2
121     return testdata
122
123
124 def run(test_config):
125     con_dic = test_config["load_manager"]
126     scenarios_conf = con_dic["scenarios"]
127
128     if test_config["contexts"]["yardstick_ip"] is None:
129         con_dic["contexts"]["yardstick_ip"] =\
130             conf_parser.ip_parser("yardstick_test_ip")
131
132     env_pre(test_config)
133     LOG.info("yardstick environment prepare done!")
134
135     test_num = conf_parser.str_to_list(scenarios_conf["num_stack"])
136     rw = scenarios_conf["rw"]
137     bs = scenarios_conf["bs"]
138     size = scenarios_conf["size"]
139     rwmixwrite = scenarios_conf["rwmixwrite"]
140     numjobs = scenarios_conf["num_jobs"]
141     direct = scenarios_conf["direct"]
142     volume_num = scenarios_conf["volume_num"]
143
144     result = []
145
146     for value in test_num:
147         case_config = {"stack_num": int(value),
148                        "volume_num": volume_num,
149                        "rw": rw,
150                        "bs": bs,
151                        "size": size,
152                        "rwmixwrite": rwmixwrite,
153                        "numjobs": numjobs,
154                        "direct": direct}
155         data_reply = do_test(case_config)
156         result.append(data_reply)
157
158         LOG.info("%s stack successful run" % (value))
159
160         conf_parser.result_to_file(data_reply, test_config["out_file"])
161
162     LOG.info('END POSCA stress multistack storage parallel testcase')
163     LOG.info("The result data is %s", result)
164     return result