iperf_server.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. --[[
  2. @module iperf_server
  3. @summary iperf服务器模式测试模块
  4. @version 1.0
  5. @date 2025.10.29
  6. @author 拓毅恒
  7. @usage
  8. 本模块演示如何初始化CH390以太网并启动iperf服务器模式进行测试。
  9. 适用于路由器连接场景,设备通过DHCP从路由器获取IP地址。
  10. 使用步骤:
  11. 1、初始化SPI接口连接CH390
  12. 2、设置CH390驱动和网络参数
  13. 3、配置从路由器获取IP地址
  14. 4、启动iperf服务器并处理测试结果
  15. 本文件没有对外接口,直接在 main.lua 中 require "iperf_server" 即可加载运行。
  16. ]]
  17. -- 引入必要的模块
  18. local netdrv_eth_spi = require "netdrv_eth_spi"
  19. -- 记录服务器IP
  20. local server_ip = "0.0.0.0"
  21. -- iperf测试报告处理函数
  22. local function iperf_report_handler(bytes, ms_duration, bandwidth)
  23. -- 转换为Mbps显示
  24. local bandwidth_mbps = bandwidth / 1024 / 1024 * 8
  25. log.info("iperf报告", string.format("数据量: %d bytes, 持续时间: %d ms, 带宽: %.2f Mbps", bytes, ms_duration, bandwidth_mbps))
  26. end
  27. -- iperf服务器任务
  28. local function iperf_server_task()
  29. log.info("iperf测试", "等待网络初始化完成...")
  30. -- 等待IP地址获取成功
  31. log.info("iperf测试", "等待获取IP地址...")
  32. local ip_wait_count = 60
  33. while true do
  34. local ipv4 = socket.localIP(socket.LWIP_USER1)
  35. if ipv4 and ipv4 ~= "0.0.0.0" then
  36. log.info("iperf测试", "IP获取成功:", ipv4)
  37. server_ip = ipv4
  38. break
  39. end
  40. -- 超时检查
  41. if ip_wait_count <= 0 then
  42. log.error("iperf测试", "获取IP地址超时")
  43. return
  44. end
  45. ip_wait_count = ip_wait_count - 1
  46. sys.wait(1000)
  47. end
  48. -- 等待以太网连接
  49. while not socket.adapter(socket.LWIP_USER1) do
  50. sys.wait(100)
  51. end
  52. log.info("iperf测试", "以太网连接状态: 已连接")
  53. log.info("iperf测试", "网络配置完成")
  54. -- 订阅iperf测试报告事件
  55. sys.subscribe("IPERF_REPORT", iperf_report_handler)
  56. -- 启动iperf服务器
  57. log.info("iperf测试", "启动服务器模式")
  58. log.info("iperf测试", "服务器IP地址:", server_ip, "端口: 5001")
  59. iperf.server(socket.LWIP_USER1)
  60. sys.wait(2000)
  61. log.info("iperf测试", "服务器已启动,等待客户端连接")
  62. log.info("iperf测试", "请在客户端设备上设置服务器IP地址为:", server_ip)
  63. end
  64. -- 执行iperf服务器模式测试
  65. sys.taskInit(iperf_server_task)