abf8044a2560e97111c96d2c6283be239ac452d0
[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 utils.infra_setup.runner.yardstick as Runner
20 from utils.parser import Parser as conf_parser
21 import docker
22 # --------------------------------------------------
23 # logging configuration
24 # --------------------------------------------------
25 LOG = log.Logger(__name__).getLogger()
26
27 test_dict = {
28     "action": "runTestCase",
29     "args": {
30         "opts": {
31             "task-args": {}
32         },
33         "testcase": "ping_bottlenecks"
34     }
35 }
36
37
38 def env_pre(con_dic):
39     Runner.yardstick_env_prepare(con_dic['contexts'])
40
41
42 def do_test(test_config, con_dic):
43     out_file = ("/tmp/yardstick_" + str(uuid.uuid4()) + ".out")
44     client = docker.from_env()
45     con = client.containers.get('bottleneckcompose_yardstick_1')
46     cmd = ('yardstick task start /home/opnfv/repos/yardstick/'
47            'samples/ping_bottlenecks.yaml --output-file ' + out_file)
48     stdout = con.exec_run(cmd)
49     LOG.debug(stdout)
50     with open(out_file) as f:
51         data = json.load(f)
52         if data["status"] == 1:
53             LOG.info("yardstick run success")
54             out_value = 1
55         else:
56             LOG.error("yardstick error exit")
57             out_value = 0
58     os.remove(out_file)
59     return out_value
60
61
62 def func_run(condic):
63     test_config = {}
64     test_date = do_test(test_config, condic)
65     return test_date
66
67
68 def run(test_config):
69     con_dic = test_config["load_manager"]
70     test_num = con_dic['scenarios']['num_stack'].split(',')
71     if con_dic["contexts"]["yardstick_test_ip"] is None:
72         con_dic["contexts"]["yardstick_test_ip"] =\
73             conf_parser.ip_parser("yardstick_test_ip")
74
75     env_pre(con_dic)
76
77     for num in test_num:
78         result = []
79         out_num = 0
80         pool = multiprocessing.Pool(processes=num)
81         for i in range(0, int(num)):
82             result.append(pool.apply_async(func_run, (con_dic, )))
83         pool.close()
84         pool.join()
85         for res in result:
86             out_num = out_num + float(res.get())
87         LOG.info("%s thread success %d times" % (num, out_num))
88         if out_num < num:
89             success_rate = ('%d/%d' % (out_num, num))
90             LOG.error('error thread: %d '
91                       'the successful rate is %s'
92                       % (num - out_num, success_rate))
93             break
94     LOG.info('END POSCA stress ping test')