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