What is a Vegeta

Vegeta is a versatile HTTP load testing tool written in the Go language. It provides command line tools and a development library.

The official address: https://github.com/tsenart/vegeta

Install Vegeta

Vegeta installation is very simple. Due to the excellent cross-platform nature of Go, you can download the official pre-compiled version directly out of the box.

  • Precompiled version

The Linux version is used as an example:

$$tar XZVF wget HTTP: / / https://github.com/tsenart/vegeta/releases/download/v7.0.3/vegeta-7.0.3-linux-amd64.tar.gz Vegeta-7.0.3-linux-amd64.tar. gz $mv /usr/local/bin/Copy the code

If you are using another platform, you can download the corresponding version from the official website and decompress it to use it.

  • macOS

$ brew install vegetaCopy the code

Vegeta usage

$ vegeta --help Usage: vegeta [global flags] <command> [command flags]global flags: -cpus int Number of cpus used (4 by default) -profile string Specifies which profiler is enabled during execution, supporting CPU and heap. -version Prints the version and exits. Attack Command: -body String Specifies the contents of the request body file. -cert string Specifies the client certificate file in PEM format for HTTPS requests. If -key is not specified, it is set to the value of the flag. -connections int Specifies the maximum number of open free connections for each target host. The default value is 10000. -duration duration Specifies the duration for sending requests to the destination host. The value 0 indicates permanent. -header value Specifies the request header of the target. You can specify multiple headers repeatedly. -http2 Specifies whether to send HTTP/2 requests to supported servers. The default value is true. -insecure Specifies whether invalid server TLS certificates are ignored. -keepalive Specifies whether to use persistent links. The default value is true. -key string Specifies the private key file of the SSL client certificate with PEM encoding used in THE HTTPS request. -laddr value Specifies the local IP address to be used. The default value is 0.0.0.0. -lazy Specifies whether to use the lazy mode to read the target. -output string Specifies the location of the output file. The default value is standard output. -rate uint Specifies the number of requests sent to the target per second. The default value is 50. -redirects int Specifies the maximum number of redirects for each request. The default value is 10. When the value is -1, the redirect is not followed but the response is marked as success. -root-certs value Specifies the trusted TLS root certificate files. If multiple root certificate files are used, separate them with commas (,). If this parameter is not specified, the default CA certificate is used. - Targets string Specifies the target file. The default value is standard. -timeout duration Specifies the timeout duration of each request. The default value is 30s. -workers uint Specifies the number of initialization processes. The default value is 10. Report Command: -Inputs string Specifies the report input file. The default value is standard input. -output string Specifies the report output file. The default value is standard output. - Reporter string Specifies the format of the report to be generated. The format can be text, JSON, plot, or hist[buckets]. The default is text. Dump command: -dumper string Indicates the file to be dumped. The file can be in JSON or CSV format. The default value is JSON. - Inputs string Indicates the input file to be saved. The default value is standard input. Multiple inputs are separated by commas. -output string Indicates the output file to be saved. The default value is standard output.Copy the code

“Vegeta” is used

Pressure measurement and report generation using standard input

$ echo "GET http://www.hi-linux.com/img/linux/docker-dry-01.png" | vegeta attack -rate=50 -connections=1 -duration=1s | Tee the bin | vegeta report Requests total rate, 50, Duration 51.02 [total, attack, wait] 37.41975091 s, Remote braking [mean, 50, 99, Max], remote braking [mean, 50, 99, Max], 37.204824958s, 37.319751221s Bytes In [total, mean] 9505000, 190100.00 Bytes Out [total, mean] 0, 0.00 Success [ratio] 100.00% Status Codes [code:count] 200:50 Error Set:Copy the code

Pressure test using the contents of the object file

  • Create a target. TXT file with the following contents:

$ vim target.txt
GET http://www.hi-linux.com/
GET http://www.hi-linux.com/posts/19779.html
GET http://www.hi-linux.com/img/linux/docker-dry-01.pngCopy the code
  • Use Vegeta for pressure measurement

$ vegeta attack -targets="target.txt" -rate=10 -duration=2s  > results.binCopy the code
  • Customize the target file

In addition to the simplest HTTP GET requests defined above, you can also define the following more flexible and complex HTTP requests:

1. Customize the request header

GET http://user:[email protected]:80/path/to
X-Account-ID: 8675309

DELETE http://hi-linux.com:80/path/to/remove
Confirmation-Token: 90215
Authorization: Token DEADBEEFCopy the code

2. Customize the request body

POST http://hi-linux.com:80/things
@/path/to/newthing.json

PATCH http://hi-linux.com:80/thing/71988591
@/path/to/thing-71988591.jsonCopy the code

