JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / 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     def __init__(self):
44         for tool in cst.TOOLS:
45             obj_name = 'vstf_' + tool
46             obj = getattr(sys.modules[__name__], obj_name)
47             cls_name = tool.title()
48             cls = getattr(obj, tool.title())
49             self.__dict__.update({tool: cls()})
50
51     @deco.check("operation", choices=cst.OPERATIONS)
52     @deco.check("action", choices=cst.ACTIONS)
53     @deco.check("tool", choices=cst.TOOLS)
54     @deco.check("params", defaults={})
55     def run(self, **kwargs):
56         print "_run in"
57         operation = kwargs.pop("operation")
58         tool = kwargs.pop("tool")
59         instance = getattr(self, tool)
60         action = kwargs.pop("action")
61         func_name = "%s_%s" % (action, operation)
62         func = getattr(instance, func_name)
63         LOG.info(kwargs['params'])
64         LOG.info(func)
65         ret = func(**kwargs['params'])
66         return ret
67
68     def force_clean(self):
69         LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
70         for tool in cst.TOOLS:
71             instance = getattr(self, tool)
72             instance.force_clean()
73         return True
74
75
76 def unit_test():
77     from vstf.common.log import setup_logging
78     setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-vstfperf.log", clevel=logging.INFO)
79
80     perf = Vstfperf()
81     start = {
82         "operation": "start",
83         "action": "send",
84         "tool": "netperf",
85         "params": {
86             "namespace": "vnet_name1",
87             "protocol": "udp_lat",
88             "dst": [
89                 {"ip": "192.168.1.102"}
90             ],
91             "size": 64,
92             "threads": 1,
93             "time": 100,
94         },
95     }
96     perf.run(**start)
97
98     stop = {
99         "operation": "stop",
100         "action": "send",
101         "tool": "netperf",
102     }
103     perf.run(**stop)
104
105
106 if __name__ == '__main__':
107     unit_test()