wrk 压测工具

wrk 是一款常用的 HTTP 压测工具它结合了多线程、基于事件的 I/O 和 Lua 脚本的强大功能,能够模拟大量的并发请求并收集响应数据。

主要特点:

  1. 高性能:wrk 可以通过多线程机制并发发送大量的 HTTP 请求,最大化网络资源和 CPU 使用率,适合测试高流量场景。
  2. 灵活的 Lua 脚本支持:可以通过 Lua 脚本定制请求逻辑,例如动态生成请求体、处理认证、模拟更复杂的用户行为等。
  3. 简单易用:命令行工具设计简洁,可以通过指定目标 URL、请求数和并发数来快速进行压力测试。

安装

环境是 centos 7.6wrk 目前不支持第三方工具安装,只能通过下载源代码编译安装

# 克隆源代码,需要科学上网
git clone https://github.com/wg/wrk.git

# 切换到源代码目录
cd wrk

## 下载编译源码安装依赖
yum install build-essential unzip git openssl-devel -y
yum groupinstall "Development Tools" -y

# 开始构建编译
make

# 拷贝到应用目录中
cp wrk /usr/local/bin

# 验证是否成功
wrk                                                                                                                                !3751
Usage: wrk <options> <url>
  Options:
    -c, --connections <N>  Connections to keep open
    -d, --duration    <T>  Duration of test
    -t, --threads     <N>  Number of threads to use

    -s, --script      <S>  Load Lua script file
    -H, --header      <H>  Add header to request
        --latency          Print latency statistics
        --timeout     <T>  Socket/request timeout
    -v, --version          Print version details

  Numeric arguments may include a SI unit (1k, 1M, 1G)
  Time arguments may include a time unit (2s, 2m, 2h)

看到上述信息表示安装成功!

参数

在使用之前来看一下上边输出几个参数的含义

-c 发起的连接数量
-d 测试的持续时间
-t 测试使用的线程数量

-s 表示加载 lua 脚本文件
-H 添加请求头信息

--latency 表示输出延迟统计数据
--timeout 测试超时时间

案例

看一个真实测试,两台虚拟机一台上边用 docker 先启动一个 nginx 服务

docker run --network=host --name=nginx1 -itd nginx:latest

# 查看服务
docker ps                                                                                                                            !3767
CONTAINER ID   IMAGE          COMMAND                 PORTS                               NAMES
62ee9b27a140   nginx:latest   "/docker-entrypoint.…"  0.0.0.0:80->80/tcp, :::80->80/tcp   nginx-1

# 测试服务
curl -I 192.168.56.61                                                                                                                !3766
HTTP/1.1 200 OK
Server: nginx/1.27.0
Date: Fri, 13 Sep 2024 05:59:44 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 28 May 2024 13:22:30 GMT
Connection: keep-alive
ETag: "6655da96-267"
Accept-Ranges: bytes

在本台机器上测试

# wrk --latency -c 60 -t 2 --timeout 2 http://192.168.56.61                                                                            !3768
Running 10s test @ http://192.168.56.61
  2 threads and 60 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.62ms    2.38ms  27.38ms   74.56%
    Req/Sec     8.38k     1.57k   13.87k    66.00%
  Latency Distribution
     50%    2.98ms
     75%    4.57ms
     90%    6.76ms
     99%   12.04ms
  167779 requests in 10.08s, 136.49MB read
Requests/sec:  16652.76
Transfer/sec:     13.55MB

在另外一台机器上测试

% wrk --latency -c 60 -t 2 --timeout 2 http://192.168.56.61
Running 10s test @ http://192.168.56.61
  2 threads and 60 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    34.48ms   41.52ms 365.18ms   83.32%
    Req/Sec     1.39k   429.38     2.20k    59.80%
  Latency Distribution
     50%   12.92ms
     75%   48.72ms
     90%  100.91ms
     99%  163.74ms
  27748 requests in 10.10s, 22.57MB read
Requests/sec:   2746.92
Transfer/sec:      2.23MB

数据对比是不是很明显!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值