對于一些有并發要求的業務,特别是對接外部流量時,産品上線前一定要做的就是壓力測試,但是正常的壓力測試并不能覆寫所有情況。以gemeter、ab,、webbench、http_load為例,這些通過模拟請求的壓測工具,隻能發送特定的參數,對于一些參數異常導緻的程式處理異常是無法考慮到的,是以就需要一款能複制真實流量,并且不影響線上業務的工具。
流量複制工具有很多,例如Gor、tcpreplay、tcpcopy等,這些工具貼合真實場景,能模拟真實流量,并支援流量的放大或縮小,更容易測試出程式的瓶頸和潛在問題。
幾款流量複制工具:
- gor: https://github.com/buger/goreplay
- tcpreplay: https://github.com/appneta/tcpreplay
- tcpcopy: https://github.com/session-replay-tools/tcpcopy
- Nginx子產品ngx_http_mirror_module,在Nginx 1.13.4中開始引入,使用前請檢查nginx版本
Nginx子產品ngx_http_mirror_module
配置如下:
server {
listen 8080;
access_log /home/work/log/nginx/org.log;
root html/org;
}
server {
listen 8081;
access_log /home/work/log/nginx/mir.log ;
root html/mir;
}
upstream backend {
server 127.0.0.1:8080;
}
upstream test_backend {
server 127.0.0.1:8081;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
mirror /mirror;
proxy_pass http://backend;
}
location /mirror {
internal;
proxy_pass http://test_backend$request_uri;
}
}
流量放大,配置兩個mirror即可
location / {
mirror /mirror;
mirror /mirror;
proxy_pass http://backend;
}
使用是很友善,但是線上nginx一般都承載了不止一個業務,修改nginx配置後需要
nginx -s reload
來使之生效,這種操作線上上還是盡量需要避免的。
gor
Gor概述
Gor 是用 Golang 寫的一個 HTTP 實時流量複制工具。功能更強大,支援流量的放大、縮小,頻率限制,還支援把請求記錄到檔案,友善回放和分析,也支援和 ElasticSearch 內建,将流量存入 ES 進行實時分析。
下載下傳安裝,可以下載下傳編譯好的二進制檔案直接使用
> wget https://github.com/buger/goreplay/releases/download/v0.16.1/gor_0.16.1_x64.tar.gz
> tar xzvf gor_0.16.1_x64.tar.gz
流量複制到檔案
可以将流量複制到檔案,然後再對他們進行回放。回放的時候,流量會維持原始的時間間隔。如果你使用了百分比來進行速率限制,那麼回放的速率會相應的增加或減少。有了這種速率限制,gor就可以用來進行壓力測試。
#write to file
gor --input-raw :80 --output-file requests_origin.gor
#read from file
gor --input-file requests_origin.gor --output-http "http://localhost:8081"
可以使用時間戳命名錄制檔案,預設情況下,檔案是按“塊”存儲的,即檔案大小到達上限後,添加字尾,并建立另一個檔案,如下
gor --input-raw :80 --output-file %Y%m%d.gor
#append false
20140608_0.gor
20140608_1.gor
20140609_0.gor
20140609_1.gor
預設是按“塊”存儲檔案的方式,但是可以參數配置,--output-file-append,使用之後如下
gor --input-raw :80 --output-file %Y%m%d.gor --output-file-append
#append true
20140608.gor
20140609.gor
時間格式化檔案名的配置說明:
%Y: year including the century (at least 4 digits)
%m: month of the year (01..12)
%d: Day of the month (01..31)
%H: Hour of the day, 24-hour clock (00..23)
%M: Minute of the hour (00..59)
%S: Second of the minute (00..60)
預設格式是%Y%m%d%H
流量回放
目前,這種方式隻支援"input-file",而且隻能用百分比去控制回放速率。請注意,這個回放的速率比例是相對于input的。即按照錄下來的流量的時間戳去進行回放。
以2倍速率回放
gor --input-file "requests_origin.gor|200%" --output-http "http://localhost:8081"
如果“input-flie”是多個檔案,可以用正則去比對,
gor --input-file "requests_origin*.gor|200%" --output-http "http://localhost:8081"
配合如下配置參數,可以更好進行壓力測試
--input-file-loop
重複循環執行input-file
--exit-after 30s
在30s後停止,可以控制壓力測試的時間。分鐘的機關是m
Gor常用指令
簡單的HTTP流量複制
> gor --input-raw :80 --output-http "http://localhost:8081"
HTTP流量複制頻率控制(擷取每秒超過10個請求)
> gor --input-tcp :28020 --output-http "http://localhost:8081|10"
HTTP流量複制縮小
> gor --input-raw :80 --output-tcp "http://localhost:8081|10%"
HTTP流量記錄到本地檔案
> gor --input-raw :80 --output-file requests_origin.gor
HTTP流量回放和壓測
> gor --input-file "requests_origin.gor|200%" --output-http "http://localhost:8081"
HTTP流量過濾複制
> gor --input-raw :8080 --output-http http://localhost:8081 --output-http-url-regexp ^www.
自定義一些流量複制的參數
> gor --input-raw :80 --output-http http://localhost:8081 --http-allow-method POST --http-set-header 'User-Agent: Gor' -output-http-workers=1 -http-allow-url test.php
将流量複制兩份到不同的測試服務
> gor --input-tcp :8080 --output-http "http://localhost:8081" --output-http "http://localhost:8082"
将流量像負載均衡一樣配置設定到不同的伺服器
> gor --input-tcp :8080 --output-http "http://localhost:8081" --output-http "http://localhost:8082" --split-output true
Gor配置參數
> gor --help
-http-allow-header value
gor --input-raw :8080 --output-http localhost:8081 --http-allow-header api-version:v1.1
用一個正規表達式來比對http頭部,如果請求的頭部沒有比對上,則被拒絕
-http-allow-method value
gor --input-raw :8080 --output-http localhost:8081 --http-allow-method GET
類似于一個白名單機制來允許通過的http請求方法,除此之外的方法都被拒絕.
-http-allow-url value
gor --input-raw :8080 --output-http localhost:8081 --http-allow-url ^www
一個正規表達式用來比對url, 用來過濾完全比對的的url,在此之外的都被過濾掉
-http-disallow-header value
gor --input-raw :8080 --output-http localhost:8081 --http-disallow-header "User-Agent: Replayed by Gor"
用一個正規表達式來比對http頭部,比對到的請求會被拒絕掉
-http-disallow-url value
gor --input-raw :8080 --output-http localhost:8081 --http-disallow-url ^www
用一個正規表達式來比對url,如果請求比對上了,則會被拒絕
-http-set-header value
gor --input-raw :8080 --output-http localhost:8081 --http-set-header 'User-Agent: Gor'
設定頭資訊,如果已經存在會覆寫
-http-set-param value
gor --input-raw :8080 --output-http localhost:8081 --http-set-param api_key=v1.1
設定請求參數,如果已經存在會覆寫
更多參數請查閱官方文檔 https://github.com/buger/goreplay/wiki