golang跨平台HTTP性能测试工具插件库bombardier的使用
golang跨平台HTTP性能测试工具插件库bombardier的使用
bombardier是一个HTTP(S)基准测试工具。它使用Go编程语言编写,并采用了性能极佳的fasthttp库而不是Go默认的http库。
从bombardier v1.1版本开始,您可以测试HTTP/2.x服务或使用更符合RFC标准的HTTP客户端。
安装
您可以在发布页面获取二进制文件。或者,要获取最新版本,可以运行:
Go 1.18+:
go install github.com/codesenberg/bombardier@latest
使用
基本命令格式:
bombardier [<flags>] <url>
已知问题
据我所知,使用fasthttp无法正确传递Host头,您可以使用net/http(–http1/–http2标志)来解决此问题。
示例
示例1:基本测试
> bombardier -c 125 -n 10000000 http://localhost:8080
Bombarding http://localhost:8080 with 10000000 requests using 125 connections
10000000 / 10000000 [============================================] 100.00% 37s Done!
Statistics Avg Stdev Max
Reqs/sec 264560.00 10733.06 268434
Latency 471.00us 522.34us 51.00ms
HTTP codes:
1xx - 0, 2xx - 10000000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 292.92MB/s
示例2:真实服务器测试(带延迟分布)
> bombardier -c 200 -d 10s -l http://ya.ru
Bombarding http://ya.ru for 10s using 200 connections
[=========================================================================] 10s Done!
Statistics Avg Stdev Max
Reqs/sec 6607.00 524.56 7109
Latency 29.86ms 5.36ms 305.02ms
Latency Distribution
50% 28.00ms
75% 32.00ms
90% 34.00ms
99% 48.00ms
HTTP codes:
1xx - 0, 2xx - 0, 3xx - 66561, 4xx - 0, 5xx - 0
others - 5
Errors:
dialing to the given TCP address timed out - 5
Throughput: 3.06MB/s
代码示例
以下是一个简单的Go程序示例,展示如何使用bombardier库进行编程式测试:
package main
import (
"fmt"
"github.com/codesenberg/bombardier"
)
func main() {
// 创建配置
config := bombardier.Config{
NumConns: 125, // 并发连接数
NumReqs: nil, // 请求总数(nil表示不限制)
Duration: nil, // 持续时间(nil表示不限制)
Timeout: nil, // 超时时间
Method: "GET", // HTTP方法
URL: "http://localhost:8080", // 目标URL
Headers: nil, // 请求头
Body: nil, // 请求体
Latency: false, // 是否计算延迟分布
PrintProgress: true, // 是否打印进度
PrintResult: true, // 是否打印结果
}
// 运行测试
result, err := bombardier.Bombard(config)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// 打印结果
fmt.Printf("\nFinal Results:\n")
fmt.Printf("Requests per second: %.2f\n", result.ReqsPerSec)
fmt.Printf("Average latency: %.2f ms\n", result.LatencyMean.Seconds()*1000)
fmt.Printf("HTTP status codes:\n")
for code, count := range result.StatusCodes {
fmt.Printf(" %d: %d\n", code, count)
}
}
以上代码演示了如何以编程方式使用bombardier库进行HTTP性能测试。您可以根据需要调整配置参数,如并发连接数、请求总数、持续时间等。
更多关于golang跨平台HTTP性能测试工具插件库bombardier的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang跨平台HTTP性能测试工具插件库bombardier的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Golang跨平台HTTP性能测试工具Bombardier使用指南
Bombardier是一个用Go语言编写的高性能HTTP(S)基准测试工具,支持跨平台运行。下面我将详细介绍它的使用方法和示例代码。
Bombardier特点
- 跨平台支持(Windows/Linux/macOS)
- 高性能,能产生大量并发请求
- 支持HTTP/1.x和HTTP/2
- 提供详细的统计报告
- 支持多种HTTP方法(GET, POST, PUT, DELETE等)
- 可设置请求头、请求体等
安装Bombardier
go get -u github.com/codesenberg/bombardier
或者直接下载预编译的二进制文件: https://github.com/codesenberg/bombardier/releases
基本使用示例
简单GET请求测试
bombardier -c 125 -n 10000 http://localhost:8080
参数说明:
-c 125
: 并发连接数125-n 10000
: 总请求数10000
代码中使用Bombardier库
虽然Bombardier主要作为命令行工具使用,但也可以通过代码调用其核心功能:
package main
import (
"fmt"
"time"
"github.com/codesenberg/bombardier/runner"
)
func main() {
config := runner.Config{
URL: "http://localhost:8080",
Method: "GET",
Connections: 100,
Duration: 10 * time.Second,
Timeout: 10 * time.Second,
PrintProgress: true,
PrintResult: true,
}
benchmark, err := runner.NewBenchmark(config)
if err != nil {
panic(err)
}
result, err := benchmark.Run()
if err != nil {
panic(err)
}
fmt.Printf("\nFinal Results:\n")
fmt.Printf("Requests: %d\n", result.ReqCount)
fmt.Printf("Time taken: %v\n", result.TimeTaken)
fmt.Printf("Requests/sec: %.2f\n", result.Rps)
fmt.Printf("Transfer/sec: %s\n", humanizeBytes(result.Transfer))
}
func humanizeBytes(bytes uint64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := uint64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}
常用命令行参数
参数 | 说明 |
---|---|
-c |
并发连接数 |
-n |
总请求数 |
-d |
测试持续时间 (如10s, 5m) |
-t |
超时时间 |
-m |
HTTP方法 (GET, POST等) |
-b |
请求体内容 |
-H |
自定义请求头 |
-k |
启用HTTP Keep-Alive |
-p |
使用HTTP代理 |
-l |
启用速率限制 (请求/秒) |
-h2 |
使用HTTP/2协议 |
高级用法示例
POST请求测试
bombardier -c 50 -n 5000 -m POST -b '{"key":"value"}' -H "Content-Type: application/json" http://localhost:8080/api
带认证的请求
bombardier -c 100 -n 10000 -H "Authorization: Bearer token123" http://localhost:8080/secure
持续30秒的测试
bombardier -c 200 -d 30s http://localhost:8080
结果解读
Bombardier会输出类似如下的统计信息:
Bombarding http://localhost:8080 with 10000 request(s) using 125 connection(s)
10000 / 10000 [=================================================================] 100.00% 1m0s
Done!
Statistics Avg Stdev Max
Reqs/sec 165.67 12.34 200.00
Latency 754.12ms 100.23ms 1.50s
HTTP codes:
1xx - 0, 2xx - 10000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 45.21KB/s
关键指标:
- Reqs/sec: 每秒请求数
- Latency: 延迟时间
- HTTP codes: 响应状态码分布
- Throughput: 吞吐量
性能优化建议
- 测试前关闭所有不必要的日志记录
- 从本地网络测试以减少网络延迟影响
- 逐步增加并发数观察系统表现
- 监控服务器资源使用情况(CPU、内存、网络等)
- 长时间测试时注意客户端和服务器端的资源消耗
Bombardier是一个强大而灵活的工具,可以很好地满足大多数HTTP性能测试需求。通过合理配置参数,你可以全面评估Web服务的性能表现。