Rename ResourceProfile.check_if_sa_running()
[yardstick.git] / yardstick / network_services / traffic_profile / prox_profile.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 """ Fixed traffic profile definitions """
15
16 from __future__ import absolute_import
17
18 import logging
19
20 from yardstick.network_services.traffic_profile.base import TrafficProfile
21 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxProfileHelper
22
23 LOG = logging.getLogger(__name__)
24
25
26 class ProxProfile(TrafficProfile):
27     """
28     This profile adds a single stream at the beginning of the traffic session
29     """
30
31     @staticmethod
32     def fill_samples(samples, traffic_gen):
33         for vpci_idx, intf in enumerate(traffic_gen.vpci_if_name_ascending):
34             name = intf[1]
35             # TODO: VNFDs KPIs values needs to be mapped to TRex structure
36             xe_port = traffic_gen.resource_helper.sut.port_stats([vpci_idx])
37             samples[name] = {
38                 "in_packets": xe_port[6],
39                 "out_packets": xe_port[7],
40             }
41
42     def __init__(self, tp_config):
43         super(ProxProfile, self).__init__(tp_config)
44         self.queue = None
45         self.done = False
46         self.results = []
47
48         # TODO: get init values from tp_config
49         self.prox_config = tp_config["traffic_profile"]
50         self.pkt_sizes = [int(x) for x in self.prox_config.get("packet_sizes", [])]
51         self.pkt_size_iterator = iter(self.pkt_sizes)
52         self.duration = int(self.prox_config.get("duration", 5))
53         self.precision = float(self.prox_config.get('test_precision', 1.0))
54         self.tolerated_loss = float(self.prox_config.get('tolerated_loss', 0.0))
55
56         # TODO: is this ever a function of packet size?
57         self.lower_bound = float(self.prox_config.get('lower_bound', 10.0))
58         self.upper_bound = float(self.prox_config.get('upper_bound', 100.0))
59         self.step_value = float(self.prox_config.get('step_value', 10.0))
60         self._profile_helper = None
61
62     def make_profile_helper(self, traffic_gen):
63         if self._profile_helper is None:
64             self._profile_helper = ProxProfileHelper.make_profile_helper(traffic_gen)
65         return self._profile_helper
66
67     def init(self, queue):
68         self.pkt_size_iterator = iter(self.pkt_sizes)
69         self.queue = queue
70         self.queue.cancel_join_thread()
71
72     def bounds_iterator(self, logger=None):
73         if logger:
74             logger.debug("Interval [%s, %s), step: %d", self.lower_bound,
75                          self.upper_bound, self.step_value)
76
77         test_value = self.lower_bound
78         while test_value <= self.upper_bound:
79             if logger:
80                 logger.info("Testing with value %s", test_value)
81             yield test_value
82             test_value += self.step_value
83
84     @property
85     def min_pkt_size(self):
86         """Return the minimum required packet size for the test.
87
88         Defaults to 64. Individual test must override this method if they have
89         other requirements.
90
91         Returns:
92             int. The minimum required packet size for the test.
93         """
94         return 64
95
96     def run_test_with_pkt_size(self, traffic_generator, pkt_size, duration):
97         raise NotImplementedError
98
99     def execute_traffic(self, traffic_generator):
100         self.make_profile_helper(traffic_generator)
101
102         try:
103             pkt_size = next(self.pkt_size_iterator)
104         except StopIteration:
105             self.done = True
106             return
107
108         # Adjust packet size upwards if it's less than the minimum
109         # required packet size for the test.
110         if pkt_size < self.min_pkt_size:
111             pkt_size += self.min_pkt_size - 64
112
113         duration = self.duration
114         self.run_test_with_pkt_size(traffic_generator, pkt_size, duration)