3. Customize the request header and request body

POST http://hi-linux.com:80/things
X-Account-ID: 99
@/path/to/newthing.jsonCopy the code

Generate pressure test report

  • Generates a pressure test report in JSON format

$ vegeta report -inputs=results.bin -reporter=json > metrics.jsonCopy the code
  • Generate a Dygraphs HTML 5 pressure measurement report

$ cat results.bin | vegeta report -reporter=plot > plot.htmlCopy the code

  • Computes and prints a text-based histogram

$cat the bin | vegeta report - reporter = "hist [0, 2 ms, 4 ms, 6 ms]" Bucket # % Histogram [0, 2 ms] 32.65% 6007 # # # # # # # # # # # # # # # # # # # # # # # # [2 ms, 4 ms] 29.92% 5505 # # # # # # # # # # # # # # # # # # # # # # [4 ms, 6 ms] 11.51% 2117 # # # # # # # # 4771 25.93% [6 ms, + Inf] # # # # # # # # # # # # # # # # # # #Copy the code
  • Generate real-time graphical pressure survey report

If you are an iTerm user, you can use Jaggr to integrate Vegeta with Jplot and draw pressure measurement reports in real time on your terminal. To do this you need to install Jaggr and jplot first:

1. Install jaggr

# macOS $brew install rs/tap/jaggr $go get -u github.com/rs/jaggrCopy the code

2. Install jplot

# macOS $brew install rs/tap/jplot #Copy the code

After installing Jaggr and Jplot, next you need to execute the following command in iTerm:

$ echo 'GET http://www.hi-linux.com' | \ vegeta attack -rate 50 -duration 5m | vegeta dump | \ jaggr @count=rps \ Hist \ [100200300400500 \] : code \ p25, p50, p95: latency \ sum: bytes_in \ sum: bytes_out | \ jplot rps+code.hist.100+code.hist.200+code.hist.300+code.hist.400+code.hist.500 \ latency.p95+latency.p50+latency.p25 \ bytes_in.sum+bytes_out.sumCopy the code

Distributed stress testing

When carrying out large-scale load tests, it is usually impossible to achieve the expected results due to the performance bottlenecks of Vegeta’s own machine, such as the number of open files, memory size, CPU and network bandwidth. This problem can be solved by using Vegeta in distributed mode. There are many tools for implementing similar functions, such as the powerful Ansible. Here we use Pdsh to implement:

$PDSH - b - w '10.0.1.1, at address 10.0.2.1 10.0.3.1' \ 'echo "GET http://target/ |" vegeta attack - rate = 20000 - duration = 60 s > result.bin'Copy the code

The full name of Pdsh is (Parallel Distributed Shell), Pdsh can execute the operation of the target host in Parallel, which is very convenient to batch execute commands and distribute tasks. Pdsh also supports interactive mode. When the command to be executed is uncertain, you can directly enter the Pdsh command line, which is very convenient.

After completing the previous commands, we can use Shell scripts to gather the result files together for later report generation.

$for machine in "10.0.1.1 10.0.2.1 10.0.3.1"; do scp $machine:~/result.bin $machine.bin & doneCopy the code

Finally, we can generate the pressure test report by using the vegeta Report command. The vegeta Report command reads multiple result files separated by commas at one time and generates reports, sorted by timestamp by default.

$vegeta report - inputs = "10.0.1.1. Bin, at address 10.0.2.1. Bin, 10.0.3.1 bin" Requests, the total rate of 3600000, Remote braking [mean, 99, 99, Max] 326.913689 ms, 326.913689 ms, 326.913699 ms, 7.788103249 s Bytes In [total, Mean] 3714690, 3095.57 Bytes Out [total, mean] 0, 0.00 Success [ratio] 100.0% Status Codes [code:count] 200:3600000 Error Set:Copy the code

Other related

If you think using Vegeta on the command line is complicated, you can also use the Alex project. Alex is a pressure test Web UI based on Vegeta Library and Boom package. Vegeta provides stable QPS pressure sources, and Boom provides stable concurrency number pressure sources.

The address of the project: https://github.com/ireaderlab/alex

  • Alex architecture diagram

  • Alex rendering

Reference documentation

http://www.google.comhttp://t.cn/RBqHjIuhttp://t.cn/zQ104Zphttp://t.cn/R5spIPf

Today’s idea

Every person has two educations, one which he receives from others, and one, more important, which he gives himself.

— Edward Gibbon

More highlights:

  • Nginx Unit tutorial

  • Socat tutorial

  • MySQL User and role management

  • Configure multiple nics and gateways in Linux

  • Use Nginx’s Image_filter module to build a dynamic thumbnail server