stl_benchmark_test.py revision 420216e5
1#!/router/bin/python
2from .stl_general_test import CStlGeneral_Test, CTRexScenario
3from trex_stl_lib.api import *
4import os, sys
5from collections import deque
6from time import time, sleep
7import pprint
8
9class STLBenchmark_Test(CStlGeneral_Test):
10    """Benchark stateless performance"""
11
12    def test_CPU_benchmark(self):
13        critical_test = CTRexScenario.setup_name in ('kiwi02', 'trex08', 'trex09') # temporary patch, this test needs to be fixed
14        timeout       = 60 # max time to wait for stabilization
15        stabilize     = 5  # ensure stabilization over this period
16        print('')
17
18        #self.get_benchmark_param('profiles')
19        #profiles=[{'bw_per_core': 1,
20        #  'cpu_util': 1,
21        #  'kwargs': {'packet_len': 64},
22        #  'name': 'stl/udp_for_benchmarks.py'}]
23
24        profiles = self.get_benchmark_param('profiles')
25        dp_cores     = self.stl_trex.system_info.get('dp_core_count', 0)
26
27        for profile_bench in profiles:
28
29            cpu_utils    = deque([0] * stabilize, maxlen = stabilize)
30            bps          = deque([0] * stabilize, maxlen = stabilize)
31            pps          = deque([0] * stabilize, maxlen = stabilize)
32
33            kwargs = profile_bench.get('kwargs', {})
34            print('Testing profile %s, kwargs: %s' % (profile_bench['name'], kwargs))
35            profile = STLProfile.load(os.path.join(CTRexScenario.scripts_path, profile_bench['name']), **kwargs)
36
37            self.stl_trex.reset()
38            self.stl_trex.clear_stats()
39            sleep(1)
40            self.stl_trex.add_streams(profile)
41            mult = '1%' if self.is_virt_nics else '10%'
42            self.stl_trex.start(mult = mult)
43            start_time = time()
44
45            for i in range(timeout + 1):
46                stats = self.stl_trex.get_stats()
47                cpu_utils.append(stats['global']['cpu_util'])
48                bps.append(stats['global']['tx_bps'])
49                pps.append(stats['global']['tx_pps'])
50
51                if i > stabilize and min(cpu_utils) > max(cpu_utils) * 0.95:
52                    break
53                sleep(0.5)
54
55            agv_cpu_util    = sum(cpu_utils) / stabilize
56            agv_pps         = sum(pps) / stabilize
57            agv_bps         = sum(bps) / stabilize
58
59            if agv_cpu_util == 0.0:
60                agv_cpu_util=1.0;
61
62            agv_mpps         = (agv_pps/1e6);
63            agv_gbps         = (agv_bps/1e9)
64
65
66            agv_gbps_norm    = agv_gbps * 100.0/agv_cpu_util;
67            agv_mpps_norm    = agv_mpps * 100.0/agv_cpu_util;
68
69            agv_gbps_norm_pc    = agv_gbps_norm/dp_cores;
70            agv_mpps_norm_pc    = agv_mpps_norm/dp_cores;
71
72
73            if critical_test and i == timeout and agv_cpu_util > 10:
74                raise Exception('Timeout on waiting for stabilization, last CPU util values: %s' % list(cpu_utils))
75            if stats[0]['opackets'] < 300 or stats[1]['opackets'] < 300:
76                raise Exception('Too few opackets, port0: %s, port1: %s' % (stats[0]['opackets'], stats[1]['opackets']))
77            if stats['global']['queue_full'] > 100000:
78                raise Exception('Too much queue_full: %s' % stats['global']['queue_full'])
79            if not cpu_utils[-1]:
80                raise Exception('CPU util is zero, last values: %s' % list(cpu_utils))
81            print('Done (%ss), CPU util: %4g, norm_pps_per_core:%6smpps norm_bw_per_core: %6sGb/core' % (int(time() - start_time), agv_cpu_util, round(agv_mpps_norm_pc,2), round(agv_gbps_norm_pc, 2)))
82
83            # report benchmarks to elk
84            if self.elk:
85                streams=kwargs.get('stream_count',1)
86                elk_obj = self.get_elk_obj()
87                print("\n* Reporting to elk *\n")
88                name=profile_bench['name']
89                elk_obj['test']={ "name" : name,
90                            "type"  : "stateless-range",
91                            "cores" : dp_cores,
92                            "cpu%"  : agv_cpu_util,
93                            "mpps" :  (agv_mpps),
94                            "streams_count" :streams,
95                            "mpps_pc" :  (agv_mpps_norm_pc),
96                            "gbps_pc" :  (agv_gbps_norm_pc),
97                            "gbps" :  (agv_gbps),
98                            "avg-pktsize" : round((1000.0*agv_gbps/(8.0*agv_mpps))),
99                            "latecny" : { "min" : -1.0,
100                                          "max" : -1.0,
101                                          "avr" : -1.0
102                                         }
103                    };
104                #pprint.pprint(elk_obj);
105                self.elk.perf.push_data(elk_obj)
106
107
108    def tearDown(self):
109        self.stl_trex.reset()
110        self.stl_trex.clear_stats()
111        sleep(1)
112        CStlGeneral_Test.tearDown(self)
113
114