Merge "Module to manage pip packages"
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_rfc2544_trex.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """ Trex traffic generation definitions which implements rfc2544 """
15
16 from __future__ import absolute_import
17 from __future__ import print_function
18 import time
19 import logging
20 from collections import Mapping
21
22 from yardstick.network_services.vnf_generic.vnf.tg_trex import TrexTrafficGen
23 from yardstick.network_services.vnf_generic.vnf.sample_vnf import Rfc2544ResourceHelper
24 from yardstick.network_services.vnf_generic.vnf.tg_trex import TrexResourceHelper
25
26 LOGGING = logging.getLogger(__name__)
27
28
29 class TrexRfc2544ResourceHelper(Rfc2544ResourceHelper):
30
31     def is_done(self):
32         return self.latency and self.iteration.value > 10
33
34
35 class TrexRfcResourceHelper(TrexResourceHelper):
36
37     LATENCY_TIME_SLEEP = 120
38     RUN_DURATION = 30
39     WAIT_TIME = 3
40
41     def __init__(self, setup_helper, rfc_helper_type=None):
42         super(TrexRfcResourceHelper, self).__init__(setup_helper)
43
44         if rfc_helper_type is None:
45             rfc_helper_type = TrexRfc2544ResourceHelper
46
47         self.rfc2544_helper = rfc_helper_type(self.scenario_helper)
48
49     def _run_traffic_once(self, traffic_profile):
50         if self._terminated.value:
51             return
52
53         traffic_profile.execute_traffic(self)
54         self.client_started.value = 1
55         time.sleep(self.RUN_DURATION)
56         self.client.stop(traffic_profile.ports)
57         time.sleep(self.WAIT_TIME)
58         samples = traffic_profile.get_drop_percentage(self)
59         self._queue.put(samples)
60
61         if not self.rfc2544_helper.is_done():
62             return
63
64         self.client.stop(traffic_profile.ports)
65         self.client.reset(ports=traffic_profile.ports)
66         self.client.remove_all_streams(traffic_profile.ports)
67         traffic_profile.execute_traffic_latency(samples=samples)
68         multiplier = traffic_profile.calculate_pps(samples)[1]
69         for _ in range(5):
70             time.sleep(self.LATENCY_TIME_SLEEP)
71             self.client.stop(traffic_profile.ports)
72             time.sleep(self.WAIT_TIME)
73             last_res = self.client.get_stats(traffic_profile.ports)
74             if not isinstance(last_res, Mapping):
75                 self._terminated.value = 1
76                 continue
77             self.generate_samples(traffic_profile.ports, 'latency', {})
78             self._queue.put(samples)
79             self.client.start(mult=str(multiplier),
80                               ports=traffic_profile.ports,
81                               duration=120, force=True)
82
83     def start_client(self, ports, mult=None, duration=None, force=True):
84         self.client.start(ports=ports, mult=mult, duration=duration, force=force)
85
86     def clear_client_stats(self, ports):
87         self.client.clear_stats(ports=ports)
88
89     def collect_kpi(self):
90         self.rfc2544_helper.iteration.value += 1
91         return super(TrexRfcResourceHelper, self).collect_kpi()
92
93
94 class TrexTrafficGenRFC(TrexTrafficGen):
95     """
96     This class handles mapping traffic profile and generating
97     traffic for rfc2544 testcase.
98     """
99
100     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
101         if resource_helper_type is None:
102             resource_helper_type = TrexRfcResourceHelper
103
104         super(TrexTrafficGenRFC, self).__init__(name, vnfd, setup_env_helper_type,
105                                                 resource_helper_type)