summaryrefslogtreecommitdiffstats
path: root/VNFs/DPPD-PROX/helper-scripts/rapid/rapid_test.py
blob: 261c8bb18bbc601fa77a96402bd87d2e8595f879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/python

##
## Copyright (c) 2010-2020 Intel Corporation
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##

import time
from past.utils import old_div
from rapid_log import RapidLog
from rapid_log import bcolors

class RapidTest(object):
    """
    Class to manage the flowsizetesting
    """
    @staticmethod
    def get_percentageof10Gbps(pps_speed,size):
        # speed is given in pps, returning % of 10Gb/s
        return (pps_speed / 1000000.0 * 0.08 * (size+28))

    @staticmethod
    def get_pps(speed,size):
        # speed is given in % of 10Gb/s, returning Mpps
        return (speed * 100.0 / (8*(size+28)))

    @staticmethod
    def get_speed(packet_speed,size):
        # return speed in Gb/s
        return (packet_speed / 1000.0 * (8*(size+28)))

    @staticmethod
    def set_background_flows(background_machines, number_of_flows):
        for machine in background_machines:
            machine.set_flows(number_of_flows)

    @staticmethod
    def set_background_speed(background_machines, speed):
        for machine in background_machines:
            machine.set_generator_speed(speed)

    @staticmethod
    def set_background_size(background_machines, size):
        for machine in background_machines:
            machine.set_udp_packet_size(size)

    @staticmethod
    def start_background_traffic(background_machines):
        for machine in background_machines:
            machine.start()

    @staticmethod
    def stop_background_traffic(background_machines):
        for machine in background_machines:
            machine.stop()

    @staticmethod
    def report_result(flow_number, size, speed, pps_req_tx, pps_tx, pps_sut_tx,
        pps_rx, lat_avg, lat_perc, lat_perc_max, lat_max, tx, rx, tot_drop,
        elapsed_time,speed_prefix='', lat_avg_prefix='', lat_perc_prefix='',
        lat_max_prefix='', abs_drop_rate_prefix='', drop_rate_prefix=''):
        if flow_number < 0:
            flow_number_str = '| ({:>4}) |'.format(abs(flow_number))
        else:
            flow_number_str = '|{:>7} |'.format(flow_number)
        if pps_req_tx is None:
            pps_req_tx_str = '{0: >14}'.format('   NA     |')
        else:
            pps_req_tx_str = '{:>7.3f} Mpps |'.format(pps_req_tx)
        if pps_tx is None:
            pps_tx_str = '{0: >14}'.format('   NA     |')
        else:
            pps_tx_str = '{:>7.3f} Mpps |'.format(pps_tx) 
        if pps_sut_tx is None:
            pps_sut_tx_str = '{0: >14}'.format('   NA     |')
        else:
            pps_sut_tx_str = '{:>7.3f} Mpps |'.format(pps_sut_tx)
        if pps_rx is None:
            pps_rx_str = '{0: >25}'.format('NA        |')
        else:
            pps_rx_str = bcolors.OKBLUE + '{:>4.1f} Gb/s |{:7.3f} Mpps {}|'.format(RapidTest.get_speed(pps_rx,size),pps_rx,bcolors.ENDC)
        if tot_drop is None:
            tot_drop_str = ' |       NA  | '
        else:
            tot_drop_str = ' | {:>9.0f} | '.format(tot_drop)
        if lat_perc is None:
            lat_perc_str = ' |{:^10.10}|'.format('NA')
        elif lat_perc_max == True:
            lat_perc_str = '|>{}{:>5.0f} us{} |'.format(lat_perc_prefix,float(lat_perc), bcolors.ENDC) 
        else:
            lat_perc_str = '| {}{:>5.0f} us{} |'.format(lat_perc_prefix,float(lat_perc), bcolors.ENDC) 
        if elapsed_time is None:
            elapsed_time_str = ' NA |'
        else:
            elapsed_time_str = '{:>3.0f} |'.format(elapsed_time)
        return(flow_number_str + '{:>5.1f}'.format(speed) + '% '+speed_prefix +'{:>6.3f}'.format(RapidTest.get_pps(speed,size)) + ' Mpps|'+ pps_req_tx_str + pps_tx_str + bcolors.ENDC + pps_sut_tx_str + pps_rx_str +lat_avg_prefix+ ' {:>6.0f}'.format(lat_avg)+' us'+lat_perc_str+lat_max_prefix+'{:>6.0f}'.format(lat_max)+' us | ' + '{:>9.0f}'.format(tx) + ' | {:>9.0f}'.format(rx) + ' | '+ abs_drop_rate_prefix+ '{:>9.0f}'.format(tx-rx) + tot_drop_str +drop_rate_prefix+ '{:>5.2f}'.format(old_div(float(tx-rx),tx))  +bcolors.ENDC+' |' + elapsed_time_str)
            
    def run_iteration(self, requested_duration, flow_number, size, speed):
        BUCKET_SIZE_EXP = self.gen_machine.bucket_size_exp
        LAT_PERCENTILE = self.test['lat_percentile']
        r = 0;
        sleep_time = 2
        while (r < self.test['maxr']):
            time.sleep(sleep_time)
            # Sleep_time is needed to be able to do accurate measurements to check for packet loss. We need to make this time large enough so that we do not take the first measurement while some packets from the previous tests migth still be in flight
            t1_rx, t1_non_dp_rx, t1_tx, t1_non_dp_tx, t1_drop, t1_tx_fail, t1_tsc, abs_tsc_hz = self.gen_machine.core_stats()
            t1_dp_rx = t1_rx - t1_non_dp_rx
            t1_dp_tx = t1_tx - t1_non_dp_tx
            self.gen_machine.start_gen_cores()
            time.sleep(2) ## Needs to be 2 seconds since this 1 sec is the time that PROX uses to refresh the stats. Note that this can be changed in PROX!! Don't do it.
            if self.sut_machine!= None:
                t2_sut_rx, t2_sut_non_dp_rx, t2_sut_tx, t2_sut_non_dp_tx, t2_sut_drop, t2_sut_tx_fail, t2_sut_tsc, sut_tsc_hz = self.sut_machine.core_stats()
            t2_rx, t2_non_dp_rx, t2_tx, t2_non_dp_tx, t2_drop, t2_tx_fail, t2_tsc, tsc_hz = self.gen_machine.core_stats()
            tx = t2_tx - t1_tx
            dp_tx =  tx - (t2_non_dp_tx - t1_non_dp_tx )
            dp_rx =  t2_rx - t1_rx - (t2_non_dp_rx - t1_non_dp_rx) 
            tot_dp_drop = dp_tx - dp_rx
            if tx == 0:
                RapidLog.critical("TX = 0. Test interrupted since no packet has been sent.")
            if dp_tx == 0:
                RapidLog.critical("Only non-dataplane packets (e.g. ARP) sent. Test interrupted since no packet has been sent.")
            # Ask PROX to calibrate the bucket size once we have a PROX function to do this.
            # Measure latency statistics per second
            lat_min, lat_max, lat_avg, used_avg, t2_lat_tsc, lat_hz, buckets = self.gen_machine.lat_stats()
            lat_samples = sum(buckets)
            sample_count = 0
            for sample_percentile, bucket in enumerate(buckets,start=1):
                sample_count += bucket
                if sample_count > (lat_samples * LAT_PERCENTILE):
                    break
            percentile_max = (sample_percentile == len(buckets))
            sample_percentile = sample_percentile *  float(2 ** BUCKET_SIZE_EXP) / (old_div(float(lat_hz),float(10**6)))
            if self.test['test'] == 'fixed_rate':
                RapidLog.info(self.report_result(flow_number,size,speed,None,None,None,None,lat_avg,sample_percentile,percentile_max,lat_max, dp_tx, dp_rx , None, None))
            tot_rx = tot_non_dp_rx = tot_tx = tot_non_dp_tx = tot_drop = 0
            lat_avg = used_avg = 0
            buckets_total = [0] * 128
            tot_lat_samples = 0
            tot_lat_measurement_duration = float(0)
            tot_core_measurement_duration = float(0)
            tot_sut_core_measurement_duration = float(0)
            tot_sut_rx = tot_sut_non_dp_rx = tot_sut_tx = tot_sut_non_dp_tx = tot_sut_drop = tot_sut_tx_fail = tot_sut_tsc = 0
            lat_avail = core_avail = sut_avail = False
            while (tot_core_measurement_duration - float(requested_duration) <= 0.1) or (tot_lat_measurement_duration - float(requested_duration) <= 0.1):
                time.sleep(0.5)
                lat_min_sample, lat_max_sample, lat_avg_sample, used_sample, t3_lat_tsc, lat_hz, buckets = self.gen_machine.lat_stats()
                # Get statistics after some execution time
                if t3_lat_tsc != t2_lat_tsc:
                    single_lat_measurement_duration = (t3_lat_tsc - t2_lat_tsc) * 1.0 / lat_hz  # time difference between the 2 measurements, expressed in seconds.
                    # A second has passed in between to lat_stats requests. Hence we need to process the results
                    tot_lat_measurement_duration = tot_lat_measurement_duration + single_lat_measurement_duration
                    if lat_min > lat_min_sample:
                        lat_min = lat_min_sample
                    if lat_max < lat_max_sample:
                        lat_max = lat_max_sample
                    lat_avg = lat_avg + lat_avg_sample * single_lat_measurement_duration # Sometimes, There is more than 1 second between 2 lat_stats. Hence we will take the latest measurement
                    used_avg = used_avg + used_sample * single_lat_measurement_duration  # and give it more weigth.
                    lat_samples = sum(buckets)
                    tot_lat_samples += lat_samples
                    sample_count = 0
                    for sample_percentile, bucket in enumerate(buckets,start=1):
                        sample_count += bucket
                        if sample_count > lat_samples * LAT_PERCENTILE:
                            break
                    percentile_max = (sample_percentile == len(buckets))
                    sample_percentile = sample_percentile *  float(2 ** BUCKET_SIZE_EXP) / (old_div(float(lat_hz),float(10**6)))
                    buckets_total = [buckets_total[i] + buckets[i] for i in range(len(buckets_total))] 
                    t2_lat_tsc = t3_lat_tsc
                    lat_avail = True
                t3_rx, t3_non_dp_rx, t3_tx, t3_non_dp_tx, t3_drop, t3_tx_fail, t3_tsc, tsc_hz = self.gen_machine.core_stats()
                if t3_tsc != t2_tsc:
                    single_core_measurement_duration = (t3_tsc - t2_tsc) * 1.0 / tsc_hz  # time difference between the 2 measurements, expressed in seconds.
                    tot_core_measurement_duration = tot_core_measurement_duration + single_core_measurement_duration
                    delta_rx = t3_rx - t2_rx
                    tot_rx += delta_rx
                    delta_non_dp_rx = t3_non_dp_rx - t2_non_dp_rx
                    tot_non_dp_rx += delta_non_dp_rx
                    delta_tx = t3_tx - t2_tx
                    tot_tx += delta_tx
                    delta_non_dp_tx = t3_non_dp_tx - t2_non_dp_tx
                    tot_non_dp_tx += delta_non_dp_tx
                    delta_dp_tx = delta_tx -delta_non_dp_tx
                    delta_dp_rx = delta_rx -delta_non_dp_rx
                    delta_dp_drop = delta_dp_tx - delta_dp_rx
                    tot_dp_drop += delta_dp_drop
                    delta_drop = t3_drop - t2_drop
                    tot_drop += delta_drop
                    t2_rx, t2_non_dp_rx, t2_tx, t2_non_dp_tx, t2_drop, t2_tx_fail, t2_tsc = t3_rx, t3_non_dp_rx, t3_tx, t3_non_dp_tx, t3_drop, t3_tx_fail, t3_tsc
                    core_avail = True
                if self.sut_machine!=None:
                    t3_sut_rx, t3_sut_non_dp_rx, t3_sut_tx, t3_sut_non_dp_tx, t3_sut_drop, t3_sut_tx_fail, t3_sut_tsc, sut_tsc_hz = self.sut_machine.core_stats()
                    if t3_sut_tsc != t2_sut_tsc:
                        single_sut_core_measurement_duration = (t3_sut_tsc - t2_sut_tsc) * 1.0 / tsc_hz  # time difference between the 2 measurements, expressed in seconds.
                        tot_sut_core_measurement_duration = tot_sut_core_measurement_duration + single_sut_core_measurement_duration
                        tot_sut_rx += t3_sut_rx - t2_sut_rx
                        tot_sut_non_dp_rx += t3_sut_non_dp_rx - t2_sut_non_dp_rx
                        delta_sut_tx = t3_sut_tx - t2_sut_tx
                        tot_sut_tx += delta_sut_tx
                        delta_sut_non_dp_tx = t3_sut_non_dp_tx - t2_sut_non_dp_tx
                        tot_sut_non_dp_tx += delta_sut_non_dp_tx 
                        t2_sut_rx, t2_sut_non_dp_rx, t2_sut_tx, t2_sut_non_dp_tx, t2_sut_drop, t2_sut_tx_fail, t2_sut_tsc = t3_sut_rx, t3_sut_non_dp_rx, t3_sut_tx, t3_sut_non_dp_tx, t3_sut_drop, t3_sut_tx_fail, t3_sut_tsc
                        sut_avail = True
                if self.test['test'] == 'fixed_rate':
                    if lat_avail == core_avail == True:
                        lat_avail = core_avail = False
                        pps_req_tx = (delta_tx + delta_drop - delta_rx)/single_core_measurement_duration/1000000
                        pps_tx = delta_tx/single_core_measurement_duration/1000000
                        if self.sut_machine != None and sut_avail:
                            pps_sut_tx = delta_sut_tx/single_sut_core_measurement_duration/1000000
                            sut_avail = False
                        else:
                            pps_sut_tx = None
                        pps_rx = delta_rx/single_core_measurement_duration/1000000
                        RapidLog.info(self.report_result(flow_number, size,
                            speed, pps_req_tx, pps_tx, pps_sut_tx, pps_rx,
                            lat_avg_sample, sample_percentile, percentile_max,
                            lat_max_sample, delta_dp_tx, delta_dp_rx,
                            tot_dp_drop, single_core_measurement_duration))
            #Stop generating
            self.gen_machine.stop_gen_cores()
            r += 1
            lat_avg = old_div(lat_avg, float(tot_lat_measurement_duration))
            used_avg = old_div(used_avg, float(tot_lat_measurement_duration))
            t4_tsc = t2_tsc
            while t4_tsc == t2_tsc:
                t4_rx, t4_non_dp_rx, t4_tx, t4_non_dp_tx, t4_drop, t4_tx_fail, t4_tsc, abs_tsc_hz = self.gen_machine.core_stats()
            if self.test['test'] == 'fixed_rate':
                t4_lat_tsc = t2_lat_tsc
                while t4_lat_tsc == t2_lat_tsc:
                    lat_min_sample, lat_max_sample, lat_avg_sample, used_sample, t4_lat_tsc, lat_hz, buckets = self.gen_machine.lat_stats()
                sample_count = 0
                lat_samples = sum(buckets)
                for percentile, bucket in enumerate(buckets,start=1):
                    sample_count += bucket
                    if sample_count > lat_samples * LAT_PERCENTILE:
                        break
                percentile_max = (percentile == len(buckets))
                percentile = percentile *  float(2 ** BUCKET_SIZE_EXP) / (old_div(float(lat_hz),float(10**6)))
                lat_max = lat_max_sample
                lat_avg = lat_avg_sample
                delta_rx = t4_rx - t2_rx
                delta_non_dp_rx = t4_non_dp_rx - t2_non_dp_rx
                delta_tx = t4_tx - t2_tx
                delta_non_dp_tx = t4_non_dp_tx - t2_non_dp_tx
                delta_dp_tx = delta_tx -delta_non_dp_tx
                delta_dp_rx = delta_rx -delta_non_dp_rx
                dp_tx = delta_dp_tx
                dp_rx = delta_dp_rx
                tot_dp_drop += delta_dp_tx - delta_dp_rx
                pps_req_tx = None
                pps_tx = None
                pps_sut_tx = None
                pps_rx = None
                drop_rate = 100.0*(dp_tx-dp_rx)/dp_tx
                tot_core_measurement_duration = None
                break ## Not really needed since the while loop will stop when evaluating the value of r
            else:
                sample_count = 0
                for percentile, bucket in enumerate(buckets_total,start=1):
                    sample_count += bucket
                    if sample_count > tot_lat_samples * LAT_PERCENTILE:
                        break
                percentile_max = (percentile == len(buckets_total))
                percentile = percentile *  float(2 ** BUCKET_SIZE_EXP) / (old_div(float(lat_hz),float(10**6)))
                pps_req_tx = (tot_tx + tot_drop - tot_rx)/tot_core_measurement_duration/1000000.0 # tot_drop is all packets dropped by all tasks. This includes packets dropped at the generator task + packets dropped by the nop task. In steady state, this equals to the number of packets received by this VM
                pps_tx = tot_tx/tot_core_measurement_duration/1000000.0 # tot_tx is all generated packets actually accepted by the interface
                pps_rx = tot_rx/tot_core_measurement_duration/1000000.0 # tot_rx is all packets received by the nop task = all packets received in the gen VM
                if self.sut_machine != None and sut_avail:
                    pps_sut_tx = tot_sut_tx / tot_sut_core_measurement_duration / 1000000.0
                else:
                    pps_sut_tx = None
                dp_tx = (t4_tx - t1_tx) - (t4_non_dp_tx - t1_non_dp_tx)
                dp_rx = (t4_rx - t1_rx) - (t4_non_dp_rx - t1_non_dp_rx)
                tot_dp_drop = dp_tx - dp_rx
                drop_rate = 100.0*tot_dp_drop/dp_tx
                if ((drop_rate < self.test['drop_rate_threshold']) or (tot_dp_drop == self.test['drop_rate_threshold'] ==0) or (tot_dp_drop > self.test['maxz'])):
                    break
        return(pps_req_tx,pps_tx,pps_sut_tx,pps_rx,lat_avg,percentile,percentile_max,lat_max,dp_tx,dp_rx,tot_dp_drop,(t4_tx_fail - t1_tx_fail),drop_rate,lat_min,used_avg,r,tot_core_measurement_duration)