电商网站的数据库设计,wordpress主题下载oa,国内最好景观设计公司排名,杏林建设网站0. 简要介绍WRK 是一款轻量且易用的 HTTP 压力测试工具#xff0c;通过该工具我们可以方便地对我们所开发的 WebAPI 项目进行压力测试#xff0c;并且针对测试的情况返回结果。PS#xff1a;Wrk 并不能针对测试的结果生成动态的图表#xff0c;如果有这种需要#xff0c;可… 0. 简要介绍WRK 是一款轻量且易用的 HTTP 压力测试工具通过该工具我们可以方便地对我们所开发的 WebAPI 项目进行压力测试并且针对测试的情况返回结果。PSWrk 并不能针对测试的结果生成动态的图表如果有这种需要可以尝试使用另一款工具 Vegeta。该项目使用的 Golang 进行编写其 GitHub 地址为https://github.com/tsenart/vegeta下面的内容就是一个标准的测试结果信息Copy# 针对 127.0.0.1:8080 进行压力测试wrk -t12 -c400 -d30s http://127.0.0.1:8080/index.htmlCopy# 这里是测试结果Running 30s test http://127.0.0.1:8080/index.html 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.33MB1. 安装关于 OS X 与 Windows 的安装可以参考 Wrk 官方 WIKI 进行操作本文主要讲解一下 CentOS 7.x 下如果进行编译。Copysudo 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. 命令说明Copy./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 1s3. 开始压力测试执行了上述代码之后我们可以看到很直观的信息第一个就是 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 上面其地址为https://github.com/wg/wrk/tree/master/scripts。这里我们以实现 POST 请求为例Copywrk.method POSTwrk.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 个阶段setup、running、done每个 WRK 线程中都有独立的脚本环境。5.1 WRK 的全局属性Copywrk { scheme http, host localhost, port nil, method GET, path /, headers {}, body nil, thread userdata, }5.2 WRK 的全局方法Copy-- 生成整个request的string例如返回-- GET / HTTP/1.1-- Host: tool.lufunction 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 或 falsefunction wrk.connect(addr)5.3 Setup 阶段setup() 方法是在线程创建之后启动之前。Copyfunction setup(thread)-- thread提供了1个属性3个方法-- thread.addr 设置请求需要打到的ip-- thread:get(name) 获取线程全局变量-- thread:set(name, value) 设置线程全局变量-- thread:stop() 终止线程5.4 Running 阶段Copyfunction init(args)-- 每个线程仅调用1次args 用于获取命令行中传入的参数, 例如 --envprefunction delay()-- 每个线程调用多次发送下一个请求之前的延迟, 单位为msfunction request()-- 每个线程调用多次返回http请求function response(status, headers, body)-- 每个线程调用多次返回http响应5.5 Done 阶段可以用于自定义结果报表整个过程中只执行一次。Copyfunction done(summary, latency, requests)latency.min -- minimum value seenlatency.max -- maximum value seenlatency.mean -- average value seenlatency.stdev -- standard deviationlatency:percentile(99.0) -- 99th percentile valuelatency(i) -- raw value and countsummary { 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 } }而官方的 setup.lua 脚本则是重载这些方法并使用的一个 DEMOCopy-- example script that demonstrates use of setup() to pass-- data to and from the threadslocal counter 1local threads {}function setup(thread) thread:set(id, counter) table.insert(threads, thread) counter counter 1endfunction init(args) requests 0 responses 0 local msg thread %d created print(msg:format(id))endfunction request() requests requests 1 return wrk.request()endfunction response(status, headers, body) responses responses 1endfunction 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)) endend6. 参考资料wrk中的lua脚本https://type.so/linux/lua-script-in-wrk.htmlhttp 性能测试 wrk使用教程https://juejin.im/post/5a59e74f5188257353008fea原文地址:https://www.cnblogs.com/myzony/p/9798116.html.NET社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com