golang高性能HTTP基准测试与统计分析工具插件库go-benchmark-app的使用

Go Benchmark App - 高性能HTTP基准测试与统计分析工具

简介

Go Benchmark App 是一个用于HTTP基准测试的工具,可以通过不同的规则和配置进行测试。其主要目标是:通过相同的基准测试工具、相同的参数、重复且时间分布合理的测试,来比较不同Web应用程序的性能,获取平均结果并与其他应用程序进行比较。

Go Benchmark App

支持的基准测试工具

  • ab (ApacheBench)
  • wrk
  • siege

这些工具需要预先安装在系统中。

配置方法

所有配置都在 config/main.toml 文件中:

# Title for describing benchmarks
title = "CMS benchmarks"
# Benchmarks version
version = "0.1"
# Delay via try in seconds
delay = 20
# How much we should try
try = 10

# ab benchmarks parametres
[ab]
concurency = 5000
keepalive = false
requests = 10000

# wrk benchmarks parametres
[wrk]
connections = 5000
duration = 10
threads = 1000

# siege benchmarks parametres
[siege]
concurrent = 100
time = 30

# Applications parametres - list
[[app]]
title = "Application Banchamrs Title"
path = "fool/path/to/app"
url = "http://localhost:5000/test"

使用方法

准备工作

  1. 安装 Go 1.6 或更高版本
  2. 检查基准测试工具是否安装:
    $ whereis ab
    $ whereis wrk
    $ whereis siege
    
  3. 按照上述说明配置 config/main.toml 文件

安装与运行

  1. 安装应用程序:

    $ go install github.com/mrlsd/go-benchmark-app
    
  2. 查看命令行帮助:

    $ go-benchmark-app -h
    

    输出:

    Go Benchmark Applications v1.0.0
    Options:
      -c FILE
         load configuration from FILE (default "config/main.toml")
      -v	verbose output
    
  3. 使用自定义配置文件:

    $ go-benchmark-app -c path/to/cfg.toml
    
  4. 启用详细输出:

    $ go-benchmark-app -v
    

示例代码

配置示例

[[app]]
title = "My App"
path = "apps/myapp.sh"
url = "http://localhost:5000/test"

应用启动脚本示例

#!/bin/sh
prepare -to -run
/full/path/to/app -v param -d param -etc

技巧与建议

  1. 如果基准测试因socket错误失败(资源不可用),可以尝试:

    • 增加配置文件中的 delay 选项(例如60秒)
    • 修改系统限制:打开文件数、TCP/IP配置等系统限制
  2. 为了灵活运行测试应用程序,可以创建启动脚本:

    • 在配置文件中指定脚本路径
    • 脚本中可以启动docker、nginx或其他有用的命令
  3. 每个应用程序使用一个URL,这样可以简化结果分析、解释和比较。

许可证

MIT License


更多关于golang高性能HTTP基准测试与统计分析工具插件库go-benchmark-app的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang高性能HTTP基准测试与统计分析工具插件库go-benchmark-app的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


go-benchmark-app: Golang高性能HTTP基准测试与统计分析工具

go-benchmark-app 是一个专为Golang设计的高性能HTTP基准测试工具库,它提供了简单易用的API来进行HTTP压力测试,并生成详细的统计分析报告。

主要特性

  1. 支持并发请求测试
  2. 提供详细的统计指标(平均响应时间、吞吐量、错误率等)
  3. 可自定义测试持续时间或请求总数
  4. 支持结果可视化输出
  5. 轻量级且易于集成

安装

go get github.com/your-repo/go-benchmark-app

基本使用示例

package main

import (
	"fmt"
	"github.com/your-repo/go-benchmark-app/benchmark"
	"time"
)

func main() {
	// 创建测试配置
	config := benchmark.Config{
		URL:             "http://example.com/api",
		Method:          "GET",
		Concurrency:     50,      // 并发数
		TotalRequests:   1000,    // 总请求数
		Timeout:         5 * time.Second,
		PrintInterval:   1 * time.Second, // 打印间隔
	}

	// 运行基准测试
	result, err := benchmark.Run(config)
	if err != nil {
		fmt.Printf("Benchmark failed: %v\n", err)
		return
	}

	// 打印结果
	fmt.Println("\n=== Benchmark Results ===")
	fmt.Printf("Total time: %v\n", result.TotalTime)
	fmt.Printf("Requests: %d\n", result.TotalRequests)
	fmt.Printf("Successful: %d\n", result.SuccessfulRequests)
	fmt.Printf("Failed: %d\n", result.FailedRequests)
	fmt.Printf("Requests/sec: %.2f\n", result.RequestsPerSecond)
	fmt.Printf("Avg latency: %v\n", result.AvgLatency)
	fmt.Printf("Max latency: %v\n", result.MaxLatency)
	fmt.Printf("Min latency: %v\n", result.MinLatency)
	fmt.Printf("Error rate: %.2f%%\n", result.ErrorRate*100)

	// 打印百分位延迟
	fmt.Println("\nLatency Percentiles:")
	fmt.Printf("50%%: %v\n", result.LatencyPercentiles[50])
	fmt.Printf("90%%: %v\n", result.LatencyPercentiles[90])
	fmt.Printf("95%%: %v\n", result.LatencyPercentiles[95])
	fmt.Printf("99%%: %v\n", result.LatencyPercentiles[99])
}

高级功能

自定义请求头和数据

config := benchmark.Config{
    URL:    "http://example.com/api",
    Method: "POST",
    Headers: map[string]string{
        "Content-Type":  "application/json",
        "Authorization": "Bearer token",
    },
    Body: []byte(`{"key":"value"}`),
    // 其他配置...
}

持续测试模式

config := benchmark.Config{
    URL:         "http://example.com/api",
    Method:      "GET",
    Concurrency: 100,
    Duration:    30 * time.Second, // 持续测试30秒
    // 其他配置...
}

结果可视化

// 生成ASCII图表显示请求速率变化
benchmark.PlotRequestsPerSecond(result)

// 生成延迟分布直方图
benchmark.PlotLatencyDistribution(result)

性能优化建议

  1. 调整并发数:根据目标服务器的能力调整并发数,过高可能导致客户端成为瓶颈
  2. 使用连接池:工具内部已经实现了HTTP连接池,无需额外配置
  3. 减少日志输出:对于极高并发的测试,减少PrintInterval可以提高性能
  4. 分布式测试:对于大规模测试,考虑从多台机器同时运行测试

注意事项

  1. 请确保有权限对目标服务进行压力测试
  2. 避免在生产环境直接进行高压测试
  3. 测试前考虑目标服务的承载能力
  4. 长时间测试注意监控客户端资源使用情况

go-benchmark-app 是一个简单但功能强大的工具,可以帮助开发者评估HTTP服务的性能表现,找出性能瓶颈,并进行优化前后的对比测试。

回到顶部