一个好的接口可以给自动化测试带来无限的动力,如果接口不好,我们可能会调用ssh去执行bash,或者调用Pexpect库去交互,但是有好的接口,给我们的只是一个包,那就是一个好的自动化测试工具。
TRex Stateful Python API测试
- 启动 Stateful Server
root@trex:/tmp/trex/v2.23# ./trex_daemon_server restart TRex server daemon is killed Firing up TRex REST daemon @ port 8090 ... TRex server daemon is started
- 进入目录
root@trex:/tmp/trex/v2.23# cd automation/trex_control_plane/stf/examples/
- gav.py
1 import argparse 2 import stf_path 3 from trex_stf_lib.trex_client import CTRexClient 4 from pprint import pprint 5 6 # sample TRex stateful run 7 # assuming server daemon is running. 8 9 def minimal_stateful_test(server): 10 print('Connecting to %s' % server) 11 trex_client = CTRexClient(server) 12 13 print('Connected, start TRex') 14 trex_client.start_trex( 15 c = 1, 16 m = 1, 17 f = 'cap2/http_virus.yaml', 18 d = 30, 19 ) 20 21 print('Sample until end') 22 result = trex_client.sample_to_run_finish() 23 24 print('Test results:') 25 print(result) 26 27 print('TX by ports:') 28 tx_ptks_dict = result.get_last_value('trex-global.data', 'opackets-*') 29 print(' | '.join(['%s: %s' % (k.split('-')[-1], tx_ptks_dict[k]) for k in sorted(tx_ptks_d ict.keys())])) 30 31 print('RX by ports:') 32 rx_ptks_dict = result.get_last_value('trex-global.data', 'ipackets-*') 33 print(' | '.join(['%s: %s' % (k.split('-')[-1], rx_ptks_dict[k]) for k in sorted(rx_ptks_d ict.keys())])) 34 35 print('CPU utilization:') 36 print(result.get_value_list('trex-global.data.m_cpu_util')) 37 38 #Dump of *latest* result sample, uncomment to see it all 39 #print('Latest result dump:') 40 #pprint(result.get_latest_dump()) 41 42 43 if __name__ == '__main__': 44 parser = argparse.ArgumentParser(description="Example for TRex Stateful, assuming server dae mon is running.") 45 parser.add_argument('-s', '--server', 46 dest='server', 47 help='Remote trex address', 48 default='127.0.0.1', 49 type = str) 50 args = parser.parse_args() 51 52 minimal_stateful_test(args.server)
- result
root@trex:/tmp/trex/v2.23/automation/trex_control_plane/stf/examples# python gav.py Connecting to 127.0.0.1Connected, start TRexSample until endTest results:Is valid history? TrueDone warmup? TrueExpected tx rate: {u'm_tx_expected_pps': 2800.0, u'm_tx_expected_bps': 14577600.0, u'm_tx_expected_cps': 100.0}Current tx rate: {u'm_tx_bps': 5102998.5, u'm_tx_cps': 31.6, u'm_tx_pps': 978.1}Minimum latency: NoneMaximum latency: NoneAverage latency: NoneAverage window latency: NoneTotal drops: 44986Drop rate: 3328075.8History size so far: 34TX by ports:0: 38987 | 1: 44985RX by ports:0: 23991 | 1: 14995CPU utilization:[0.0, 0.06825, 1.015, 1.059, 0.8428, 0.7444, 0.6778, 0.5246, 0.7297, 0.705, 0.546, 0.4228, 0.6708, 0.5279, 0.5601, 0.4331, 0.3598, 0.4806, 0.412, 0.369, 0.3962, 0.3549, 0.2902, 0.2594, 0.2125, 0.4259, 0.3509, 0.2767, 0.2138, 0.3293, 0.4195, 0.4195, 0.4195, 0.4195]
- 在结果中可以显示发包,收包速率,以及CPU的利用率,当然还可以知道其它的一些关键字,因此适合在自动化中运用
参考
`