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