Merge "Add Factor Testcase RX_PKT_Size"
[bottlenecks.git] / testsuites / posca / testcase_script / posca_factor_tx_cache_size.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2016 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
11 import os
12 import argparse
13 import time
14 import logging
15 import ConfigParser
16 import json
17
18 #------------------------------------------------------
19 # parser for configuration files in each test case
20 # ------------------------------------------------------
21 parser = argparse.ArgumentParser()
22 parser.add_argument("-c", "--conf",
23                     help="configuration files for the testcase, in yaml format",
24                     default="/home/opnfv/bottlenecks/testsuites/posca/testcase_cfg/posca_factor_tx_cache_size.yaml")
25 args = parser.parse_args()
26
27 cmd="curl -i"
28 order_arg="-H \"Content-Type: application/json\" -X POST -d \'{\"cmd\": \"start\", \"opts\":{\"output-file\": \"/tem/yardstick.out\"}, \"args\": \"../samples/netperf.yaml\"}'"
29
30 #--------------------------------------------------
31 # logging configuration
32 #--------------------------------------------------
33 logger = logging.getLogger(__name__)
34
35 def posca_env_check():
36     print "========== posca system bandwidth env check ==========="
37     if os.path.exists(r'/home/opnfv/bottlenecks/testsuites/posca/test_result/'):
38         return True
39     else:
40         os.mkdirs(r'/home/opnfv/bottlenecks/testsuites/posca/test_result/')
41
42 def posca_output_result(time_new,input_1,input_2,input_3,input_4,input_5):
43     save_dic={}
44     save_dic['tx_pkt_size']=input_1
45     save_dic['rx_cache_size']=input_2
46     save_dic['throughput ']=input_3
47     save_dic['latency']=input_4
48     save_dic['cpu_load']=input_5
49     with open("/home/opnfv/bottlenecks/testsuites/posca/test_result/factor_tx_cache_size_%s.json"%(time_new),"a") as f:
50         f.write(json.dumps(save_dic,f))
51         f.write("\n")
52
53 def posca_config_read(config_str):
54     print "========== posca system bandwidth config read ==========="
55
56     con_dic = {}
57     config = ConfigParser.ConfigParser()
58     with open(config_str,"rd") as cfgfile:
59         config.readfp(cfgfile)
60         con_dic['test_ip']=config.get("config","test_ip")
61         con_dic['test_throughput']=config.get("config","throughput")
62         con_dic['test_tool']=config.get("config","tool")
63         con_dic['test_time']=config.get("config","test_time")
64         con_dic['test_protocol']=config.get("config","protocol")
65         con_dic['test_pkt_s']=config.get("config","pkt sizes")
66         con_dic['test_tx_cache_s']=config.get("config","tx cache sizes")
67         con_dic['test_rx_cache_s']=config.get("config","rx cache sizes")
68         con_dic['test_cpu_load']=config.get("config","cpu load")
69         con_dic['test_latency']=config.get("config","latency")
70
71     return con_dic
72
73 def posca_run(con_dic):
74     print "========== run posca system bandwidth ==========="
75
76     test_pkt_s_a = con_dic['test_pkt_s'].split(',')
77     test_rx_cache_s_a = con_dic['test_rx_cache_s'].split(',')
78     time_new = time.strftime('%H_%M',time.localtime(time.time()))
79     bandwidth_tmp = 1
80
81     for test_pkt_s_e in test_pkt_s_a:
82         for test_rx_cache_s_e in test_rx_cache_s_a:
83             print "Package size %s"%(test_pkt_s_e)
84             order_excute = os.popen("%s %s http://%s/api/v3/yardstick/tasks/task %s %s %s %s"%(cmd,order_arg,con_dic['test_ip'],test_pkt_s_e,test_pkt_s_e,con_dic['test_tx_cache_s'],test_rx_cache_s_e))
85             order_result = order_excute.read()
86             test_id = order_result.find("task_id")
87             time.sleep(con_dic['test_time'])
88             cmd_excute = os.popen( "%s http://%s/api/v3/yardstick/testresults?task_id=%s"%(cmd,con_dic['test_ip'],test_id))
89             test_result = cmd_excute.read()
90             bandwidth = test_result.find("bandwidth")
91             cpu_load = test_result.find("cpu_load")
92             latency = test_result.find("latency")
93             posca_output_result(time_new,test_pkt_s_e,test_rx_cache_s_e,bandwidth,latency,cpu_load)
94             if (abs(bandwidth-con_dic['test_throughput'])/con_dic['test_throughput'] >0.05) and (latency < con_dic['test_latency']) and (cpu_load < con_dic['test_cpu_load']):
95                 if abs(bandwidth_tmp-bandwidth)/bandwidth <0.05:
96                     return True
97                 else:
98                     print "%s,%s"%(bandwidth,test_rx_cache_s_e)
99             else:
100                 print "%s,%s"%(bandwidth,test_rx_cache_s_e)
101                 return False
102
103 def main():
104     if not (args.conf):
105        logger.error("Configuration files do not exist for the specified testcases")
106        exit(-1)
107     else:
108        testcase_cfg = args.conf
109
110     con_dic=posca_config_read(testcase_cfg)
111     posca_env_check()
112     posca_run(con_dic)
113
114     time.sleep(5)
115
116 if __name__=='__main__':
117     main()