码迷,mamicode.com
首页 > 系统相关 > 详细

linux下wrk的安装

时间:2019-03-24 13:49:45      阅读:1037      评论:0      收藏:0      [点我收藏+]

标签:时间   version   阶段   ati   tran   介绍   nbsp   文件中   mac   

wrk是linux下开源的性能测试工具,并且只能在linux下运行,下面介绍下安装教程(以ubantu18.04环境为例):

1.预先安装git,如:apt install git

2.从git上拉取wrk:git clone https://github.com/wg/wrk.git wrk

3.cd wrk

4.apt-get install build-essential -y

5.make

6.把生成的 wrk 移到一个 PATH 目录下面, 比如 : sudo cp wrk /usr/local/bin

注意编译的时间可能比较长,耐心等待即可

CentOs/RedHat/Fedora 下的安装:

  1. sudo yum groupinstall ‘Development Tools‘
  2. sudo yum install openssl-devel
  3.  sudo yum install git
  4.  git clone https://github.com/wg/wrk.git wrk
  5.  cd wrk
  6.  make
  7.  # 把生成的 wrk 移到一个 PATH 目录下面, 比如
  8.  sudo cp wrk /usr/local/bin

mac 下快捷安装:

        brew install wrk

 

基本使用:

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

使用 12 个线程运行 30 秒, 400 个 http 并发

命令行选项

  1. 使用方法: wrk <选项> <被测HTTP服务的URL>
  2.  Options:
  3.  -c, --connections <N> 跟服务器建立并保持的 TCP 连接数量
  4.  -d, --duration <T> 压测时间
  5.  -t, --threads <N> 使用多少个线程进行压测
  6.  -s, --script <S> 指定 Lua 脚本路径
  7.  -H, --header <H> 为每一个 HTTP 请求添加 HTTP 头
  8.  --latency 在压测结束后,打印延迟统计信息
  9.  --timeout <T> 超时时间
  10.  -v, --version 打印正在使用的 wrk 的详细版本信息 
  11.  
  12.  <N>代表数字参数,支持国际单位 (1k, 1M, 1G)
  13.  <T>代表时间参数,支持时间单位 (2s, 2m, 2h)

做一次简单压测,分析下结果:

  1. wrk -t8 -c200 -d30s --latency "http://www.bing.com"
  2.   
  3.  输出:
  4.  Running 30s test @ http://www.bing.com
  5.  8 threads and 200 connections
  6.  Thread Stats Avg Stdev Max +/- Stdev
  7.  Latency 46.67ms 215.38ms 1.67s 95.59%
  8.  Req/Sec 7.91k 1.15k 10.26k 70.77%
  9.  Latency Distribution
  10.  50% 2.93ms
  11.  75% 3.78ms
  12.  90% 4.73ms
  13.  99% 1.35s
  14.  1790465 requests in 30.01s, 684.08MB read
  15.  Requests/sec: 59658.29
  16.  Transfer/sec: 22.79MB
以上使用 8 个线程 200 个连接,对 bing 首页进行了 30 秒的压测,并要求在压测结果中输出响应延迟信息。以下对压测结果进行简单注释:

    1. Running 30s test @ http://www.bing.com (压测时间30s)
    2.  8 threads and 200 connections (共8个测试线程,200个连接)
    3.  Thread Stats Avg Stdev Max +/- Stdev
    4.  (平均值) (标准差)(最大值)(正负一个标准差所占比例)
    5.  Latency 46.67ms 215.38ms 1.67s 95.59%
    6.  (延迟)
    7.  Req/Sec 7.91k 1.15k 10.26k 70.77%
    8.  (处理中的请求数)
    9.  Latency Distribution (延迟分布)
    10.  50% 2.93ms
    11.  75% 3.78ms
    12.  90% 4.73ms
    13.  99% 1.35s (99分位的延迟)
    14.  1790465 requests in 30.01s, 684.08MB read (30.01秒内共处理完成了1790465个请求,读取了684.08MB数据)
    15.  Requests/sec: 59658.29 (平均每秒处理完成59658.29个请求)
    16.  Transfer/sec: 22.79MB (平均每秒读取数据22.79MB)
可以看到,wrk 使用方便,结果清晰。并且因为非阻塞 IO 的使用,可以在普通的测试机上创建出大量的连接,从而达到较好的压测效果。

lua脚本压测

在基本压测中, 每次发送的请求都是一样的,很多时候我们压测的请求体是每个请求都不一样, 这时候就要写lua基本来压测

使用 POST 方法压测

    1. wrk.method = "POST"
    2.  wrk.body = "foo=bar&baz=quux"
    3.  wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"

wrk -t2 -d30s -c1k -s xxx.lua http://192.168.17.1/

每个 request 的参数都不一样

  1. request = function()
  2.  uid = math.random(1, 10000000)
  3.  path = "/test?uid=" .. uid
  4.  return wrk.format(nil, path)
  5.  end

解释一下 wrk.format 这个函数

  1. wrk.format 这个函数的作用,根据参数和全局变量 wrk 生成一个 http 请求
  2.  函数签名: function wrk.format(method, path, headers, body)
  3.  method:http方法,比如GET/POST等
  4.  path: url上的路径(含函数)
  5.  headers: http header
  6.  body: http body

