Add frame support of elk one docker support
[bottlenecks.git] / testsuites / posca / testcase_script / posca_factor_ping.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 ping stress test script
11 This file contain several part:
12 Frist 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 import multiprocessing
20 import datetime
21 from utils.parser import Parser as conf_parser
22 import utils.env_prepare.quota_prepare as quota_prepare
23 import utils.env_prepare.stack_prepare as stack_prepare
24
25 import testsuites.posca.testcase_dashboard.posca_stress_ping as DashBoard
26 import utils.infra_setup.runner.docker_env as docker_env
27 # --------------------------------------------------
28 # logging configuration
29 # --------------------------------------------------
30 LOG = log.Logger(__name__).getLogger()
31
32 test_dict = {
33     "action": "runTestCase",
34     "args": {
35         "opts": {
36             "task-args": {}
37         },
38         "testcase": "ping_bottlenecks"
39     }
40 }
41 testfile = os.path.basename(__file__)
42 testcase, file_format = os.path.splitext(testfile)
43
44
45 def env_pre(test_config):
46     test_yardstick = False
47     if "yardstick" in test_config["contexts"].keys():
48         test_yardstick = True
49     stack_prepare._prepare_env_daemon(test_yardstick)
50     quota_prepare.quota_env_prepare()
51     cmd = ('yardstick env prepare')
52     LOG.info("yardstick envrionment prepare!")
53     if(test_config["contexts"]['yardstick_envpre']):
54         yardstick_container = docker_env.yardstick_info['container']
55         stdout = docker_env.docker_exec_cmd(yardstick_container, cmd)
56         LOG.debug(stdout)
57
58
59 def do_test(test_config, con_dic):
60     out_file = ("/tmp/yardstick_" + str(uuid.uuid4()) + ".out")
61     yardstick_container = docker_env.yardstick_info['container']
62     cmd = ('yardstick task start /home/opnfv/repos/yardstick/'
63            'samples/ping_bottlenecks.yaml --output-file ' + out_file)
64     stdout = docker_env.docker_exec_cmd(yardstick_container, cmd)
65     LOG.info(stdout)
66     out_value = 0
67     loop_walue = 0
68     while loop_walue < 150:
69         time.sleep(2)
70         with open(out_file) as f:
71             loop_walue = loop_walue + 1
72             data = json.load(f)
73             if data["status"] == 1:
74                 if data["result"]["criteria"] == "PASS":
75                     LOG.info("yardstick run success")
76                     out_value = 1
77                 else:
78                     LOG.error("task error exit")
79                     out_value = 0
80                 break
81             elif data["status"] == 2:
82                 LOG.error("yardstick error exit")
83     return out_value
84
85
86 def config_to_result(num, out_num, during_date):
87     testdata = {}
88     test_result = {}
89     test_result["number_of_users"] = float(num)
90     test_result["success_times"] = out_num
91     test_result["success_rate"] = out_num / num
92     test_result["duration_time"] = during_date
93     testdata["data_body"] = test_result
94     testdata["testcase"] = testcase
95     return testdata
96
97
98 def func_run(condic):
99     test_config = {}
100     test_date = do_test(test_config, condic)
101     return test_date
102
103
104 def run(test_config):
105     con_dic = test_config["load_manager"]
106     test_num = con_dic['scenarios']['num_stack'].split(',')
107     if test_config["contexts"]["yardstick_ip"] is None:
108         con_dic["contexts"]["yardstick_ip"] =\
109             conf_parser.ip_parser("yardstick_test_ip")
110
111     if "dashboard" in test_config["contexts"].keys():
112         if test_config["contexts"]["dashboard_ip"] is None:
113             test_config["contexts"]["dashboard_ip"] =\
114                 conf_parser.ip_parser("dashboard")
115         LOG.info("Create Dashboard data")
116         DashBoard.posca_stress_ping(test_config["contexts"])
117
118     LOG.info("bottlenecks envrionment prepare!")
119     env_pre(test_config)
120     LOG.info("yardstick envrionment prepare done!")
121
122     for value in test_num:
123         result = []
124         out_num = 0
125         num = int(value)
126         pool = multiprocessing.Pool(processes=num)
127         LOG.info("begin to run %s thread" % num)
128
129         starttime = datetime.datetime.now()
130         for i in range(0, int(num)):
131             result.append(pool.apply_async(func_run, (con_dic, )))
132         pool.close()
133         pool.join()
134         for res in result:
135             out_num = out_num + float(res.get())
136
137         endtime = datetime.datetime.now()
138         LOG.info("%s thread success %d times" % (num, out_num))
139         during_date = (endtime - starttime).seconds
140
141         data_reply = config_to_result(num, out_num, during_date)
142         if "dashboard" in test_config["contexts"].keys():
143             DashBoard.dashboard_send_data(test_config['contexts'], data_reply)
144         conf_parser.result_to_file(data_reply, test_config["out_file"])
145
146         if out_num < num:
147             success_rate = ('%d/%d' % (out_num, num))
148             LOG.error('error thread: %d '
149                       'the successful rate is %s'
150                       % (num - out_num, success_rate))
151             break
152     LOG.info('END POSCA stress ping test')