HTTP pressure measurement Tool wrk User Guide

Source: Internet
Author: User
Tags epoll lua git clone

Used a lot of pressure measurement tools, but has not found the favorite. Recently tried wrk feel good, write down the use of the Guide to their own memo, if can help you, that is also very good.

Installation

WRK supports most Unix-like systems and does not support Windows. Operating systems are required to support Luajit and OpenSSL, but don't worry, most Unix-like systems are supported. Installing the WRK is as simple as downloading the wrk source from GitHub and executing the make command under the project path.

git clone https://github.com/wg/wrkmake

After make, the executable file wrk is generated under the project path, which can then be used for HTTP pressure measurement. You can copy this executable to a path already in path, such as/usr/local/bin, so that you can use wrk directly on any path.

By default WRK uses its own luajit and OpenSSL, and if you want to use the installed version of the system, you can use both the With_luajit and WITH_OPENSSL options to specify their paths. Like what:

make WITH_LUAJIT=/usr WITH_OPENSSL=/usr
Basic use
    1. Command line knock down the wrk, you can see the use of Help
Usage: wrk <options> <url>                              Options:                                                -c, --connections <N>  Connections to keep open       -d, --duration    <T>  Duration of test               -t, --threads     <N>  Number of threads to use                                                             -s, --script      <S>  Load Lua script file           -H, --header      <H>  Add header to request              --latency          Print latency statistics           --timeout     <T>  Socket/request timeout         -v, --version          Print version details                                                              Numeric arguments may include a SI unit (1k, 1M, 1G)  Time arguments may include a time unit (2s, 2m, 2h)

Simply turn into Chinese:

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

See it is 4.0.2 version of WRK, using Epoll. This means that we can use a small number of threads to create a large number of connections to the service under test and perform a pressure test.

    1. Do a simple pressure test, analyze the results
wrk -t8 -c200 -d30s --latency  "http://www.bing.com"输出:Running 30s test @ http://www.bing.com  8 threads and 200 connections  Thread Stats   Avg      Stdev     Max   +/- Stdev    Latency    46.67ms  215.38ms   1.67s    95.59%    Req/Sec     7.91k     1.15k   10.26k    70.77%  Latency Distribution     50%    2.93ms     75%    3.78ms     90%    4.73ms     99%    1.35s   1790465 requests in 30.01s, 684.08MB readRequests/sec:  59658.29Transfer/sec:     22.79MB

The above uses 8 threads 200 connections, the Bing home page for 30 seconds of the pressure measurement, and required in the test results to output response delay information. The following is a simple comment on the test results:

Running 30s test @ http://www.bing.com (压测时间30s)  8 threads and 200 connections (共8个测试线程,200个连接)  Thread Stats   Avg      Stdev     Max   +/- Stdev              (平均值) (标准差)(最大值)(正负一个标准差所占比例)    Latency    46.67ms  215.38ms   1.67s    95.59%    (延迟)    Req/Sec     7.91k     1.15k   10.26k    70.77%    (处理中的请求数)  Latency Distribution (延迟分布)     50%    2.93ms     75%    3.78ms     90%    4.73ms     99%    1.35s (99分位的延迟)  1790465 requests in 30.01s, 684.08MB read (30.01秒内共处理完成了1790465个请求,读取了684.08MB数据)Requests/sec:  59658.29 (平均每秒处理完成59658.29个请求)Transfer/sec:     22.79MB (平均每秒读取数据22.79MB)

As you can see, the wrk is easy to use and results clear. And because of the use of non-blocking IO, can create a large number of connections on the common test machine, so as to achieve better pressure measurement results.

Personalize wrk with Lua scripting

The above two sections are installed and simply use the WRK, but this simple pressure test may not meet our needs. For example, we may need to use the Post method to interact with the server, and may need to use different parameters for each request to better simulate the actual usage scenarios of the service. WRK enables users to customize the pressure measurement process by using--script to specify LUA scripts to meet individual requirements.

    1. Introducing WRK support for LUA scripting

WRK supports the personalization of the pressure measurement in three stages, namely the start-up phase, the operation phase and the end stage. Each test thread has a separate LUA runtime environment.

Start-up phase
function setup(thread)

Implementing the Setup method in a script file, Wrk calls the method when the test thread has been initialized but has not yet started. WRK invokes the Setup method for each test thread and passes in the object thread that represents the test thread as a parameter. The Setup method can manipulate the thread object, get information, store information, and even close the thread.

thread.addr             - get or set the thread‘s server addressthread:get(name)        - get the value of a global in the thread‘s envthread:set(name, value) - set the value of a global in the thread‘s envthread:stop()           - stop the thread
Run phase
function init(args)function delay()function request()function response(status, headers, body)

Init is called by the test thread and is called only once when it enters the run phase. Support for command-line arguments from the start wrk command;
Delay is called every time the request is sent, and if delay is required, the delay is the corresponding time;
Request is used to generate requests, and each request invokes the method, so be careful not to take time-consuming actions in the method;
Reponse is called every time a response is received; To improve performance, if the method is not defined, then WRK will not parse headers and body;

End Stage
function done(summary, latency, requests)

This method is called only once during the entire test, and can be obtained from the object given by the parameter to generate a customized test report.

Variables and methods that can be accessed in custom scripts

Variable: wrk

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

A table-type variable, WRK, is a global variable that modifies the table, affecting all requests.

Method: Wrk.fomat wrk.lookup Wrk.connect

  function wrk.format(method, path, headers, body)    wrk.format returns a HTTP request string containing the passed parameters    merged with values from the wrk table.    根据参数和全局变量wrk,生成一个HTTP rquest string。  function wrk.lookup(host, service)    wrk.lookup returns a table containing all known addresses for the host    and service pair. This corresponds to the POSIX getaddrinfo() function.    给定host和service(port/well known service name),返回所有可用的服务器地址信息。  function wrk.connect(addr)    wrk.connect returns true if the address can be connected to, otherwise    it returns false. The address must be one returned from wrk.lookup().    测试与给定的服务器地址信息是否可以成功创建连接
    1. Example
Using the Post METHOD
wrk.method = "POST"wrk.body   = "foo=bar&baz=quux"wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"

By modifying the global variable WRK, all requests use the Post method, and the body and Content-type headers are specified.

Change one parameter for each request
request = function()   uid = math.random(1, 10000000)   path = "/test?uid=" .. uid   return wrk.format(nil, path)end

The UID parameters in the request are randomly generated by randomly generating the UID between the 1~10000000 in the requests method.

10ms delay before each request
function delay()   return 10end
Each thread must be certified before it is authenticated and then the token is obtained for pressure measurement.
token = nilpath  = "/authenticate"request = function()   return wrk.format("GET", path)endresponse = function(status, headers, body)   if not token and status == 200 then      token = headers["X-Token"]      path  = "/resource"      wrk.headers["X-Token"] = token   endend

In the absence of tokens, first access to/authenticate certification. After successful authentication, read token and replace path as/resource.

Support for HTTP pipeline service with pressure measurement
init = function(args)   local r = {}   r[1] = wrk.format(nil, "/?foo")   r[2] = wrk.format(nil, "/?bar")   r[3] = wrk.format(nil, "/?baz")   req = table.concat(r)endrequest = function()   return reqend

By stitching together three HTTP request requests in the Init method, a three request is sent each time to use HTTP pipeline.

At last

The source code is very concise, simple read read, very admire the author of Wrk.

HTTP pressure measurement Tool wrk User Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.