wrk 压力测试

来源:互联网 发布:centos 自动获取ip 编辑:程序博客网 时间:2024/05/22 07:52

Install

brew install wrk

or

 git clone https://github.com/wg/wrk.git cd wrk make
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

Useway

wrk -t24 -c72 -d300s --latency -s api.lua http://vpcal-napos-miracle-keeper-1.vm.elenet.me:6830/invoke/
  • 1
  • 1

parameter

-t : thread

-c : connection

-d : during

—latency :

  Latency Distribution     50%  474.09ms     75%  651.11ms     90%  902.69ms     99%    1.46s
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

-s : lua(script)

result

Running 30s test @ http://www.baidu.com  12 threads and 100 connections  Thread Stats   Avg      Stdev     Max   +/- Stdev    Latency   525.47ms  271.92ms   2.00s    74.97%    Req/Sec    16.17      8.90    60.00     77.08%  Latency Distribution     50%  474.09ms     75%  651.11ms     90%  902.69ms     99%    1.46s  5499 requests in 30.09s, 80.66MB read  Socket errors: connect 0, read 2, write 0, timeout 3Requests/sec:    182.74Transfer/sec:      2.68MB
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Stdev : 标准差

Latency : 响应时间

Req/Sec : 请求数/thread*s

Lua

wrk.method = "POST" wrk.body = '{"skuId":1}' wrk.headers["Content-Type"] = "application/json"
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

Function

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  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

Instance

bash

#!/bin/bashhead="threadPool,concurrent,latency.min,latency.max,latency.mean,latency.stdev,upper_50,upper_90,upper_95,upper_99,duration,requests,bytes,errors.connect,errors.read,errors.write,errors.status,errors.timeout,qps,Transfer/sec(KB)"## threadPoolSizethreadPool=100## 压测持续时间,单位s、mduration=1s## 每轮压测结束后休息时间sleepTime=5s## 压测路径URLurl="http://localhost:38290/rpc" ## 间隔递增并发数concurrentInterval=12## 停止压测并发数,超过此值停止压测stopConcurrent=1000# 并发计数器i=0echo $head;while (($i <= $stopConcurrent))do    let i+=concurrentInterval    ret=`wrk -t12 -c$i -d$duration --latency -s post.lua $url | sed -n '$p'`    ret="$threadPool,$i,"$ret;    echo $ret;    sleep $sleepTimedone
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

post.lua

function response(status, headers, body)  --print(body)endfunction init(args)    for k, v in pairs(args) do        --print(k, v)    endendfunction done(summary, latency, requests)  print("latency.min,latency.max,latency.mean,latency.stdev,latency:percentile(50.0),latency:percentile(90.0),latency:percentile(95.0),latency:percentile(99.0),summary.duration,summary.requests,summary.bytes,summary.errors.connect,summary.errors.read,summary.errors.write,summary.errors.status,summary.errors.timeout,qps,Transfer/sec(KB)")  print(latency.min/1000 .. "," .. latency.max/1000 .. "," .. latency.mean/1000 .. "," .. latency.stdev/1000 .. "," .. latency:percentile(50.0)/1000 .. "," .. latency:percentile(90.0)/1000 .. "," .. latency:percentile(95.0)/1000 .. "," .. latency:percentile(99.0)/1000 .. "," .. summary.duration/1000000 .. "," .. summary.requests .. "," .. summary.bytes .. "," .. summary.errors.connect.. "," .. summary.errors.read.. "," .. summary.errors.write.. "," .. summary.errors.status.. "," .. summary.errors.timeout .. "," .. summary.requests/(summary.duration/1000000) .. "," .. (summary.bytes/1024)/(summary.duration/1000000))endfunction file_exists(file)  local f = io.open(file, "rb")  if f then f:close() end  return f ~= nilendfunction shuffle(paths)  local j, k  local n = #paths  for i = 1, n do    j, k = math.random(n), math.random(n)    paths[j], paths[k] = paths[k], paths[j]  end  return pathsendfunction 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 + 1] = line    end  end  return shuffle(lines)endfunction request()    path = paths[counter]    counter = counter + 1    if counter > #paths then      counter = 1    end    wrk.method = "POST"    wrk.body = path    wrk.headers["Content-Type"] = "application/json;charset=utf-8"    return wrk.format(wrk.method,nil,wrk.headers,wrk.body)endcounter = 1math.randomseed(os.time())math.random(); math.random(); math.random()paths = non_empty_lines_from("paths.txt")if #paths <= 0 then  print("multiplepaths: No paths found. You have to create a file paths.txt with one path per line")  os.exit()endprint("multiplepaths: Found " .. #paths .. " paths")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

paths.txt

{"ver":"1.0","soa":{"rpc":"1.2649","req":"86eca521-34ce-4f95-b114-ae966e2e04bc"},"iface":"me.ele.napos.order.proxy.api.TestService","method":"test","args":{},"metas":{}}{"ver":"1.0","soa":{"rpc":"1.2649","req":"86eca521-34ce-4f95-b114-ae966e2e04bc"},"iface":"me.ele.napos.order.proxy.api.QueryOrderService","method":"getUnprocessedOrders","args":{"restaurantId":"59968"},"metas":{}}
0 0
原创粉丝点击