<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>性能测试神器wrk教程 on 重定向学院</title><link>https://itest2025.pages.dev/categories/%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95%E7%A5%9E%E5%99%A8wrk%E6%95%99%E7%A8%8B/</link><description>Recent content in 性能测试神器wrk教程 on 重定向学院</description><generator>Hugo</generator><language>zh-CN</language><lastBuildDate>Tue, 17 Jun 2025 17:29:15 +0800</lastBuildDate><atom:link href="https://itest2025.pages.dev/categories/%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95%E7%A5%9E%E5%99%A8wrk%E6%95%99%E7%A8%8B/index.xml" rel="self" type="application/rss+xml"/><item><title>wrk系列教程(1)安装</title><link>https://itest2025.pages.dev/tutorials/15-001-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B1%E5%AE%89%E8%A3%85/</link><pubDate>Fri, 01 Mar 2019 02:00:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/15-001-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B1%E5%AE%89%E8%A3%85/</guid><description>&lt;p>&lt;a href="https://github.com/wg/wrk" target="_blank" rel="noopener noreffer ">wrk&lt;/a>是一个高性能的性能测试工具，具有非常强的并发能力，很适合做benchmark。&lt;/p>
&lt;p>wrk适合做一些简单的单接口性能压测，具有一定的扩展能力，不太适合做全链路的带业务压测。&lt;/p>
&lt;h3 id="下载源码">下载源码&lt;/h3>
&lt;p>首先你需要1台linux虚拟机或者mac机器。&lt;/p>
&lt;p>如果你用使用git，可以使用下面命令来下载源码&lt;/p>
&lt;pre tabindex="0">&lt;code>git clone https://github.com/wg/wrk.git
&lt;/code>&lt;/pre>&lt;p>或者你可以使用wget来下载源码的压缩包，&lt;strong>并自行解压&lt;/strong>&lt;/p>
&lt;pre tabindex="0">&lt;code>wget https://github.com/wg/wrk/archive/master.zip
&lt;/code>&lt;/pre>&lt;h3 id="安装wrk">安装wrk&lt;/h3>
&lt;p>cd到wrk源码所在目录，运行&lt;/p>
&lt;pre tabindex="0">&lt;code>make
&lt;/code>&lt;/pre>&lt;p>如果一切正常，一段时间后wrk会自动编译完成。我们可以将编译出的wrk可执行文件拷贝到&lt;code>/usr/local/bin&lt;/code>中，这样调用起来会比较方便。&lt;/p>
&lt;p>如果需要定制化安装，你可以参考&lt;a href="https://github.com/wg/wrk/blob/master/INSTALL" target="_blank" rel="noopener noreffer ">这里&lt;/a>&lt;/p>
&lt;h3 id="验证安装">验证安装&lt;/h3>
&lt;p>运行wrk可执行程序，你应该看到下面的结果&lt;/p>
&lt;pre tabindex="0">&lt;code>Usage: wrk &amp;lt;options&amp;gt; &amp;lt;url&amp;gt;
 Options:
 -c, --connections &amp;lt;N&amp;gt; Connections to keep open
 -d, --duration &amp;lt;T&amp;gt; Duration of test
 -t, --threads &amp;lt;N&amp;gt; Number of threads to use

 -s, --script &amp;lt;S&amp;gt; Load Lua script file
 -H, --header &amp;lt;H&amp;gt; Add header to request
 --latency Print latency statistics
 --timeout &amp;lt;T&amp;gt; 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)
&lt;/code>&lt;/pre>&lt;p>原始封面&lt;/p></description></item><item><title>wrk系列教程(2)简单使用</title><link>https://itest2025.pages.dev/tutorials/15-002-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B2%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8/</link><pubDate>Sat, 02 Mar 2019 02:00:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/15-002-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B2%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8/</guid><description>&lt;p>简单起见，建议大家可以先安装python。&lt;/p>
&lt;h3 id="在本机启动被测服务">在本机启动被测服务&lt;/h3>
&lt;p>创建一个&lt;code>index.html&lt;/code>文件&lt;/p>
&lt;pre tabindex="0">&lt;code>echo &amp;#39;&amp;lt;h3&amp;gt;It works&amp;lt;/h3&amp;gt;&amp;#39; &amp;gt; index.html
&lt;/code>&lt;/pre>&lt;p>index.html里面文件内容很简单，一句话&lt;/p>
&lt;pre tabindex="0">&lt;code>&amp;lt;h3&amp;gt;It works&amp;lt;/h3&amp;gt;&amp;#39;
&lt;/code>&lt;/pre>&lt;p>使用下面命令启动web服务。&lt;/p>
&lt;pre tabindex="0">&lt;code>python -m SimpleHTTPServer #python2

