Merge "NSB NFVi PROX BNG and vPE losing many packets"
[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
15 import logging
16 import time
17
18 from yardstick.common import utils
19 from yardstick.network_services.vnf_generic.vnf import sample_vnf
20 from yardstick.network_services.vnf_generic.vnf import tg_trex
21
22
23 LOGGING = logging.getLogger(__name__)
24
25
26 class TrexRfcResourceHelper(tg_trex.TrexResourceHelper):
27
28     SAMPLING_PERIOD = 2
29     TRANSIENT_PERIOD = 10
30
31     def __init__(self, setup_helper):
32         super(TrexRfcResourceHelper, self).__init__(setup_helper)
33         self.rfc2544_helper = sample_vnf.Rfc2544ResourceHelper(
34             self.scenario_helper)
35
36     def _run_traffic_once(self, traffic_profile):
37         self.client_started.value = 1
38         ports, port_pg_id = traffic_profile.execute_traffic(self)
39
40         samples = []
41         timeout = int(traffic_profile.config.duration) - self.TRANSIENT_PERIOD
42         time.sleep(self.TRANSIENT_PERIOD)
43         for _ in utils.Timer(timeout=timeout):
44             samples.append(self._get_samples(ports, port_pg_id=port_pg_id))
45             time.sleep(self.SAMPLING_PERIOD)
46
47         traffic_profile.stop_traffic(self)
48         completed, output = traffic_profile.get_drop_percentage(
49             samples, self.rfc2544_helper.tolerance_low,
50             self.rfc2544_helper.tolerance_high,
51             self.rfc2544_helper.correlated_traffic)
52         self._queue.put(output)
53         return completed
54
55     def start_client(self, ports, mult=None, duration=None, force=True):
56         self.client.start(ports=ports, mult=mult, duration=duration, force=force)
57
58     def clear_client_stats(self, ports):
59         self.client.clear_stats(ports=ports)
60
61
62 class TrexTrafficGenRFC(tg_trex.TrexTrafficGen):
63     """
64     This class handles mapping traffic profile and generating
65     traffic for rfc2544 testcase.
66     """
67
68     def __init__(self, name, vnfd, task_id, setup_env_helper_type=None,
69                  resource_helper_type=None):
70         if resource_helper_type is None:
71             resource_helper_type = TrexRfcResourceHelper
72         super(TrexTrafficGenRFC, self).__init__(
73             name, vnfd, task_id, setup_env_helper_type, resource_helper_type)