在windows下安装wrk并压测
一、环境与背景
- 宿主机:Windows 11(版本 10.0.26200)
- 子系统:WSL 2 + Ubuntu(通过
wsl --install -d Ubuntu 安装)
- 工具:wrk(HTTP 压测工具)
二、安装 WSL 2 与 Ubuntu
1. 检查 WSL 是否已安装
若提示wsl不是命令,需先启用功能;若显示docker-desktop等,说明已装好。
2. 安装 Ubuntu 发行版
- 耗时:约 3~10 分钟(视网速而定,约 1GB 镜像)。
- 初始化:安装完成后自动启动,按要求创建普通用户(不要填 root,如
darling)并设置密码。
3. 进入 Ubuntu 子系统
可能让你输入用户名密码啥的,你就输吧,一直往下走就行
此时提示符变为 用户名@计算机名:~$,即已进入 Linux 环境。
三、在 Ubuntu 中安装 wrk
1. 更新软件源并安装
1 2
| sudo apt update sudo apt install wrk -y
|
2. 验证安装
四、执行压测命令
在 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
| 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
|
结论
网络已连通,但接口性能较差(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 |