每个线程先登录然后压测

  1. token = nil
  2.  path = "/authenticate"
  3.   
  4.  request = function()
  5.  return wrk.format("GET", path)
  6.  end
  7.  
     
  8.  response = function(status, headers, body)
  9.  if not token and status == 200 then
  10.  token = headers["X-Token"]
  11.  path = "/resource"
  12.  wrk.headers["X-Token"] = token
  13.  end
  14.  end

发送 json

  1. request = function()
  2.  local headers = { }
  3.  headers[‘Content-Type‘] = "application/json"
  4.  body = {
  5.  mobile={"1533899828"},
  6.  params={code=math.random(1000,9999)}
  7.  }
  8.  
  9.  local cjson = require("cjson")
  10.  body_str = cjson.encode(body)
  11.  return wrk.format(‘POST‘, nil, headers, body_str)
  12.  end

若运行的时候报错找不到cjson, 可以安装 luarocks install lua-cjson

wrk lua脚本说明

wrk 压测脚本有3个生命周期, 分别是 启动阶段,运行阶段和结束阶段,每个线程都有自己的lua运行环境

技术图片

启动阶段

  1. function setup(thread)
  2.  在脚本文件中实现setup方法,wrk就会在测试线程已经初始化但还没有启动的时候调用该方法。wrk会为每一个测试线程调用一次setup方法,并传入代表测试线程的对象thread作为参数。setup方法中可操作该thread对象,获取信息、存储信息、甚至关闭该线程。
  3.  -- thread提供了1个属性,3个方法
  4.  -- thread.addr 设置请求需要打到的ip
  5.  -- thread:get(name) 获取线程全局变量
  6.  -- thread:set(name, value) 设置线程全局变量
  7.  -- thread:stop() 终止线程

运行阶段

  1. function init(args)
  2.  -- 每个线程仅调用1次,args 用于获取命令行中传入的参数, 例如 --env=pre
  3.  
  4.  function delay()
  5.  -- 每次请求调用1次,发送下一个请求之前的延迟, 单位为ms
  6.   
  7.  function request()
  8.  -- 每次请求调用1次,返回http请求
  9.   
  10.  function response(status, headers, body)
  11.  -- 每次请求调用1次,返回 http 响应
init由测试线程调用,只会在进入运行阶段时,调用一次。支持从启动wrk的命令中,获取命令行参数;
delay在每次发送request之前调用,如果需要delay,那么delay相应时间; request用来生成请求;
每一次请求都会调用该方法,所以注意不要在该方法中做耗时的操作; reponse在每次收到一个响应时调用;
为提升性能,如果没有定义该方法,那么wrk不会解析headers和body; 结束阶段

结束阶段

  1. function done(summary, latency, requests)
  2.   
  3.  latency.min -- minimum value seen
  4.  latency.max -- maximum value seen
  5.  latency.mean -- average value seen
  6.  latency.stdev -- standard deviation
  7.  latency:percentile(99.0) -- 99th percentile value
  8.  latency(i) -- raw value and count
  9.  
     
  10.  summary = {
  11.  duration = N, -- run duration in microseconds
  12.  requests = N, -- total completed requests
  13.  bytes = N, -- total bytes received
  14.  errors = {
  15.  connect = N, -- total socket connection errors
  16.  read = N, -- total socket read errors
  17.  write = N, -- total socket write errors
  18.  status = N, -- total HTTP status codes > 399
  19.  timeout = N -- total request timeouts
  20.  }
  21.  }

该方法在整个测试过程中只会调用一次,可从参数给定的对象中,获取压测结果,生成定制化的测试报告。

 

线程变量

  1. wrk = {
  2.  scheme = "http",
  3.  host = "localhost",
  4.  port = nil,
  5.  method = "GET",
  6.  path = "/",
  7.  headers = {},
  8.  body = nil,
  9.  thread = <userdata>,
  10.  }
  11.  
  12.  -- 生成整个request的string,例如:返回
  13.  -- GET / HTTP/1.1
  14.  -- Host: tool.lu
  15.  function wrk.format(method, path, headers, body)
  16.  -- method: http方法, 如GET/POST/DELETE 等
  17.  -- path: url的路径, 如 /index, /index?a=b&c=d
  18.  -- headers: 一个header的table
  19.  -- body: 一个http body, 字符串类型
  20.   
  21.  -- 获取域名的IP和端口,返回table,例如:返回 `{127.0.0.1:80}`
  22.  function wrk.lookup(host, service)
  23.  -- host:一个主机名或者地址串(IPv4的点分十进制串或者IPv6的16进制串)
  24.  -- service:服务名可以是十进制的端口号,也可以是已定义的服务名称,如ftp、http等
  25.  
  26.  -- 判断addr是否能连接,例如:`127.0.0.1:80`,返回 true 或 false
  27.  function wrk.connect(addr)


linux下wrk的安装

标签:时间   version   阶段   ati   tran   介绍   nbsp   文件中   mac   

原文地址:https://www.cnblogs.com/maxwellsky/p/10587794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!