Add support to the test case required by YARDSTICK-35
[yardstick.git] / yardstick / vTC / apexlake / experimental_framework / benchmarks / rfc2544_throughput_benchmark.py
1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
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 from experimental_framework.benchmarks import benchmark_base_class
16 from experimental_framework.packet_generators \
17     import dpdk_packet_generator as dpdk
18 import experimental_framework.common as common
19 from experimental_framework.constants import framework_parameters as fp
20
21
22 PACKET_SIZE = 'packet_size'
23 VLAN_SENDER = 'vlan_sender'
24 VLAN_RECEIVER = 'vlan_receiver'
25
26
27 class RFC2544ThroughputBenchmark(benchmark_base_class.BenchmarkBaseClass):
28     """
29     Calculates the throughput of the VNF under test according to the RFC2544.
30     """
31
32     def __init__(self, name, params):
33         benchmark_base_class.BenchmarkBaseClass.__init__(self, name, params)
34         self.base_dir = common.get_base_dir() + \
35             fp.EXPERIMENTAL_FRAMEWORK_DIR + fp.DPDK_PKTGEN_DIR
36         self.results_file = self.base_dir + 'experiment.res'
37         self.lua_file = self.base_dir + 'rfc2544.lua'
38
39     def init(self):
40         """
41         Initialize the benchmark
42         :return: None
43         """
44         pass
45
46     def finalize(self):
47         """
48         :return: None
49         """
50         pass
51
52     def get_features(self):
53         """
54         Returns the features associated to the benchmark
55         :return:
56         """
57         features = dict()
58         features['description'] = 'RFC 2544 Throughput calculation'
59         features['parameters'] = [PACKET_SIZE, VLAN_SENDER, VLAN_RECEIVER]
60         features['allowed_values'] = dict()
61         features['allowed_values'][PACKET_SIZE] = ['64', '128', '256', '512',
62                                                    '1024', '1280', '1514']
63         features['allowed_values'][VLAN_SENDER] = map(str, range(-1, 4096))
64         features['allowed_values'][VLAN_RECEIVER] = map(str, range(-1, 4096))
65         features['default_values'] = dict()
66         features['default_values'][PACKET_SIZE] = '1280'
67         features['default_values'][VLAN_SENDER] = '1007'
68         features['default_values'][VLAN_RECEIVER] = '1006'
69         return features
70
71     def run(self):
72         """
73         Sends and receive traffic according to the RFC methodology in order
74         to measure the throughput of the workload
75         :return: Results of the testcase (type: dict)
76         """
77         ret_val = dict()
78         packet_size = self._extract_packet_size_from_params()
79         ret_val[PACKET_SIZE] = packet_size
80
81         # Packetgen management
82         packetgen = dpdk.DpdkPacketGenerator()
83         self._configure_lua_file()
84         packetgen.init_dpdk_pktgen(dpdk_interfaces=2,
85                                    pcap_file_0='packet_' +
86                                                packet_size + '.pcap',
87                                    pcap_file_1='igmp.pcap',
88                                    lua_script='rfc2544.lua',
89                                    vlan_0=self.params[VLAN_SENDER],
90                                    vlan_1=self.params[VLAN_RECEIVER])
91         common.LOG.debug('Start the packet generator - packet size: ' +
92                          str(packet_size))
93         packetgen.send_traffic()
94         common.LOG.debug('Stop the packet generator')
95
96         # Result Collection
97         results = self._get_results()
98         for metric_name in results.keys():
99             ret_val[metric_name] = results[metric_name]
100         self._reset_lua_file()
101         return ret_val
102
103     def _extract_packet_size_from_params(self):
104         """
105         Extracts packet sizes from parameters
106         :return: packet_sizes (list)
107         """
108         packet_size = '1280'  # default value
109         if PACKET_SIZE in self.params.keys() and \
110                 isinstance(self.params[PACKET_SIZE], str):
111             packet_size = self.params[PACKET_SIZE]
112         return packet_size
113
114     def _configure_lua_file(self):
115         """
116         Configure the packet gen to write the results into the right file
117         :return: None
118         """
119         common.replace_in_file(self.lua_file, 'local out_file = ""',
120                                'local out_file = "' +
121                                self.results_file + '"')
122
123     def _reset_lua_file(self):
124         """
125         Sets back the configuration of the local file var to the default
126         :return:
127         """
128         common.replace_in_file(self.lua_file, 'local out_file = "' +
129                                self.results_file + '"',
130                                'local out_file = ""')
131
132     def _get_results(self):
133         """
134         Returns the results of the experiment
135         :return: None
136         """
137         throughput = common.get_file_first_line(self.results_file)
138         ret_val = dict()
139         try:
140             ret_val['throughput'] = int(throughput)
141         except:
142             ret_val['throughput'] = 0
143         return ret_val