php 压测流量回放,Web流量复制和压力测试工具Gor

Gor概述

Gor 是用 Golang 写的一个 HTTP 实时流量复制工具。只需要在 LB 或者 Varnish 入口服务器上执行一个进程,就可以把生产环境的流量复制到任何地方,比如 Staging 环境、Dev 环境。完美解决了 HTTP 层实时流量复制和压力测试的问题。

57e058ad4995?from=singlemessage

Image.png

Gor的功能

Gor 支持流量的放大和缩小、频率限制,这样不需要搭建和生产环境一致的服务器集群也可以正确测试。Gor 还支持根据正则表达式过滤流量,这意味着可以单独测试某个 API 服务。还可以修改 HTTP 请求头,比如替换 User-Agent, 或者增加某些 HTTP Header 。

Gor 还可以把请求记录到文件,以备回放和分析。Gor 支持和 ElasticSearch 集成,将流量存入 ES 进行实时分析。

Gor的安装

二进制版本

官方预编译版本使用比较简单,开箱即用。

通过以下地址下载最新版本

https://github.com/buger/gor/releases

Linux

$ wget https://github.com/buger/gor/releases/download/v0.12.1/gor_0.12.1_x64.tar.gz

$ tar xzvf gor_0.12.1_x64.tar.gz

$ cp gor /usr/local/bin

Mac

$ wget https://github.com/buger/gor/releases/download/v0.12.1/gor_0.12.1_mac.tar.gz

$ tar xzvf gor_0.12.1_mac.tar.gz

编译安装

搭建标准的Go语言环境,可参考http://golang.org/doc/code.html

并设置$GOPATH环境变量

获取源代码

$ go get github.com/buger/gor

编译

$ cd $GOPATH/src/github.com/buger/gor

$ go build

编译完成会产生一个gor二进制文件。

Gor实践

性能测试

可以将流量复制到文件,然后再对他们进行回放。回放的时候,流量会维持原始的时间间隔。如果你使用了百分比来进行速率限制,那么回放的速率会相应的增加或减少。有了这种速率限制,gor就可以用来进行压力测试。

#write to file

gor --input-raw :80 --output-file requests.log

#read from file

gor --input-file requests.gor --output-http"http://staging.com"

流量复制到文件

可以使用时间戳命名录制文件,默认情况下,文件是按“块”存储的,即文件大小到达上限后,添加后缀,并新建另一个文件,如下

gor ... --output-file %Y%m%d.log

#append false

20140608_0.log

20140608_1.log

20140609_0.log

20140609_1.log

默认是按“块”存储文件的方式,但是可以参数配置,--output-file-append,使用之后如下

gor ... --output-file %Y%m%d.log --output-file-append

#append true

20140608.log

20140609.log

时间格式化文件名的配置说明:

%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的。即按照录下来的流量的时间戳去进行回放。

# Replay from file on 2x speed

gor --input-file "requests.gor|200%" --output-http "staging.com"

上面的命令例子,是以2倍的速率回放。如果“input-flie”是多个文件,可以用正则去匹配,如“request*.gor|200%”

配合如下配置参数,可以更好的体现压力测试的效果。

--input-file-loop 重复循环执行input-file

--exit-after 30s 在30s后停止,可以控制压力测试的时间。分钟的单位是m

性能测试是gor比较常见的用途,其他常用命令参考“Gor常见命令”

Gor常用命令

简单的HTTP流量复制

$ gor --input-raw :80 --output-http "http://staging.com"

HTTP流量复制频率控制(获取每秒超过10个请求)

$ gor --input-tcp :28020 --output-http "http://staging.com|10"

HTTP流量复制缩小

$ gor --input-raw :80 --output-tcp "replay.local:28020|10%"

HTTP流量记录到本地文件

$ gor --input-raw :80 --output-file requests.gor

HTTP流量回放和压测

$ gor --input-file "requests.gor|200%" --output-http "staging.com"

HTTP流量过滤复制

$ gor --input-raw :8080 --output-http staging.com --output-http-url-regexp ^www.

自定义一些流量复制的参数

$ gor --input-raw :80 --output-http 192.168.2.6:8000 --http-allow-method POST --http-set-header 'User-Agent: Gor' -output-http-workers=1 -http-allow-url test.php

将流量复制两份到不同的测试服务

$ gor --input-tcp :28020 --output-http "http://staging.com" --output-http "http://dev.com"

将流量像负载均衡一样分配到不同的服务器

