295cb799a010154d2ff6d6046b10d6277cb6ded5
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_impairtest.py
1 #!/usr/bin/python
2
3 ##
4 ## Copyright (c) 2010-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 from rapid_log import RapidLog
23 from rapid_test import RapidTest
24
25 class ImpairTest(RapidTest):
26     """
27     Class to manage the impair testing
28     """
29     def __init__(self, test_param, lat_percentile, runtime, pushgateway,
30             environment_file, gen_machine):
31         self.test = test_param
32         self.gen_machine = gen_machine
33         self.sut_machine = sut_machine
34         self.test['lat_percentile'] = lat_percentile
35         self.test['runtime'] = runtime
36         self.test['pushgateway'] = pushgateway
37         self.test['environment_file'] = environment_file
38
39     def run(self):
40     #    fieldnames = ['Flows','PacketSize','RequestedPPS','GeneratedPPS','SentPPS','ForwardedPPS','ReceivedPPS','AvgLatencyUSEC','MaxLatencyUSEC','Dropped','DropRate']
41     #    writer = csv.DictWriter(data_csv_file, fieldnames=fieldnames)
42     #    writer.writeheader()
43         size = self.test['packetsize']
44         flow_number = self.test['flowsize']
45         RapidLog.info("+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+")
46         RapidLog.info("| Generator is sending UDP ("+'{:>5}'.format(flow_number)+" flow) packets ("+ '{:>5}'.format(size) +" bytes) to SUT via GW dropping and delaying packets. SUT sends packets back. Use ctrl-c to stop the test    |")
47         RapidLog.info("+--------+--------------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+------------+")
48         RapidLog.info("| Test   |  Speed requested   | Sent to NIC    |  Sent by Gen   | Forward by SUT |  Rec. by Gen   |  Avg. Latency  |  Max. Latency  |  Packets Lost  | Loss Ratio |")
49         RapidLog.info("+--------+--------------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+------------+")
50         attempts = 0
51         self.gen_machine.set_udp_packet_size(size)
52         self.gen_machine.set_flows(flow_number)
53         self.gen_machine.start_latency_cores()
54         speed = self.test['startspeed']
55         self.gen_machine.set_generator_speed(speed)
56         while True:
57             attempts += 1
58             print('Measurement ongoing at speed: ' + str(round(speed,2)) + '%      ',end='\r')
59             sys.stdout.flush()
60             time.sleep(1)
61             # Get statistics now that the generation is stable and NO ARP messages any more
62             pps_req_tx,pps_tx,pps_sut_tx_str,pps_rx,lat_avg, lat_perc, lat_perc_max, lat_max, abs_dropped, abs_tx_fail, abs_tx, lat_min, lat_used, r, actual_duration = run_iteration(float(self.test['runtime']),flow_number,size,speed)
63             drop_rate = 100.0*abs_dropped/abs_tx
64             if lat_used < 0.95:
65                 lat_warning = bcolors.FAIL + ' Potential latency accuracy problem: {:>3.0f}%'.format(lat_used*100) +  bcolors.ENDC
66             else:
67                 lat_warning = ''
68             RapidLog.info('|{:>7}'.format(str(attempts))+" | " + '{:>5.1f}'.format(speed) + '% ' +'{:>6.3f}'.format(get_pps(speed,size)) + ' Mpps | '+ '{:>9.3f}'.format(pps_req_tx)+' Mpps | '+ '{:>9.3f}'.format(pps_tx) +' Mpps | ' + '{:>9}'.format(pps_sut_tx_str) +' Mpps | '+ '{:>9.3f}'.format(pps_rx)+' Mpps | '+ '{:>9.0f}'.format(lat_avg)+' us   | '+ '{:>9.0f}'.format(lat_max)+' us   | '+ '{:>14d}'.format(abs_dropped)+ ' |''{:>9.2f}'.format(drop_rate)+ '%  |'+lat_warning)
69     #        writer.writerow({'Flows':flow_number,'PacketSize':(size+4),'RequestedPPS':get_pps(speed,size),'GeneratedPPS':pps_req_tx,'SentPPS':pps_tx,'ForwardedPPS':pps_sut_tx_str,'ReceivedPPS':pps_rx,'AvgLatencyUSEC':lat_avg,'MaxLatencyUSEC':lat_max,'Dropped':abs_dropped,'DropRate':drop_rate})
70             if self.test['pushgateway']:
71                 URL     = self.test['pushgateway'] + '/metrics/job/' + TestName + '/instance/' + self.test['environment_file'] 
72                 DATA = 'Flows {}\nPacketSize {}\nRequestedPPS {}\nGeneratedPPS {}\nSentPPS {}\nForwardedPPS {}\nReceivedPPS {}\nAvgLatencyUSEC {}\nMaxLatencyUSEC {}\nDropped {}\nDropRate {}\n'.format(flow_number,size+4,get_pps(speed,size),pps_req_tx,pps_tx,pps_sut_tx_str,pps_rx,lat_avg,lat_max,abs_dropped,drop_rate)
73                 HEADERS = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}
74                 response = requests.post(url=URL, data=DATA,headers=HEADERS)
75                 if (response.status_code != 202) and (response.status_code != 200):
76                     RapidLog.info('Cannot send metrics to {}'.format(URL))
77                     RapidLog.info(DATA)
78         self.gen_machine.stop_latency_cores()
79         return (True)