认识 parallelHTTP — 一个用于压力测试你的 API 的简易工具

发布: (2025年12月13日 GMT+8 14:43)
5 min read
原文: Dev.to

Source: Dev.to

什么是 ParallelHTTP

ParallelHTTP 是一个极简却功能完整的工具,能够让你:

  • 并行发送多个 HTTP 请求。
  • 配置请求方法(GET、POST 等)、接口 URL、请求体以及超时时间。
  • 跟踪响应时间和状态码。
  • 获取聚合摘要:成功/错误计数、平均延迟、延迟分位数。
  • 将响应导出为 CSV 以便后续检查。

它提供了多种 接口,取决于你的使用方式:

  • Web UI – 通过浏览器进行交互式测试。
  • CLI 模式 – 在终端快速测试,适合自动化、脚本或 CI。
  • Docker 镜像 – 容器化使用,便于与其他工具集成。
  • REST API 端点 – 以编程方式触发负载或与其他系统集成。

为什么要并行 HTTP 请求 —— parallelHTTP 的定位

在进行 压力测试基准测试负载测试 时,常常需要并行发送大量 HTTP 请求。并行请求能够模拟并发客户端,往往更贴近真实的使用场景,尤其是在高负载情况下。

实现并行的方式有很多(线程、异步编程、语言特定库等)。例如,在 Python 中可以使用线程或 async 库来并行化 HTTP 调用。

parallelHTTP 将 UI、CLI、REST API 打包在一起——你无需编写自定义脚本或胶水代码。对于只想快速搭建负载测试的开发者或 QA 工程师,它提供了即插即用的解决方案。

如何安装 parallelHTTP

你可以通过 二进制文件DockerWeb UICLI 使用 ParallelHTTP。

1. 使用二进制文件安装(CLI 模式)

运行:

./parallelhttp --help

可用参数:

-duration duration
      Max duration for all calls. Example: 0->no limit, 1ms, 1s, 10m
-endpoint string
      Request endpoint to be called.
-format string
      Response format. One of: text, yaml, json. Default json. (default "json")
-method string
      Request Method. Default GET. (default "GET")
-parallel int
      Number of parallel calls. Default 1. (default 1)
-timeout duration
      Request timeout. Default 10s

使用示例

./parallelhttp \
  --endpoint=http://localhost:8080/test \
  --parallel=5 \
  --method=GET \
  --timeout=2s \
  --duration=10s \
  --format=json

示例输出(JSON):

{
  "requests": [
    {
      "response": {
        "status_code": 200,
        "time": "2025-12-02T04:39:26.450811405+01:00",
        "duration": 176680135,
        "duration_h": "176.680135ms"
      },
      "error": null,
      "error_message": null
    },
    {
      "response": {
        "status_code": 200,
        "time": "2025-12-02T04:39:26.450838753+01:00",
        "duration": 177105875,
        "duration_h": "177.105875ms"
      },
      "error": null,
      "error_message": null
    },
    {
      "response": {
        "status_code": 200,
        "time": "2025-12-02T04:39:26.450989804+01:00",
        "duration": 176999320,
        "duration_h": "176.99932ms"
      },
      "error": null,
      "error_message": null
    },
    {
      "response": {
        "status_code": 200,
        "time": "2025-12-02T04:39:26.450761076+01:00",
        "duration": 177158817,
        "duration_h": "177.158817ms"
      },
      "error": null,
      "error_message": null
    },
    {
      "response": {
        "status_code": 200,
        "time": "2025-12-02T04:39:26.450879196+01:00",
        "duration": 179940733,
        "duration_h": "179.940733ms"
      },
      "error": null,
      "error_message": null
    }
  ],
  "stats": {
    "start_time": "2025-12-02T04:39:26.450727731+01:00",
    "end_time": "2025-12-02T04:39:26.630824982+01:00",
    "duration": "180.097251ms",
    "latency": {
      "p50": "177.105875ms",
      "p90": "179.940733ms",
      "p99": "179.940733ms"
    }
  }
}

2. Web UI(Docker)

docker run --rm -p 8080:8080 -it nicumicle/parallelhttp

在浏览器中打开 http://localhost:8080。你将看到一个 UI,可以选择端点、配置参数并运行测试,结果会直接在浏览器中显示。

使用场景 —— parallelHTTP 的优势

  • API 性能测试 – 模拟 100、1 000 或 10 000 并发请求,观察延迟、错误和超时情况。
  • 新服务器版本基准对比 – 比较不同版本之间的延迟或错误率。
  • 生产前的压力/负载测试 – 在功能上线或流量高峰前验证系统稳定性。
  • 负载下的回归测试 – 集成到 CI/CD 流水线,在代码变更后自动运行负载测试。
  • 导出详细指标 – CSV 导出可将结果导入电子表格或可视化工具,进行更深入的分析。

结语

parallelHTTP 提供了一种简洁的方式来并行发送 HTTP 请求、测量延迟和状态码,并导出结果。它是开发者、QA 工程师或任何需要快速进行 API 压力测试的人的实用工具。

探索仓库并用自己的 API 试一试吧。

Back to Blog

相关文章

阅读更多 »

网站上线后如何进行测试?

封面图片:How do you test your website after launch? https://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A...