add yardstick iruya 9.0.0 release notes
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_rfc2544_trex.py
1 # Copyright (c) 2016-2019 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
15 import logging
16 import time
17
18 from six import moves
19 from yardstick.common import utils
20 from yardstick.network_services.vnf_generic.vnf import sample_vnf
21 from yardstick.network_services.vnf_generic.vnf import tg_trex
22 from trex_stl_lib.trex_stl_exceptions import STLError
23
24
25 LOG = logging.getLogger(__name__)
26
27
28 class TrexRfcResourceHelper(tg_trex.TrexResourceHelper):
29
30     SAMPLING_PERIOD = 2
31     TRANSIENT_PERIOD = 10
32
33     def __init__(self, setup_helper):
34         super(TrexRfcResourceHelper, self).__init__(setup_helper)
35         self.rfc2544_helper = sample_vnf.Rfc2544ResourceHelper(
36             self.scenario_helper)
37
38     def _run_traffic_once(self, traffic_profile):
39         self.client_started.value = 1
40         ports, port_pg_id = traffic_profile.execute_traffic(self)
41
42         samples = []
43         timeout = int(traffic_profile.config.duration) - self.TRANSIENT_PERIOD
44         time.sleep(self.TRANSIENT_PERIOD)
45         for _ in utils.Timer(timeout=timeout):
46             samples.append(self._get_samples(ports, port_pg_id=port_pg_id))
47             time.sleep(self.SAMPLING_PERIOD)
48
49         traffic_profile.stop_traffic(self)
50         completed, output = traffic_profile.get_drop_percentage(
51             samples, self.rfc2544_helper.tolerance_low,
52             self.rfc2544_helper.tolerance_high,
53             self.rfc2544_helper.correlated_traffic,
54             self.rfc2544_helper.resolution)
55         self._queue.put(output)
56         return completed
57
58     def start_client(self, ports, mult=None, duration=None, force=True):
59         self.client.start(ports=ports, mult=mult, duration=duration, force=force)
60
61     def clear_client_stats(self, ports):
62         self.client.clear_stats(ports=ports)
63
64     def run_test(self, traffic_profile, tasks_queue, results_queue, *args): # pragma: no cover
65         LOG.debug("Trex resource_helper run_test")
66         if self._terminated.value:
67             return
68         # if we don't do this we can hang waiting for the queue to drain
69         # have to do this in the subprocess
70         self._queue.cancel_join_thread()
71         try:
72             self._build_ports()
73             self.client = self._connect()
74             self.client.reset(ports=self.all_ports)
75             self.client.remove_all_streams(self.all_ports)  # remove all streams
76             traffic_profile.register_generator(self)
77
78             completed = False
79             self.rfc2544_helper.iteration.value = 0
80             self.client_started.value = 1
81             while completed is False and not self._terminated.value:
82                 LOG.debug("Wait for task ...")
83                 try:
84                     task = tasks_queue.get(True, 5)
85                 except moves.queue.Empty:
86                     LOG.debug("Wait for task timeout, continue waiting...")
87                     continue
88                 else:
89                     if task != 'RUN_TRAFFIC':
90                         continue
91                 self.rfc2544_helper.iteration.value += 1
92                 LOG.info("Got %s task, start iteration %d", task,
93                          self.rfc2544_helper.iteration.value)
94                 completed = self._run_traffic_once(traffic_profile)
95                 if completed:
96                     LOG.debug("%s::run_test - test completed",
97                               self.__class__.__name__)
98                     results_queue.put('COMPLETE')
99                 else:
100                     results_queue.put('CONTINUE')
101                 tasks_queue.task_done()
102
103             self.client.stop(self.all_ports)
104             self.client.disconnect()
105             self._terminated.value = 0
106         except STLError:
107             if self._terminated.value:
108                 LOG.debug("traffic generator is stopped")
109                 return  # return if trex/tg server is stopped.
110             raise
111
112         self.client_started.value = 0
113         LOG.debug("%s::run_test done", self.__class__.__name__)
114
115 class TrexTrafficGenRFC(tg_trex.TrexTrafficGen):
116     """
117     This class handles mapping traffic profile and generating
118     traffic for rfc2544 testcase.
119     """
120
121     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
122         if resource_helper_type is None:
123             resource_helper_type = TrexRfcResourceHelper
124
125         super(TrexTrafficGenRFC, self).__init__(name, vnfd, setup_env_helper_type,
126                                                 resource_helper_type)