fix regex expression to find IPV4 address
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_impairtest.py
1 #!/usr/bin/python
2
3 ##
4 ## Copyright (c) 2020 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##
11 ##     http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19
20 import sys
21 import time
22 import requests
23 from rapid_log import RapidLog
24 from rapid_log import bcolors
25 from rapid_test import RapidTest
26 from statistics import mean
27
28 class ImpairTest(RapidTest):
29     """
30     Class to manage the impair testing
31     """
32     def __init__(self, test_param, lat_percentile, runtime, testname,
33             environment_file, gen_machine, sut_machine, background_machines):
34         super().__init__(test_param, runtime, testname, environment_file)
35         self.gen_machine = gen_machine
36         self.sut_machine = sut_machine
37         self.background_machines = background_machines
38         self.test['lat_percentile'] = lat_percentile
39
40     def run(self):
41         result_details = {'Details': 'Nothing'}
42         imix = self.test['imix']
43         size = mean (imix)
44         flow_number = self.test['flowsize']
45         attempts = self.test['steps']
46         self.gen_machine.set_udp_packet_size(imix)
47         flow_number = self.gen_machine.set_flows(flow_number)
48         self.gen_machine.start_latency_cores()
49         RapidLog.info('+' + '-' * 188 + '+')
50         RapidLog.info(("| Generator is sending UDP ({:>5} flow) packets ({:>5}"
51             " bytes) to SUT via GW dropping and delaying packets. SUT sends "
52             "packets back.{:>60}").format(flow_number,round(size),'|'))
53         RapidLog.info('+' + '-' * 8 + '+' + '-' * 18 + '+' + '-' * 13 +
54             '+' + '-' * 13 + '+' + '-' * 13 + '+' + '-' * 24 + '+' +
55             '-' * 10 + '+' + '-' * 10 + '+' + '-' * 10 + '+' + '-' * 11
56             + '+' + '-' * 11 + '+' + '-' * 11 + '+'  + '-' * 11 +  '+'
57             + '-' * 7 + '+' + '-' * 4 + '+')
58         RapidLog.info(('| Test   | Speed requested  | Gen by core | Sent by NIC'
59             ' | Fwrd by SUT | Rec. by core           | Avg. Lat.|{:.0f} Pcentil'
60             '| Max. Lat.|   Sent    |  Received |    Lost   | Total Lost|'
61             'L.Ratio|Time|').format(self.test['lat_percentile']*100))
62         RapidLog.info('+' + '-' * 8 + '+' + '-' * 18 + '+' + '-' * 13 +
63             '+' + '-' * 13 + '+' + '-' * 13 + '+' + '-' * 24 + '+' +
64             '-' * 10 + '+' + '-' * 10 + '+' + '-' * 10 + '+' + '-' * 11
65             + '+' + '-' * 11 + '+' + '-' * 11 + '+'  + '-' * 11 +  '+'
66             + '-' * 7 + '+' + '-' * 4 + '+')
67         speed = self.test['startspeed']
68         self.gen_machine.set_generator_speed(speed)
69         while attempts:
70             attempts -= 1
71             print('Measurement ongoing at speed: ' + str(round(speed,2)) + '%      ',end='\r')
72             sys.stdout.flush()
73             time.sleep(1)
74             # Get statistics now that the generation is stable and NO ARP messages any more
75             iteration_data = self.run_iteration(float(self.test['runtime']),flow_number,size,speed)
76             iteration_data['speed'] = speed
77             # Drop rate is expressed in percentage. lat_used is a ratio (0 to 1). The sum of these 2 should be 100%.
78             # If the sum is lower than 95, it means that more than 5% of the latency measurements where dropped for accuracy reasons.
79             if (iteration_data['drop_rate'] +
80                     iteration_data['lat_used'] * 100) < 95:
81                 lat_warning = ('{} Latency accuracy issue?: {:>3.0f}%'
82                     '{}').format(bcolors.WARNING,
83                             iteration_data['lat_used']*100, bcolors.ENDC)
84             else:
85                 lat_warning = ''
86             iteration_prefix = {'speed' : '',
87                     'lat_avg' : '',
88                     'lat_perc' : '',
89                     'lat_max' : '',
90                     'abs_drop_rate' : '',
91                     'drop_rate' : ''}
92             RapidLog.info(self.report_result(attempts, size, iteration_data,
93                 iteration_prefix))
94             iteration_data['test'] = self.test['testname']
95             iteration_data['environment_file'] = self.test['environment_file']
96             iteration_data['Flows'] = flow_number
97             iteration_data['Size'] = size
98             iteration_data['RequestedSpeed'] = RapidTest.get_pps(
99                     iteration_data['speed'] ,size)
100             result_details = self.post_data(iteration_data)
101             RapidLog.debug(result_details)
102         RapidLog.info('+' + '-' * 8 + '+' + '-' * 18 + '+' + '-' * 13 +
103             '+' + '-' * 13 + '+' + '-' * 13 + '+' + '-' * 24 + '+' +
104             '-' * 10 + '+' + '-' * 10 + '+' + '-' * 10 + '+' + '-' * 11
105             + '+' + '-' * 11 + '+' + '-' * 11 + '+'  + '-' * 11 +  '+'
106             + '-' * 7 + '+' + '-' * 4 + '+')
107         self.gen_machine.stop_latency_cores()
108         return (True, result_details)