在windows下安装wrk并压测

一、环境与背景

  • 宿主机:Windows 11(版本 10.0.26200)
  • 子系统:WSL 2 + Ubuntu(通过 wsl --install -d Ubuntu 安装)
  • 工具:wrk(HTTP 压测工具)

二、安装 WSL 2 与 Ubuntu

1. 检查 WSL 是否已安装

1
2
# 在 Windows CMD / PowerShell 中执行
wsl -l -v

若提示wsl不是命令,需先启用功能;若显示docker-desktop等,说明已装好。

2. 安装 Ubuntu 发行版

1
wsl --install -d Ubuntu
  • 耗时:约 3~10 分钟(视网速而定,约 1GB 镜像)。
  • 初始化:安装完成后自动启动,按要求创建普通用户不要填 root,如 darling)并设置密码。

3. 进入 Ubuntu 子系统

1
wsl -d Ubuntu

可能让你输入用户名密码啥的,你就输吧,一直往下走就行

此时提示符变为 用户名@计算机名:~$,即已进入 Linux 环境。


三、在 Ubuntu 中安装 wrk

1. 更新软件源并安装

1
2
sudo apt update
sudo apt install wrk -y

2. 验证安装

1
2
wrk -v
# 正常输出版本信息,如 wrk 4.2.0

四、执行压测命令

在 WSL 内部执行

基础用法

1
wrk -t 线程数 -c 连接数 -d 压测时间 测试URL
1
2
3
wrk -t1 -d1s -c2 -s /mnt/e/go_learning/webook_project/scripts/wrk/signup.lua http://localhost:9090

wrk -t1 -d1s -c2 -s /mnt/e/go_learning/webook_project/scripts/wrk/signup.lua http://localhost:80/users/signup

e/go_learning/webook_project/scripts/wrk/signup.lua这一部分是在windows中测试注册功能的脚本的文件地址,这个脚本文件需要根据自己测试内容来写,笔者就不给出了

参数释义

参数 含义
-t1 启动 1 个线程
-d1s 压测持续 1 秒
-c2 保持 2 个并发连接
-s 指定 Lua 脚本路径
http://localhost:80/users/signup 目标地址

注意:脚本路径找不到

  • 场景No such file or directory
  • 原因:Windows 路径与 WSL 路径映射错误。
  • 解决:Windows E:\dir\file.lua 对应 WSL 的 /mnt/e/dir/file.lua(注意盘符小写)。

输出结果

1
2
3
4
5
6
7
8
9
darling@darling123456:/mnt/c/Windows/System32$ wrk -t1 -d1s -c2 -s /mnt/e/go_learning/webook_project/scripts/wrk/signup.lua http://localhost:80/users/signupnup
Running 1s test @ http://localhost:80/users/signup
1 threads and 2 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 419.90ms 134.68ms 538.57ms 100.00%
Req/Sec 4.50 2.12 6.00 100.00%
4 requests in 1.00s, 516.00B read
Requests/sec: 3.99
Transfer/sec: 514.77B
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 命令:-t1 表示1个线程,-d1s 表示压测1秒,-c2 表示2个并发连接,-s 指定Lua脚本路径,最后是目标URL
wrk -t1 -d1s -c2 -s /mnt/e/go_learning/webook_project/scripts/wrk/signup.lua http://localhost:80/users/signupnup

# 测试信息:1秒压测,目标地址
Running 1s test @ http://localhost:80/users/signup

# 测试配置:1个线程,2个并发连接
1 threads and 2 connections

# 线程统计表头:平均值、标准差、最大值、标准差覆盖率
Thread Stats Avg Stdev Max +/- Stdev

# 延迟统计:平均420ms,标准差134ms,最大538ms,100%的请求落在这个范围
Latency 419.90ms 134.68ms 538.57ms 100.00%

# 单线程QPS:平均每秒4.5个请求,最大峰值6个
Req/Sec 4.50 2.12 6.00 100.00%

# 汇总:1秒内完成4个请求,读取516字节数据
4 requests in 1.00s, 516.00B read

# 整体QPS:每秒约4个请求(核心性能指标,正常值应为数百以上)
Requests/sec: 3.99

# 网络吞吐量:每秒传输约515字节
Transfer/sec: 514.77B

结论

网络已连通,但接口性能较差(QPS≈4,平均延迟420ms)


五、快速命令速查表(备忘)

操作 命令
进入 Ubuntu wsl -d Ubuntu
安装 wrk sudo apt install wrk -y
端口转发(K8s) kubectl port-forward service/webook 9090:80
简单 GET 压测 wrk -t2 -c10 -d5s http://localhost:9090/ping
POST 脚本压测 wrk -t1 -c2 -d1s -s /path/to/script.lua http://localhost:9090
查看 K8s Pods kubectl get pods
查看 K8s Services kubectl get svc
查看 80 端口占用 (Windows) netstat -ano | findstr :80