JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / controller / sw_perf / model.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 import logging
11
12 from vstf.controller.fabricant import Fabricant
13 from vstf.controller.sw_perf.raw_data import RawDataProcess
14 from vstf.common import perfmark as mark
15
16 LOG = logging.getLogger(__name__)
17
18
19 class NetDeviceMgr(Fabricant):
20     @classmethod
21     def add(cls, dst, conn, dev):
22         self = cls(dst, conn)
23         LOG.info(dev)
24         ret = self.config_dev(netdev=dev)
25         LOG.info(ret)
26
27     @classmethod
28     def remove(cls, dst, conn, dev):
29         self = cls(dst, conn)
30         LOG.info(dev)
31         ret = self.recover_dev(netdev=dev)
32         LOG.info(ret)
33
34     @classmethod
35     def clear(cls, dst, conn):
36         self = cls(dst, conn)
37         self.clean_all_namespace()
38
39
40 class Actor(Fabricant):
41     def __init__(self, dst, conn, tool, params):
42         super(Actor, self).__init__(dst, conn)
43         self._tool = tool
44         self._params = params
45         self._data = {}
46
47     def __repr__(self):
48         repr_dict = self.__dict__
49         repr_keys = list(repr_dict.keys())
50         repr_keys.sort()
51         return '%s(%s)' % (self.__class__.__name__, ', '.join(['%s=%r' % (k, repr_dict[k]) for k in repr_keys]))
52
53
54 class Sender(Actor):
55     def start(self, pktsize, **kwargs):
56         LOG.info("Sender.start")
57         if 'ratep' in kwargs and kwargs['ratep']:
58             self._params['ratep'] = kwargs['ratep']
59         self._params['size'] = pktsize
60
61         ret, info = self.perf_run(
62             operation="start",
63             action="send",
64             tool=self._tool,
65             params=self._params
66         )
67         LOG.info(ret)
68         if ret:
69             raise Exception(info)
70         LOG.info(info)
71         print ret
72
73     def stop(self):
74         LOG.info(self._params)
75         rets = self.perf_run(
76             operation="stop",
77             action="send",
78             tool=self._tool,
79             params={}
80         )
81         LOG.info(rets)
82         minlatency, avglatency, maxlatency = 0, 0, 0
83         count = 0
84         for (ret, info) in rets:
85             if ret:
86                 raise Exception(info)
87             if self.is_data() and ret == 0:
88                 count += 1
89                 minlatency += info[mark.minLatency]
90                 avglatency += info[mark.avgLatency]
91                 maxlatency += info[mark.maxLatency]
92         count = 1 if not count else count
93         self._data[mark.minLatency] = minlatency / count
94         self._data[mark.avgLatency] = avglatency / count
95         self._data[mark.maxLatency] = maxlatency / count
96
97         print rets
98
99     def is_data(self):
100         if '_lat' in self._params['protocol']:
101             return True
102         return False
103
104     def result(self):
105         return self._data
106
107
108 class Receiver(Actor):
109     def start(self, **kwargs):
110         LOG.info("Receiver.start")
111         ret, info = self.perf_run(
112             operation="start",
113             action="receive",
114             tool=self._tool,
115             params=self._params
116         )
117         LOG.info(ret)
118         if ret:
119             raise Exception(info)
120         LOG.info(info)
121         return ret
122
123     def stop(self):
124         LOG.info("Receiver.stop")
125         ret, info = self.perf_run(
126             operation="stop",
127             action="receive",
128             tool=self._tool,
129             params=self._params
130         )
131         LOG.info(ret)
132         if ret:
133             raise Exception(info)
134         LOG.info(info)
135         return ret
136
137
138 class NicWatcher(Fabricant):
139     def __init__(self, dst, conn, params):
140         super(NicWatcher, self).__init__(dst, conn)
141         self._params = params
142         self._pid = None
143         self._data = {}
144
145     def start(self):
146         print "NicWatcher.start"
147         self._pid = self.run_vnstat(device=self._params["iface"], namespace=self._params["namespace"])
148         print self._pid
149
150     def stop(self):
151         print "NicWatcher.stop"
152         if self._pid:
153             data = self.kill_vnstat(pid=self._pid)
154             self._data = RawDataProcess.process(data)
155             print "---------------------------------"
156             print self._data
157             print "---------------------------------"
158
159     def result(self, **kwargs):
160         return self._data
161
162
163 class CpuWatcher(Fabricant):
164     def __init__(self, dst, conn):
165         super(CpuWatcher, self).__init__(dst, conn)
166         self._pid = None
167         self._data = {
168             "cpu_num": 0,
169             "idle": 0,
170             "cpu_mhz": 0
171         }
172
173     def start(self):
174         print "CpuWatcher.start"
175         self._pid = self.run_cpuwatch()
176         print self._pid
177
178     def stop(self):
179         print "CpuWatcher.stop"
180         if self._pid:
181             print self._pid
182             data = self.kill_cpuwatch(pid=self._pid)
183             self._data = RawDataProcess.process(data)
184             print "---------------------------------"
185             print self._data
186             print "---------------------------------"
187
188     def result(self, **kwargs):
189         return self._data
190
191
192 def unit_test():
193     pass
194
195
196 if __name__ == '__main__':
197     unit_test()