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