Linux流量复制工具

对于一些有并发要求的业务,特别是对接外部流量时,产品上线前一定要做的就是压力测试,但是常规的压力测试并不能覆盖所有情况。以gemeter、ab,、webbench、http_load为例,这些通过模拟请求的压测工具,只能发送特定的参数,对于一些参数异常导致的程序处理异常是无法考虑到的,所以就需要一款能复制真实流量,并且不影响线上业务的工具。
流量复制工具有很多,例如Gor、tcpreplay、tcpcopy等,这些工具贴合真实场景,能模拟真实流量,并支持流量的放大或缩小,更容易测试出程序的瓶颈和潜在问题。

几款流量复制工具:

  1. gor: https://github.com/buger/goreplay
  2. tcpreplay: https://github.com/appneta/tcpreplay
  3. tcpcopy: https://github.com/session-replay-tools/tcpcopy
  4. 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 https://github.com/buger/goreplay

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



作者:JouyPub
链接:https://www.jianshu.com/p/03f74521c592
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值