1 wrk介绍

wrk是一款现代化的HTTP性能测试工具,即使运行在单核CPU上也能产生显著的压力。它融合了一种多线程设计,并使用了一些可扩展事件通知机制,例如epoll and kqueue。一个可选的LuaJIT脚本能产生HTTP请求,响应处理和自定义报告,更详细的脚本内容可以参考scripts目录下的一些例子。

2 wrk下载和安装

先安装git

cd /usr/local/src
sudo yum install git -y

下载wrk文件

git clone https://github.com/wg/wrk.git
cd wrk
make

编译成功后,目录下就会有一个wrk文件。如果编译过程中,出现如下错误:

若报错gcc: Command not found,则需安装gcc,参考wrk编译报错gcc: Command not found

若报错fatal error: openssl/ssl.h: No such file or directory,则需要安装openssl的库。

sudo apt-get install libssl-dev 

或者

sudo yum install  openssl-devel 

3 一个简单的例子

wrk -t12 -c400 -d30s http://127.0.0.1:8080/index.html

它将会产生如下测试,12个线程(threads),保持400个HTTP连接(connections)开启,测试时间30秒(seconds)。

详细的命令行参数如下:

-c,    --connections(连接数):      total number of HTTP connections to keep open with each thread handling N = connections/threads

-d,    --duration(测试持续时间):     duration of the test, e.g. 2s, 2m, 2h

-t,    --threads(线程):            total number of threads to use

-s,    --script(脚本):             LuaJIT script, see SCRIPTING

-H,    --header(头信息):           HTTP header to add to request, e.g. "User-Agent: wrk"

       --latency(响应信息):         print detailed latency statistics

       --timeout(超时时间):         record a timeout if a response is not received within this amount of time.

下面是输出结果:

Running 30s test @ http://127.0.0.1:8080/index.html
threads and connections
Thread Stats Avg Stdev Max +/- Stdev
Latency .91us .89ms .92ms 93.69%
Req/Sec .20k .07k .00k 86.54%
requests in .00s, .76GB read
Requests/sec: 748868.53
Transfer/sec: .33MB

结果解读如下:

Latency: 响应信息, 包括平均值, 标准偏差, 最大值, 正负一个标准差占比。
Req/Sec: 每个线程每秒钟的完成的请求数,同样有平均值,标准偏差,最大值,正负一个标准差占比。
30秒钟总共完成请求数为22464657,读取数据量为17.76GB。
线程总共平均每秒钟处理748868.53个请求(QPS),每秒钟读取606.33MB数据量。

4 发送post请求例子

首先需要创建一个post.lua文件,内容如下:

wrk.method = "POST"
wrk.headers["uid"] = "127.0.0.1"
wrk.headers["Content-Type"] = "application/json"
wrk.body ='{"uid":"127.0.0.1","Version":"1.0","devicetype":"web","port":"8080"}'

测试执行命令如下:

./wrk --latency -t100 -c1500  -d120s --timeout=15s -s post.lua http://127.0.0.1:8080/index.html

这个脚本加入了--lantency:输出结果里可以看到响应时间分布情况,

Running 2m test @ http://127.0.0.1:8080/index.html
threads and connections
Thread Stats Avg Stdev Max +/- Stdev
Latency .26ms .88ms .44s 87.94%
Req/Sec 177.91 45.09 .10k 69.97%
Latency Distribution
% .05ms
% .57ms
% .41ms
% .20ms
requests in 2.00m, .17GB read
Requests/sec: 17804.57
Transfer/sec: .05MB

5 带随机参数的get请求例子

如果想构造不同的get请求,请求带随机参数,则lua脚本如下:

request = function()
num = math.random(,)
path = "/test.html?t=" .. num
return wrk.format("GET", path)
end

6 添加参数txt文件的get请求例子

如果要测试的url需要参数化,uids.txt文件内容如下:

100
101
102

lua脚本如下:

