当前位置: 移动技术网 > IT编程>开发语言>.net > 使用 WRK 压力测试工具对 ASP.NET Core 的接口进行压力测试

使用 WRK 压力测试工具对 ASP.NET Core 的接口进行压力测试

2018年10月17日  | 移动技术网IT编程  | 我要评论

水下ufo网站,单身部落下载,宾馆偷听录音

0. 简要介绍

wrk 是一款轻量且易用的 http 压力测试工具,通过该工具我们可以方便地对我们所开发的 webapi 项目进行压力测试,并且针对测试的情况返回结果。

ps:wrk 并不能针对测试的结果生成动态的图表,如果有这种需要,可以尝试使用另一款工具 vegeta。该项目使用的 golang 进行编写,其 github 地址为:

下面的内容就是一个标准的测试结果信息:

# 针对 127.0.0.1:8080 进行压力测试
wrk -t12 -c400 -d30s http://127.0.0.1:8080/
# 这里是测试结果
running 30s test @ http://127.0.0.1:8080/
  12 threads and 400 connections
  thread stats   avg      stdev     max   +/- stdev
    latency   635.91us    0.89ms  12.92ms   93.69%
    req/sec    56.20k     8.07k   62.00k    86.54%
  22464657 requests in 30.00s, 17.76gb read
requests/sec: 748868.53
transfer/sec:    606.33mb

1. 安装

关于 os x 与 windows 的安装可以参考 wrk 官方 wiki 进行操作,本文主要讲解一下 centos 7.x 下如果进行编译。

sudo yum groupinstall 'development tools'
sudo yum install -y openssl-devel git 
git clone https://github.com/wg/wrk.git wrk
cd wrk
make

编译之后,你会得到如下结果:

可以看到生成了一个 wrk 的可执行文件,你可以将其添加到环境变量的 path 当中,这里就不再赘述,我们等会儿使用的时候直接 ./wrk 使用。

2. 命令说明

./wrk -h "authorization: bearer tokenvalue" -t 2 -c 50 -d 10s --latency --timeout 1s "http://"

上面的命令就是一个典型的压力测试命令,关于参数的含义请看下表。

执行命令时的参数 含义 示例
-c 与 http 保持连接的连接数,最终每个线程能够处理的为 连接数/线程数。 -c 50
-d 指定压力测试的时间有多长。 -d 10s,其他单位有 2s,2m,2h
如果不带单位的话,默认为秒。
-t 压力测试时所使用的线程数目,最好为你 cpu 核心的数量。 -t 4
-s 指定要执行的 lua 脚本 -s ./post.lua
-h 执行请求的时候所附带的 header 组。 -h "user-agent: wrk"
--latency 打印详细的统计信息。 --latency
--timeout 每次请求所返回响应体的时间,如果超过了配置的时间,则视为请求超时。 --timeout 1s

3. 开始压力测试

执行了上述代码之后我们可以看到很直观的信息,第一个就是 20s 的时间内完成了 2887 次请求,一共接受到了 2.46mb 的数据。在 socket errors 里面我们可以看到有 35 个请求产生了超时的情况,每秒执行的请求大概为 144.20 个,每秒的数据传输大概为 125.75 kb。

除此之外,还说明了平均每次请求所消耗的时间为 338.44 ms,最极端的情况为 994.27ms。

4. lua 脚本

在第三节我们可以看到一些标准的 get 请求我们可以直接通过指定命令来进行测试,即便该接口有授权验证,我们可以通过 -h 参数来指定 authorization 头来实现权限验证。
但是针对一些复杂的情况,我们就需要编写 lua 脚本来实现压力测试了。

官方编写了很多的 lua 脚本 demo ,存放在 github 上面,其地址为:。

这里我们以实现 post 请求为例:

wrk.method = "post"
wrk.body   = '{"username":"admin","password":"123qwe","rememberclient":true}'
wrk.headers["content-type"] = "application/json"

这里我们的接口地址更改了一下,改变成了 login 接口,该接口需要传入用户名与密码,并且其 method 为 post。

将上述 lua 脚本保存为 post.lua 文件,然后通过 -s 参数指定 lua 脚本的路径并执行。

5. lua 脚本相关详解

wrk 中执行 http 请求的时候,调用 lua 分为 3 个阶段,setuprunningdone,每个 wrk 线程中都有独立的脚本环境。

5.1 wrk 的全局属性

wrk = {
  scheme  = "http",
  host    = "localhost",
  port    = nil,
  method  = "get",
  path    = "/",
  headers = {},
  body    = nil,
  thread  = <userdata>,
}

5.2 wrk 的全局方法

-- 生成整个request的string,例如:返回
-- get / http/1.1
-- host: tool.lu
function wrk.format(method, path, headers, body)

-- 获取域名的ip和端口,返回table,例如:返回 `{127.0.0.1:80}`
function wrk.lookup(host, service)

-- 判断addr是否能连接,例如:`127.0.0.1:80`,返回 true 或 false
function wrk.connect(addr)

5.3 setup 阶段

setup() 方法是在线程创建之后,启动之前。

function setup(thread)

-- thread提供了1个属性,3个方法
-- thread.addr 设置请求需要打到的ip
-- thread:get(name) 获取线程全局变量
-- thread:set(name, value) 设置线程全局变量
-- thread:stop() 终止线程

5.4 running 阶段

function init(args)
-- 每个线程仅调用1次,args 用于获取命令行中传入的参数, 例如 --env=pre

function delay()
-- 每个线程调用多次,发送下一个请求之前的延迟, 单位为ms

function request()
-- 每个线程调用多次,返回http请求

function response(status, headers, body)
-- 每个线程调用多次,返回http响应

5.5 done 阶段

可以用于自定义结果报表,整个过程中只执行一次。

function done(summary, latency, requests)


latency.min              -- minimum value seen
latency.max              -- maximum value seen
latency.mean             -- average value seen
latency.stdev            -- standard deviation
latency:percentile(99.0) -- 99th percentile value
latency(i)               -- raw value and count

summary = {
  duration = n,  -- run duration in microseconds
  requests = n,  -- total completed requests
  bytes    = n,  -- total bytes received
  errors   = {
    connect = n, -- total socket connection errors
    read    = n, -- total socket read errors
    write   = n, -- total socket write errors
    status  = n, -- total http status codes > 399
    timeout = n  -- total request timeouts
  }
}

而官方的 脚本则是重载这些方法并使用的一个 demo:

-- example script that demonstrates use of setup() to pass
-- data to and from the threads

local counter = 1
local threads = {}

function setup(thread)
   thread:set("id", counter)
   table.insert(threads, thread)
   counter = counter + 1
end

function init(args)
   requests  = 0
   responses = 0

   local msg = "thread %d created"
   print(msg:format(id))
end

function request()
   requests = requests + 1
   return wrk.request()
end

function response(status, headers, body)
   responses = responses + 1
end

function done(summary, latency, requests)
   for index, thread in ipairs(threads) do
      local id        = thread:get("id")
      local requests  = thread:get("requests")
      local responses = thread:get("responses")
      local msg = "thread %d made %d requests and got %d responses"
      print(msg:format(id, requests, responses))
   end
end

6. 参考资料

wrk中的lua脚本:

http 性能测试 wrk使用教程:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网