python -m http.server #python3
&lt;/code>&lt;/pre>&lt;p>预期结果应该是下面这个样子&lt;/p>
&lt;pre tabindex="0">&lt;code>Serving HTTP on 0.0.0.0 port 8000 ...
&lt;/code>&lt;/pre>&lt;h3 id="验证web服务">验证web服务&lt;/h3>
&lt;p>新开1个terminal窗口，使用下面命令&lt;/p>
&lt;pre tabindex="0">&lt;code>curl localhost:8000/index.html
&lt;/code>&lt;/pre>&lt;p>这时候命令行应该显示正确的返回&lt;/p>
&lt;pre tabindex="0">&lt;code>&amp;lt;h3&amp;gt;It works&amp;lt;/h3&amp;gt;
&lt;/code>&lt;/pre>&lt;h3 id="开始压测">开始压测&lt;/h3>
&lt;p>使用命令 &lt;code>wrk -c200 -t10 -d30s http://127.0.0.1:8000/index.html&lt;/code>&lt;/p>
&lt;p>解释一下主要参数&lt;/p>
&lt;ul>
&lt;li>c: connections 表示一共起多少个链接&lt;/li>
&lt;li>t: 表示用多少个&lt;/li>
&lt;/ul>
&lt;h3 id="结果展示">结果展示&lt;/h3>
&lt;p>在我的机器上结果是这个样子的&lt;/p>
&lt;pre tabindex="0">&lt;code>Running 30s test @ http://127.0.0.1:8000/index.html
 10 threads and 200 connections
 Thread Stats Avg Stdev Max +/- Stdev
 Latency 3.24ms 6.54ms 154.00ms 95.36%
 Req/Sec 167.54 227.05 1.81k 84.83%
 16145 requests in 30.08s, 3.13MB read
 Socket errors: connect 0, read 1208, write 31, timeout 0
Requests/sec: 536.78
Transfer/sec: 106.41KB
&lt;/code>&lt;/pre>&lt;p>对我们来说最重要的1个数据就是倒数第二行的&lt;code>Requests/sec: 536.78&lt;/code>。这说明我们测出的服务的QPS是536。&lt;/p></description></item><item><title>wrk系列教程(3)统计结果</title><link>https://itest2025.pages.dev/tutorials/15-003-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B3%E7%BB%9F%E8%AE%A1%E7%BB%93%E6%9E%9C/</link><pubDate>Sun, 03 Mar 2019 02:00:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/15-003-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B3%E7%BB%9F%E8%AE%A1%E7%BB%93%E6%9E%9C/</guid><description>&lt;p>回到上一节的统计结果，我们来看一下这些结果是如何统计出来的。&lt;/p>
&lt;pre tabindex="0">&lt;code>Running 30s test @ http://127.0.0.1:8000/index.html
 10 threads and 200 connections
 Thread Stats Avg Stdev Max +/- Stdev
 Latency 3.24ms 6.54ms 154.00ms 95.36%
 Req/Sec 167.54 227.05 1.81k 84.83%
 16145 requests in 30.08s, 3.13MB read
 Socket errors: connect 0, read 1208, write 31, timeout 0