$ gor --input-tcp :28020 --output-http "http://staging.com" --output-http "http://dev.com" --split-output true

Gor配置参数

$ gor --help

-cpuprofile string

write cpu profile to file

-debug verbose

打开debug模式,显示所有接口的流量

-http-allow-header value

用一个正则表达式来匹配http头部,如果请求的头部没有匹配上,则被拒绝

gor --input-raw :8080 --output-http staging.com --http-allow-header api-version:^v1 (default [])

-http-allow-method value

类似于一个白名单机制来允许通过的http请求方法,除此之外的方法都被拒绝.

gor --input-raw :8080 --output-http staging.com --http-allow-method GET --http-allow-method OPTIONS (default [])

-http-allow-url value

一个正则表达式用来匹配url, 用来过滤完全匹配的的url,在此之外的都被过滤掉

gor --input-raw :8080 --output-http staging.com --http-allow-url ^www. (default [])

-http-disallow-header value

用一个正则表达式来匹配http头部,匹配到的请求会被拒绝掉

gor --input-raw :8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor" (default [])

-http-disallow-url value

用一个正则表达式来匹配url,如果请求匹配上了,则会被拒绝

gor --input-raw :8080 --output-http staging.com --http-disallow-url ^www. (default [])

-http-header-limiter value

读取请求,基于FNV32-1A散列来拒绝一定比例的特殊请求

gor --input-raw :8080 --output-http staging.com --http-header-imiter user-id:25% (default [])

-http-original-host

在--output-http的输出中,通常gor会使用取代请求的http头,所以应该禁用该选项,保留原始的主机头

-http-param-limiter value

Takes a fraction of requests, consistently taking or rejecting a request based on the FNV32-1A hash of a specific GET param:

gor --input-raw :8080 --output-http staging.com --http-param-limiter user_id:25% (default [])

-http-rewrite-url value

Rewrite the request url based on a mapping:

gor --input-raw :8080 --output-http staging.com --http-rewrite-url /v1/user/([^\/]+)/ping:/v2/user/$1/ping (default [])

-http-set-header value

Inject additional headers to http reqest:

gor --input-raw :8080 --output-http staging.com --http-set-header 'User-Agent: Gor' (default [])

-http-set-param value

Set request url param, if param already exists it will be overwritten:

gor --input-raw :8080 --output-http staging.com --http-set-param api_key=1 (default [])

-input-dummy value

Used for testing outputs. Emits 'Get /' request every 1s (default [])

-input-file value

从一个文件中读取请求

gor --input-file ./requests.gor --output-http staging.com (default [])

-input-http value

从一个http接口读取请求

# Listen for http on 9000

gor --input-http :9000 --output-http staging.com (default [])

-input-raw value

Capture traffic from given port (use RAW sockets and require *sudo* access):

# Capture traffic from 8080 port

gor --input-raw :8080 --output-http staging.com (default [])

-input-tcp value

用来在多个gor之间流转流量

# Receive requests from other Gor instances on 28020 port, and redirect output to staging

gor --input-tcp :28020 --output-http staging.com (default [])

-memprofile string

write memory profile to this file

-middleware string

Used for modifying traffic using external command

-output-dummy value

用来测试输入,打印出接收的数据. (default [])

-output-file value

把进入的请求写入一个文件中

gor --input-raw :80 --output-file ./requests.gor (default [])

-output-http value

转发进入的请求到一个http地址上

# Redirect all incoming requests to staging.com address

gor --input-raw :80 --output-http http://staging.com (default [])

-output-http-elasticsearch string

把请求和响应状态发送到ElasticSearch:

gor --input-raw :8080 --output-http staging.com --output-http-elasticsearch 'es_host:api_port/index_name'

-output-http-redirects int

设置多少次重定向被允许

-output-http-stats

每5秒钟输出一次输出队列的状态

-output-http-timeout duration

指定http的request/response超时时间,默认是5秒

-output-http-workers int

gor默认是动态的扩展工作者数量,你也可以指定固定数量的工作者

-output-tcp value

用来在多个gor之间流转流量

# Listen for requests on 80 port and forward them to other Gor instance on 28020 port

gor --input-raw :80 --output-tcp replay.local:28020 (default [])

-output-tcp-stats

每5秒钟报告一次tcp输出队列的状态

-split-output true

By default each output gets same traffic. If set to true it splits traffic equally among all outputs.

-stats

打开输出队列的状态

-verbose

Turn on more verbose output

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值