Fix small things for integration of ApexLake with Yardstick
[yardstick.git] / yardstick / vTC / apexlake / experimental_framework / benchmarks / rfc2544_throughput_benchmark.py
1
2 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 from experimental_framework.benchmarks import benchmark_base_class
17 from experimental_framework.packet_generators \
18     import dpdk_packet_generator as dpdk
19 import experimental_framework.common as common
20 from experimental_framework.constants import framework_parameters as fp
21
22
23 PACKET_SIZE = 'packet_size'
24 VLAN_SENDER = 'vlan_sender'
25 VLAN_RECEIVER = 'vlan_receiver'
26
27
28 class RFC2544ThroughputBenchmark(benchmark_base_class.BenchmarkBaseClass):
29     """
30     Calculates the throughput of the VNF under test according to the RFC2544.
31     """
32
33     def __init__(self, name, params):
34         benchmark_base_class.BenchmarkBaseClass.__init__(self, name, params)
35         self.base_dir = common.get_base_dir() + \
36             fp.EXPERIMENTAL_FRAMEWORK_DIR + fp.DPDK_PKTGEN_DIR
37         self.results_file = self.base_dir + 'experiment.res'
38         self.lua_file = self.base_dir + 'rfc2544.lua'
39
40     def init(self):
41         """
42         Initialize the benchmark
43         :return: None
44         """
45         pass
46
47     def finalize(self):
48         """
49         :return: None
50         """
51         pass
52
53     def get_features(self):
54         """
55         Returns the features associated to the benchmark
56         :return:
57         """
58         features = dict()
59         features['description'] = 'RFC 2544 Throughput calculation'
60         features['parameters'] = [PACKET_SIZE, VLAN_SENDER, VLAN_RECEIVER]
61         features['allowed_values'] = dict()
62         features['allowed_values'][PACKET_SIZE] = ['64', '128', '256', '512',
63                                                    '1024', '1280', '1514']
64         features['allowed_values'][VLAN_SENDER] = map(str, range(-1, 4096))
65         features['allowed_values'][VLAN_RECEIVER] = map(str, range(-1, 4096))
66         features['default_values'] = dict()
67         features['default_values'][PACKET_SIZE] = '1280'
68         features['default_values'][VLAN_SENDER] = '1007'
69         features['default_values'][VLAN_RECEIVER] = '1006'
70         return features
71
72     def run(self):
73         """
74         Sends and receive traffic according to the RFC methodology in order
75         to measure the throughput of the workload
76         :return: Results of the testcase (type: dict)
77         """
78         packet_size = self._extract_packet_size_from_params()
79
80         # Packetgen management
81         packetgen = dpdk.DpdkPacketGenerator()
82         self._configure_lua_file()
83         packetgen.init_dpdk_pktgen(dpdk_interfaces=2,
84                                    pcap_file_0='packet_' +
85                                                packet_size + '.pcap',
86                                    pcap_file_1='igmp.pcap',
87                                    lua_script='rfc2544.lua',
88                                    vlan_0=self.params[VLAN_SENDER],
89                                    vlan_1=self.params[VLAN_RECEIVER])
90         common.LOG.debug('Start the packet generator - packet size: ' +
91                          str(packet_size))
92         packetgen.send_traffic()
93         common.LOG.debug('Stop the packet generator')
94
95         return self._get_results()
96
97     def _extract_packet_size_from_params(self):
98         """
99         Extracts packet sizes from parameters
100         :return: packet_sizes (list)
101         """
102         packet_size = '1280'  # default value
103         if PACKET_SIZE in self.params.keys() and \
104                 isinstance(self.params[PACKET_SIZE], str):
105             packet_size = self.params[PACKET_SIZE]
106         return packet_size
107
108     def _configure_lua_file(self):
109         """
110         Configure the packet gen to write the results into the right file
111         :return: None
112         """
113         common.replace_in_file(self.lua_file, 'local out_file = ""',
114                                'local out_file = "' +
115                                self.results_file + '"')
116
117     def _reset_lua_file(self):
118         """
119         Sets back the configuration of the local file var to the default
120         :return:
121         """
122         common.replace_in_file(self.lua_file, 'local out_file = "' +
123                                self.results_file + '"',
124                                'local out_file = ""')
125
126     def _get_results(self):
127         """
128         Returns the results of the experiment
129         :return: None
130         """
131         throughput = common.get_file_first_line(self.results_file)
132         ret_val = dict()
133         try:
134             ret_val['throughput'] = int(throughput)
135         except:
136             ret_val['throughput'] = 0
137         return ret_val