Requests/sec: 536.78
Transfer/sec: 106.41KB
&lt;/code>&lt;/pre>&lt;p>我们一行一行来看一下这些统计数据。&lt;/p>
&lt;ul>
&lt;li>&lt;code>Running 30s test @ http://127.0.0.1:8000/index.html&lt;/code>，测试持续时间已经被测url&lt;/li>
&lt;li>&lt;code>10 threads and 200 connections&lt;/code>，总共多少个thread和connection&lt;/li>
&lt;li>&lt;code>Latency 3.24ms 6.54ms 154.00ms 95.36% &lt;/code>，时延的平均值，标准差以及最大值以及值落在标准差区间内的百分比&lt;/li>
&lt;li>&lt;code>Req/Sec 167.54 227.05 1.81k 84.83%&lt;/code>，每个线程运行时的qps平均值，标准差以及百分比区间。看了一圈源码，没太看懂这个统计的意义&lt;/li>
&lt;li>&lt;code> 16145 requests in 30.08s, 3.13MB read&lt;/code>，总请求数，运行的总时间以及传输数据的总和&lt;/li>
&lt;li>&lt;code> Socket errors: connect 0, read 1208, write 31, timeout 0&lt;/code>，socket错误的统计&lt;/li>
&lt;li>&lt;code>Requests/sec: 536.78&lt;/code>， 总请求数除以总的运行时间，也就是qps&lt;/li>
&lt;li>&lt;code>Transfer/sec: 106.41KB&lt;/code>， 传输的总数据除以总的运行时间&lt;/li>
&lt;/ul>
&lt;p>原始封面&lt;/p></description></item><item><title>wrk系列教程(4)常用参数</title><link>https://itest2025.pages.dev/tutorials/15-004-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B4%E5%B8%B8%E7%94%A8%E5%8F%82%E6%95%B0/</link><pubDate>Sun, 03 Mar 2019 02:00:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/15-004-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B4%E5%B8%B8%E7%94%A8%E5%8F%82%E6%95%B0/</guid><description>&lt;p>这一节我们看一下wrk常用的命令行参数。&lt;/p>
&lt;pre tabindex="0">&lt;code>-c, --connections: HTTP连接数的总量。对于每一个线程，保持的连接数N
 N = connections/threads

-d, --duration: 测试运行时间, e.g. 2s, 2m, 2h

-t, --threads: 总的线程数

-s, --script: lua脚本，后面会讲

-H, --header: 设置请求的HTTP header, e.g. &amp;#34;User-Agent: wrk&amp;#34;

 --latency: 打印详细的时延统计

 --timeout: 如果请求的timeout的时间内没有响应，那么就记录1个timeout
&lt;/code>&lt;/pre>&lt;h3 id="具体例子">具体例子&lt;/h3>
&lt;p>10个线程100个连接跑1个小时&lt;/p>
&lt;pre tabindex="0">&lt;code>wrk -t10 -c100 -d1h www.example.com
&lt;/code>&lt;/pre>&lt;p>10个线程100个连接跑1个小时，请求的响应超时时间设置为1s&lt;/p>
&lt;pre tabindex="0">&lt;code>wrk -t10 -c100 -d1h --timeout=1s www.example.com
&lt;/code>&lt;/pre>&lt;p>模拟ios上的safari浏览器 10个线程100个连接跑1个小时&lt;/p>
&lt;pre tabindex="0">&lt;code>wrk -t10 -c100 -d1h --header=&amp;#34;User-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1&amp;#34; www.example.com
&lt;/code>&lt;/pre>&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1511452885600-a3d2c9148a31?w=300"
 data-srcset="https://images.unsplash.com/photo-1511452885600-a3d2c9148a31?w=300, https://images.unsplash.com/photo-1511452885600-a3d2c9148a31?w=300 1.5x, https://images.unsplash.com/photo-1511452885600-a3d2c9148a31?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1511452885600-a3d2c9148a31?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>wrk系列教程(5)lua扩展</title><link>https://itest2025.pages.dev/tutorials/15-005-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B5lua%E6%89%A9%E5%B1%95/</link><pubDate>Sun, 03 Mar 2019 02:00:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/15-005-wrk%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B5lua%E6%89%A9%E5%B1%95/</guid><description>&lt;p>&lt;a href="https://github.com/wg/wrk/blob/master/SCRIPTING" target="_blank" rel="noopener noreffer ">官方文档&lt;/a>上面对wrk的lua脚本扩展方式有详细的说明。&lt;/p>
&lt;p>lua脚本可以为wrk增加更多灵活的功能，通过代码扩展，wrk的定制能力会有很大的提升。&lt;/p>
&lt;h3 id="钩子函数">钩子函数&lt;/h3>
&lt;p>wrk提供了一些简单的钩子函数，可以实现在测试过程中对请求及场景做定制化的增强。&lt;/p>
&lt;p>这些函数分别是&lt;/p>
&lt;ul>
&lt;li>setup()&lt;/li>
&lt;/ul>
&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1496851473196-e26508c21494?w=300"
 data-srcset="https://images.unsplash.com/photo-1496851473196-e26508c21494?w=300, https://images.unsplash.com/photo-1496851473196-e26508c21494?w=300 1.5x, https://images.unsplash.com/photo-1496851473196-e26508c21494?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1496851473196-e26508c21494?w=300"
 title="课程图片" />&lt;/p></description></item></channel></rss>