pytest_eth_iperf.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. """
  4. Test case for iperf example.
  5. This test case might have problem running on Windows:
  6. - use `sudo killall iperf` to force kill iperf, didn't implement windows version
  7. """
  8. from __future__ import division, unicode_literals
  9. import os
  10. import subprocess
  11. import pytest
  12. from common_test_methods import get_host_ip4_by_dest_ip
  13. from idf_iperf_test_util import IperfUtility
  14. from pytest_embedded import Dut
  15. try:
  16. from typing import Any, Callable, Tuple
  17. except ImportError:
  18. # Only used for type annotations
  19. pass
  20. NO_BANDWIDTH_LIMIT = -1 # iperf send bandwidth is not limited
  21. class IperfTestUtilityEth(IperfUtility.IperfTestUtility):
  22. """ iperf test implementation """
  23. def __init__(self, dut: str, config_name: str, pc_nic_ip: str, pc_iperf_log_file: str, test_result:Any=None) -> None:
  24. IperfUtility.IperfTestUtility.__init__(self, dut, config_name, 'None', 'None', pc_nic_ip, pc_iperf_log_file, test_result)
  25. def setup(self) -> Tuple[str,int]:
  26. """
  27. setup iperf test:
  28. 1. kill current iperf process
  29. 2. reboot DUT (currently iperf is not very robust, need to reboot DUT)
  30. """
  31. try:
  32. subprocess.check_output('sudo killall iperf 2>&1 > /dev/null', shell=True)
  33. except subprocess.CalledProcessError:
  34. pass
  35. self.dut.write('restart')
  36. self.dut.expect("Type 'help' to get the list of commands.")
  37. self.dut.expect('iperf>')
  38. dut_ip = self.dut.expect(r'esp_netif_handlers: .+ ip: (\d+\.\d+\.\d+\.\d+),').group(1)
  39. rssi = 0
  40. return dut_ip, rssi
  41. @pytest.mark.esp32
  42. @pytest.mark.ethernet_router
  43. def test_esp_eth_iperf(
  44. dut: Dut,
  45. log_performance: Callable[[str, object], None],
  46. check_performance: Callable[[str, float, str], None],
  47. ) -> None:
  48. """
  49. steps: |
  50. 1. test TCP tx rx and UDP tx rx throughput
  51. 2. compare with the pre-defined pass standard
  52. """
  53. # 1. wait for DUT
  54. dut.expect_exact('iperf>')
  55. # 2. preparing
  56. pc_iperf_log_file = os.path.join(dut.logdir, 'pc_iperf_log.md')
  57. dut_ip = dut.expect(r'esp_netif_handlers: .+ ip: (\d+\.\d+\.\d+\.\d+),').group(1)
  58. pc_nic_ip = get_host_ip4_by_dest_ip(dut_ip)
  59. test_result = {
  60. 'tcp_tx': IperfUtility.TestResult('tcp', 'tx', 'ethernet'),
  61. 'tcp_rx': IperfUtility.TestResult('tcp', 'rx', 'ethernet'),
  62. 'udp_tx': IperfUtility.TestResult('udp', 'tx', 'ethernet'),
  63. 'udp_rx': IperfUtility.TestResult('udp', 'rx', 'ethernet'),
  64. }
  65. test_utility = IperfTestUtilityEth(dut, 'ethernet', pc_nic_ip, pc_iperf_log_file, test_result)
  66. # 3. run test for TCP Tx, Rx and UDP Tx, Rx
  67. test_utility.run_test('tcp', 'tx', 0, NO_BANDWIDTH_LIMIT)
  68. test_utility.run_test('tcp', 'rx', 0, NO_BANDWIDTH_LIMIT)
  69. test_utility.run_test('udp', 'tx', 0, 80)
  70. test_utility.run_test('udp', 'rx', 0, NO_BANDWIDTH_LIMIT)
  71. # 4. log performance and compare with pass standard
  72. for throughput_type in test_result:
  73. log_performance('{}_throughput'.format(throughput_type),
  74. '{:.02f} Mbps'.format(test_result[throughput_type].get_best_throughput()))
  75. # do check after logging, otherwise test will exit immediately if check fail, some performance can't be logged.
  76. for throughput_type in test_result:
  77. check_performance('{}_throughput'.format(throughput_type + '_eth'),
  78. test_result[throughput_type].get_best_throughput(),
  79. dut.target)