urimap = {}
counter =
function init(args)
for line in io.lines("uids.txt") do
print(line)
urimap[counter] = line
counter = counter +
end
counter =
end request = function()
local path ="/GetInfo.aspx?u=%s&m=1"
parms = urimap[counter%(table.getn(urimap) + )]
path = string.format(path,parms)
counter = counter +
return wrk.format(nil, path)
end

7 添加参数txt文件的post请求例子

lua脚本如下:

urimap = {}
counter =
function init(args)
for line in io.lines("uids.txt") do
urimap[counter] = line
counter = counter +
end
counter =
end request = function()
local body1 = '{"uid":"100%s'
local body2 = '","name":"1"}'
parms = urimap[counter%(table.getn(urimap) + )]
path = "/getinfo"
method = "POST"
wrk.headers["Content-Type"] = "application/json"
body = string.format(body1,parms)..body2
counter = counter +
return wrk.format(method, path, wrk.headers, body)
end

若参数txt中有转义字符,可用如下方法处理:

parms = string.gsub(urimap[counter%(table.getn(urimap) + )],'\r','')

如果要打印返回数据,可添加如下脚本:

a=
function response(status, headers, body)
if(a==)
then
a=
print(body)
end
end

8 提交不同表单内容例子

lua脚本如下:

wrk.method = "POST"
wrk.body = ""
wrk.headers["Content-Type"] = "application/x-www-form-urlencoded" -- 提交不同表单内容
local queries = {
"version=1.0",
"version=2.0",
"version=3.0"
}
local i =
request = function()
local body = wrk.format(nil, nil, nil, queries[i % #queries + ])
i = i +
return body
end

9 访问多个url例子

如果要随机测试多个url,可参考wrk-scripts这个项目。

需要创建一个文件名为paths.txt,里面每行是一个要测试的url网址。lua脚本如下:

counter = 

-- Initialize the pseudo random number generator - http://lua-users.org/wiki/MathLibraryTutorial
math.randomseed(os.time())
math.random(); math.random(); math.random() function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end function shuffle(paths)
local j, k
local n = #paths
for i = , n do
j, k = math.random(n), math.random(n)
paths[j], paths[k] = paths[k], paths[j]
end
return paths
end function non_empty_lines_from(file)
if not file_exists(file) then return {} end
lines = {}
for line in io.lines(file) do
if not (line == '') then
lines[#lines + ] = line
end
end
return shuffle(lines)
end paths = non_empty_lines_from("paths.txt") if #paths <= then
print("multiplepaths: No paths found. You have to create a file paths.txt with one path per line")
os.exit()
end print("multiplepaths: Found " .. #paths .. " paths") request = function()
path = paths[counter]
counter = counter +
if counter > #paths then
counter =
end
return wrk.format(nil, path)
end

使用wrk进行性能测试的更多相关文章

  1. Go 压测

    1. 单测 + 压测 压测 go test -bench=. -benchmem 单元测试 go test -v . 2. pprof + 火焰图(查看cpu占用,内存占用) 嵌入代码 import ...

  2. 性能测试工具 wrk 安装与使用

    介绍 今天给大家介绍一款开源的性能测试工具 wrk,简单易用,没有Load Runner那么复杂,他和 apache benchmark(ab)同属于性能测试工具,但是比 ab 功能更加强大,并且可以 ...

  3. 性能测试工具 wrk 使用教程

    文章首发自个人微信公众号:小哈学Java 个人网站地址:https://www.exception.site/wrk 被面试官经常问到之前开发的系统接口 QPS 能达到多少,经常给不出一个数值,支支吾 ...

  4. wrk 性能测试工具安装与使用

    程序这玩意,性能是很关键的点,之前我一直以为自己写的程序能承载很多很多并发量之类的,然后,被一个搞搞安全的前辈来了个当头一棒,为什么?因为他给我测试了一下我程序的并发量,然后,我想死的心都有了,至于数 ...

  5. 002_性能测试工具wrk安装与使用

    介绍 今天给大家介绍一款开源的性能测试工具 wrk,简单易用,没有Load Runner那么复杂,他和 apache benchmark(ab)同属于性能测试工具,但是比 ab 功能更加强大,并且可以 ...

  6. HTTP性能测试工具wrk安装及使用

    wrk 是一个很简单的 http 性能测试工具,没有Load Runner那么复杂,他和 apache benchmark(ab)同属于HTTP性能测试工具,但是比 ab 功能更加强大,并且可以支持l ...

  7. 【测试设计】性能测试工具选择:wrk?jmeter?locust?还是LR?

    原文链接:http://www.51testing.com/html/49/n-3721249.html 前言 当你想做性能测试的时候,你会选择什么样的测试工具呢?是会选择wrk?jmeter?loc ...

  8. wrk -- 小巧轻盈的 http 性能测试工具.

    标签: wrk http 性能 | 发表时间:2015-06-21 00:55 | 作者:zjumty 出处:http://www.iteye.com 测试先行是软件系统质量保证的有效手段. 在单元测 ...

  9. wrk性能测试(详解)

    一.简介 wrk 是一款针对 Http 协议的基准测试工具,它能够在单机多核 CPU 的条件下,使用系统自带的高性能 I/O 机制,如 epoll,kqueue 等,通过多线程和事件模式,对目标机器产 ...

随机推荐

  1. Ajax 原生和jQuery的ajax用法

    https://www.cnblogs.com/jach/p/5709175.html form数据的序列化: $('#submit').click(function(){ $('#form').se ...

  2. sql优化原则与技巧

    加快sql查询是非常重要的技巧,简单来说加快sql查询的方式有以下几种:一.索引的引用 1.索引一般可以加速数据的检索速度,加速表与表之间的链接,提高性能,所以在对海量数据进行处理时,考虑到信息量比较 ...

  3. 必须掌握的ES6新特性

    ES6(ECMAScript2015)的出现,让前端开发者收到一份惊喜,它简洁的新语法.强大的新特性,带给我们更便捷和顺畅的编码体验,赞! 以下是ES6排名前十的最佳特性列表(排名不分先后): 1.D ...

  4. C和Lua之间的相互调用

    前面的话 第一次接触Lua是因为Unity游戏中需要热更,但是一直没搞懂Lua是怎么嵌入到别的语言中执行的,如何互相调用的.这次打算好好了解一下C跟lua是如何交互的 那么如何使用Lua语言? lua ...

  5. 5分钟了解MySQL5.7的undo log在线收缩新特性

    Part1:写在最前 在MysQL5.6版本中,可以把undo log 回滚日志分离到一个单独的表空间里:其缺点是不能回收空间大小,until MysQL5.7,but MariadDB10.1暂不支 ...

  6. Redis随笔(四)Centos7 搭redis3.2.9集群-3主3从的6个节点服务

    1.虚拟机环境 使用的Linux环境已经版本: Centos 7   64位系统 主机ip: 192.168.56.180 192.168.56.181 192.168.56.182 每台服务器是1主 ...

  7. 手把手教你构建 Kubernetes 1.8 + Flannel 网络(一)

    一.环境说明 操作系统:CentOS7 Kubernetes版本:v1.8.4 Docker版本:v17.06-ce Flannel 版本: flannel-v0.9.1 二.Ntp 服务器配置   ...

  8. 高通secury boot过程

    整理内容网盘地址: 链接:https://share.weiyun.com/79c3920b4f2097d93b719b3149a7f3f9 (密码:4jm9cx) 相关内容网站: http://bl ...

  9. main函数是主线程吗

    1.线程的概念: 线程是程序最基本的运行单位,而进程不能运行,所以能运行的,是进程中的线程. 2.线程是如何创建起来的: 进程仅仅是一个容器,包含了线程运行中所需要的数据结构等信息.一个进程创建时,操 ...

  10. ISE14.7兼容性问题集锦

    六.WARNING:iMPACT:923 - Can not find cable, check cable setup ! 这个错误是由于驱动没有安装或者驱动安装有问题,一般ISE会在安装的时候把驱 ...