Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / agent / perf / vstfperf.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 __doc__ = """
11 operation: [start, stop, restart]
12 action: [send, receive]
13 tool: [pktgen, netperf, qperf, iperf, netmap]
14 params:
15     protocol: [tcp_lat, udp_lat, tcp_bw, udp_bw]
16     namespace: None
17     src:[
18        { "iface":"eth0", "ip":"xxx.xxx.xxx.xxx", "mac":"FF:FF:FF:FF:FF:FF"}
19     ]
20     dst:[
21        { "iface":"eth0", "ip":"xxx.xxx.xxx.xxx", "mac":"FF:FF:FF:FF:FF:FF"}
22     ]
23     size: 64
24     threads: 1
25     ratep: 100000  (pps)
26     time: 100  (s)
27 """
28
29 import sys
30 import logging
31 import vstf.common.constants as cst
32 import vstf.common.decorator as deco
33 import vstf.agent.perf.pktgen as vstf_pktgen
34 import vstf.agent.perf.netmap as vstf_netmap
35 import vstf.agent.perf.qperf as vstf_qperf
36 import vstf.agent.perf.iperf as vstf_iperf
37 import vstf.agent.perf.netperf as vstf_netperf
38
39 LOG = logging.getLogger(__name__)
40
41
42 class Vstfperf(object):
43
44     def __init__(self):
45         for tool in cst.TOOLS:
46             obj_name = 'vstf_' + tool
47             obj = getattr(sys.modules[__name__], obj_name)
48             cls_name = tool.title()
49             cls = getattr(obj, tool.title())
50             self.__dict__.update({tool: cls()})
51
52     @deco.check("operation", choices=cst.OPERATIONS)
53     @deco.check("action", choices=cst.ACTIONS)
54     @deco.check("tool", choices=cst.TOOLS)
55     @deco.check("params", defaults={})
56     def run(self, **kwargs):
57         print "_run in"
58         operation = kwargs.pop("operation")
59         tool = kwargs.pop("tool")
60         instance = getattr(self, tool)
61         action = kwargs.pop("action")
62         func_name = "%s_%s" % (action, operation)
63         func = getattr(instance, func_name)
64         LOG.info(kwargs['params'])
65         LOG.info(func)
66         ret = func(**kwargs['params'])
67         return ret
68
69     def force_clean(self):
70         LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
71         for tool in cst.TOOLS:
72             instance = getattr(self, tool)
73             instance.force_clean()
74         return True
75
76
77 def unit_test():
78     from vstf.common.log import setup_logging
79     setup_logging(
80         level=logging.DEBUG,
81         log_file="/var/log/vstf/vstf-vstfperf.log",
82         clevel=logging.INFO)
83
84     perf = Vstfperf()
85     start = {
86         "operation": "start",
87         "action": "send",
88         "tool": "netperf",
89         "params": {
90             "namespace": "vnet_name1",
91             "protocol": "udp_lat",
92             "dst": [
93                 {"ip": "192.168.1.102"}
94             ],
95             "size": 64,
96             "threads": 1,
97             "time": 100,
98         },
99     }
100     perf.run(**start)
101
102     stop = {
103         "operation": "stop",
104         "action": "send",
105         "tool": "netperf",
106     }
107     perf.run(**stop)
108
109
110 if __name__ == '__main__':
111     unit_test()