golang轻量级Web应用负载测试工具插件Hey的使用
golang轻量级Web应用负载测试工具插件Hey的使用
Hey是一个小型程序,用于向Web应用程序发送负载进行测试。
安装
直接下载
- Linux 64位
- Mac 64位
- Windows 64位
包管理器
macOS用户可以使用Homebrew安装:
brew install hey
使用方法
Hey以指定的并发级别运行指定数量的请求并打印统计信息。它还支持HTTP2端点。
基本命令选项
Usage: hey [options...] <url>
Options:
-n 要运行的请求数。默认为200。
-c 并发运行的worker数。总请求数不能小于并发级别。默认为50。
-q 速率限制,每个worker每秒查询数(QPS)。默认为无限制。
-z 发送请求的持续时间。达到持续时间后,应用程序停止并退出。
如果指定了持续时间,则忽略n参数。例如:-z 10s -z 3m。
-o 输出类型。如果未提供,则打印摘要。
"csv"是唯一支持的替代方案。以逗号分隔值格式转储响应指标。
-m HTTP方法,GET、POST、PUT、DELETE、HEAD、OPTIONS之一。
-H 自定义HTTP头。可以通过重复标志指定多个。
例如:-H "Accept: text/html" -H "Content-Type: application/xml"
-t 每个请求的超时时间(秒)。默认为20,0表示无限。
-A HTTP Accept头。
-d HTTP请求体。
-D 来自文件的HTTP请求体。例如:/home/user/file.txt或./file.txt。
-T 内容类型,默认为"text/html"。
-a 基本认证,用户名:密码。
-x HTTP代理地址,格式为host:port。
-h2 启用HTTP/2。
-host HTTP Host头。
-disable-compression 禁用压缩。
-disable-keepalive 禁用keep-alive,防止在不同HTTP请求之间重用TCP连接。
-disable-redirects 禁用HTTP重定向跟随
-cpus 使用的CPU核心数。
(当前机器的默认值为8核)
示例Demo
基本GET请求测试
# 发送200个请求,并发50个worker
hey -n 200 -c 50 http://localhost:8080
带持续时间的测试
# 持续测试30秒,并发10个worker
hey -z 30s -c 10 http://localhost:8080/api
POST请求测试
# 发送1000个POST请求,并发100个worker,带JSON请求体
hey -n 1000 -c 100 -m POST -T "application/json" -d '{"key":"value"}' http://localhost:8080/api
带认证和自定义头的测试
# 带基本认证和自定义头的测试
hey -n 500 -c 20 -a "user:password" -H "X-API-Key: 12345" http://localhost:8080/secure
速率限制测试
# 限制每秒50个请求(QPS)
hey -n 1000 -c 10 -q 50 http://localhost:8080
HTTP/2测试
# 启用HTTP/2协议测试
hey -n 200 -c 20 -h2 https://localhost:8443
Hey原名boom,受Tarek Ziade的工具启发而开发。为避免名称冲突,现已更名为hey。
更多关于golang轻量级Web应用负载测试工具插件Hey的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于golang轻量级Web应用负载测试工具插件Hey的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Golang轻量级Web应用负载测试工具插件Hey的使用指南
Hey是一个用Go编写的轻量级HTTP负载测试工具,类似于Apache Bench (ab),但更加现代化和易用。下面我将详细介绍Hey的使用方法,并给出Go代码示例。
Hey的基本安装
go get -u github.com/rakyll/hey
或者直接下载预编译的二进制文件。
基本使用方法
hey -n 1000 -c 50 http://example.com
参数说明:
-n
: 总请求数-c
: 并发请求数-m
: HTTP方法 (GET, POST, PUT, DELETE等)-d
: POST请求体-H
: 添加HTTP头-t
: 单个请求超时时间(秒)
在Go代码中集成Hey
虽然Hey主要是命令行工具,但我们可以通过exec
包在Go程序中调用它:
package main
import (
"bytes"
"fmt"
"log"
"os/exec"
)
func runHeyTest(url string, requests, concurrency int) {
cmd := exec.Command("hey",
"-n", fmt.Sprintf("%d", requests),
"-c", fmt.Sprintf("%d", concurrency),
url)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
log.Fatalf("Hey测试失败: %v", err)
}
fmt.Println(out.String())
}
func main() {
runHeyTest("http://example.com", 1000, 50)
}
高级用法示例
1. POST请求测试
hey -n 500 -c 20 -m POST -d '{"username":"test","password":"123"}' -H "Content-Type: application/json" http://example.com/api/login
2. 带认证头的测试
hey -n 1000 -c 100 -H "Authorization: Bearer token123" http://example.com/api/protected
3. 输出结果到文件
hey -n 1000 -c 50 http://example.com > results.txt
解析Hey输出结果
Hey的输出包含以下关键指标:
- 总请求时间
- 每个请求的平均时间
- 最快/最慢请求时间
- 请求成功率
- 吞吐量(requests/sec)
自定义Hey的Go实现
如果你想在自己的Go项目中实现类似功能,可以参考以下简化版的负载测试代码:
package main
import (
"fmt"
"net/http"
"sync"
"time"
)
type Result struct {
Duration time.Duration
Err error
Status int
}
func worker(url string, wg *sync.WaitGroup, ch chan<- Result) {
defer wg.Done()
start := time.Now()
resp, err := http.Get(url)
duration := time.Since(start)
result := Result{Duration: duration, Err: err}
if resp != nil {
result.Status = resp.StatusCode
}
ch <- result
}
func runLoadTest(url string, requests, concurrency int) {
ch := make(chan Result, requests)
var wg sync.WaitGroup
start := time.Now()
for i := 0; i < requests; i++ {
wg.Add(1)
go worker(url, &wg, ch)
if i%concurrency == 0 && i != 0 {
time.Sleep(1 * time.Second) // 控制并发
}
}
wg.Wait()
close(ch)
totalTime := time.Since(start)
var totalDuration time.Duration
success, fail := 0, 0
for result := range ch {
totalDuration += result.Duration
if result.Err == nil && result.Status == http.StatusOK {
success++
} else {
fail++
}
}
avg := totalDuration / time.Duration(requests)
qps := float64(success) / totalTime.Seconds()
fmt.Printf("总请求数: %d\n", requests)
fmt.Printf("成功: %d\n", success)
fmt.Printf("失败: %d\n", fail)
fmt.Printf("总时间: %.2f秒\n", totalTime.Seconds())
fmt.Printf("平均响应时间: %.2f毫秒\n", float64(avg.Milliseconds()))
fmt.Printf("QPS: %.2f\n", qps)
}
func main() {
runLoadTest("http://example.com", 1000, 50)
}
Hey的优势
- 轻量级:单个二进制文件,无额外依赖
- 简单易用:命令行参数直观
- 跨平台:可在Windows、Linux、macOS上运行
- 性能良好:能产生较高的并发压力
注意事项
- 测试时先从低并发开始,逐步增加
- 避免对生产环境直接进行高压测试
- 注意测试机器的资源限制
- 长时间测试时监控测试机器的资源使用情况
Hey是一个简单但功能强大的工具,非常适合快速评估Web应用的性能表现。对于更复杂的场景,可以考虑使用Locust或JMeter等工具。