天天看點

wrk 壓力測試

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
     %  474.09ms
     %  651.11ms
     %  902.69ms
     %    1.46s           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

-s : lua(script)

result

Running s test @ http://www.baidu.com
   threads and  connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   ms  ms   s    %
    Req/Sec                   %
  Latency Distribution
     %  ms
     %  ms
     %  ms
     %    s
   requests in s, MB read
  Socket errors: connect , read , write , timeout 
Requests/sec:    
Transfer/sec:      MB           
  • 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 =   
local threads = {}  

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

function init(args)  
   requests  =   
   responses =   

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

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

function response(status, headers, body)  
   responses = responses +   
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/bash

head="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)"

## threadPoolSize
threadPool=
## 壓測持續時間,機關s、m
duration=s
## 每輪壓測結束後休息時間
sleepTime=s
## 壓測路徑URL
url="http://localhost:38290/rpc" 

## 間隔遞增并發數
concurrentInterval=
## 停止壓測并發數,超過此值停止壓測
stopConcurrent=
# 并發計數器
i=

echo $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 $sleepTime
done           
  • 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)
end

function init(args)
    for k, v in pairs(args) do
        --print(k, v)
    end
end

function 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/ .. "," .. latency.max/ .. "," .. latency.mean/ .. "," .. latency.stdev/ .. "," .. latency:percentile()/ .. "," .. latency:percentile()/ .. "," .. latency:percentile()/ .. "," .. latency:percentile()/ .. "," .. summary.duration/ .. "," .. summary.requests .. "," .. summary.bytes .. "," .. summary.errors.connect.. "," .. summary.errors.read.. "," .. summary.errors.write.. "," .. summary.errors.status.. "," .. summary.errors.timeout .. "," .. summary.requests/(summary.duration/) .. "," .. (summary.bytes/)/(summary.duration/))
end

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



function request()
    path = paths[counter]
    counter = counter + 
    if counter > #paths then
      counter = 
    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)
end

counter = 
math.randomseed(os.time())
math.random(); math.random(); math.random()

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")           
  • 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
wrk 壓力測試
  • 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":{}}
           

繼續閱讀