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