golang跨平台HTTP负载测试工具插件库cassowary的使用

Golang跨平台HTTP负载测试工具插件库Cassowary的使用

Cassowary是一个现代HTTP/S、直观且跨平台的负载测试工具,用Go语言编写,适合开发人员、测试人员和系统管理员使用。

cassowary

主要特性

📌 2种负载测试模式:标准模式和扩展模式(可以从文件中指定URL路径)
📌 CI友好:非常适合作为CI流水线的一部分
📌 灵活的指标输出:支持Cloudwatch指标、Prometheus指标(推送到Prometheus PushGateway)、JSON文件
📌 可配置:能够传递任意HTTP头,能够配置HTTP客户端
📌 支持GET、POST、PUT和PATCH - POST、PUT和PATCH数据可以在文件中定义
📌 跨平台:Linux、Mac OSX和Windows的单一预构建二进制文件
📌 可导入 - 除了CLI工具外,cassowary可以作为模块导入到您的Go应用中
📌 可视化 - Cassowary可以将请求数据导出为png格式的直方图和箱线图

安装方法

常规安装

从GitHub Releases页面获取预构建的二进制文件。

Homebrew (Mac OSX)

$ brew update && brew install cassowary

Docker

$ docker run rogerw/cassowary:v0.14.1 -u http://www.example.com -c 1 -n 10

本地开发

$ GOOS=linux go build -o dist/docker/cassowary cmd/cassowary/*.go
$ docker build -f dist/docker/Dockerfile -t test_cassowary dist/docker
$ docker run test_cassowary -u http://www.example.com -c 1 -n 10

使用示例

常规负载测试

$ ./cassowary run -u http://www.example.com -c 10 -n 100

Starting Load Test with 100 requests using 10 concurrent users

 100% |████████████████████████████████████████| [1s:0s]            1.256773616s


 TCP Connect.....................: Avg/mean=101.90ms 	Median=102.00ms	p(95)=105ms
 Server Processing...............: Avg/mean=100.18ms 	Median=100.50ms	p(95)=103ms
 Content Transfer................: Avg/mean=0.01ms 	Median=0.00ms	p(95)=0ms

Summary:
 Total Req.......................: 100
 Failed Req......................: 0
 DNS Lookup......................: 115.00ms
 Req/s...........................: 79.57

文件模式

$ ./cassowary run -u http://localhost:8000 -c 10 -n 100 -f urlpath.txt

导出指标到文件

$ ./cassowary run --json-metrics --json-metrics-file=metrics.json -u http://localhost:8000 -c 125 -n 100000

导出指标到Prometheus

$ ./cassowary run -u http://localhost:8000 -c 125 -n 100000 -p http://pushgatway:9091

导出指标到Cloudwatch

$ export AWS_REGION=eu-north-1 && ./cassowary run -u http://localhost:8000 -c 125 -n 100000 --cloudwatch

生成直方图

$ ./cassowary run -u http://localhost:8000 -c 10 -n 1000 --histogram

直方图示例

生成箱线图

$ ./cassowary run -u http://localhost:8000 -c 10 -n 1000 --boxplot

带POST数据的测试

$ ./cassowary run -u http://localhost:8000/add-user -c 10 -n 1000 --postfile user.json

指定测试持续时间

$ ./cassowary run -u http://localhost:8000 -n 100 -d 30

添加HTTP头

$ ./cassowary run -u http://localhost:8000 -c 10 -n 1000 -H 'Host: www.example.com'

禁用HTTP keep-alive

$ ./cassowary run -u http://localhost:8000 -c 10 -n 1000 --disable-keep-alive

x509认证

$ ./cassowary run -u https://localhost:8443 -c 10 -n 1000 --cert /path/to/client.pem --key /path/to/client-key.pem --ca /path/to/ca.pem --renegotiation once

作为库使用示例

package main

import (
        "encoding/json"
	"fmt"

	"github.com/rogerwelin/cassowary/pkg/client"
)

func main() {
	cass := &client.Cassowary{
		BaseURL:               "http://www.example.com",
		ConcurrencyLevel:      1,
		Requests:              10,
		DisableTerminalOutput: true,
	}
	metrics, err := cass.Coordinate()
	if err != nil {
		panic(err)
	}

        // 打印结果
	fmt.Printf("%+v\n", metrics)

        // 或以JSON格式打印
	jsonMetrics, err := json.Marshal(metrics)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(jsonMetrics))
}

贡献

欢迎贡献!要请求功能,请创建一个带有feature-request标签的新问题。发现错误?请添加带有bugs标签的问题。也欢迎Pull Request,但请先就请求的功能添加一个issue(除非是简单的错误修复或readme更改)。


更多关于golang跨平台HTTP负载测试工具插件库cassowary的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang跨平台HTTP负载测试工具插件库cassowary的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang跨平台HTTP负载测试工具Cassowary使用指南

Cassowary是一个基于Go语言开发的轻量级、跨平台HTTP负载测试工具,它可以帮助开发者快速进行API性能测试和压力测试。下面我将详细介绍Cassowary的使用方法,并提供示例代码。

安装Cassowary

首先需要安装Cassowary:

go get -u github.com/rogerwelin/cassowary/cmd/cassowary

或者直接下载预编译的二进制文件: https://github.com/rogerwelin/cassowary/releases

基本使用

命令行使用

cassowary run -u http://example.com/api -c 10 -n 1000

参数说明:

  • -u--url: 测试的目标URL
  • -c--concurrency: 并发数
  • -n--requests: 总请求数
  • -m--method: HTTP方法 (GET, POST, PUT, DELETE等)
  • -b--body: 请求体内容
  • -H--header: 请求头 (可多次使用)
  • -t--timeout: 请求超时时间(秒)

程序化使用示例

package main

import (
	"fmt"
	"github.com/rogerwelin/cassowary/pkg/runner"
	"time"
)

func main() {
	// 创建负载测试配置
	config := &runner.Config{
		URL:         "http://example.com/api",
		Method:      "GET",
		Concurrency: 10,
		Requests:    1000,
		Timeout:     10 * time.Second,
		Headers:     []string{"Content-Type: application/json", "Authorization: Bearer token"},
		Body:        `{"key":"value"}`,
		Output:      "stdout", // 或 "json", "csv"
	}

	// 创建负载测试运行器
	loadTester, err := runner.NewLoadTestRunner(config)
	if err != nil {
		panic(err)
	}

	// 运行测试
	results, err := loadTester.Run()
	if err != nil {
		panic(err)
	}

	// 输出结果
	fmt.Printf("测试完成\n")
	fmt.Printf("总请求数: %d\n", results.Requests)
	fmt.Printf("成功请求: %d\n", results.Success)
	fmt.Printf("失败请求: %d\n", results.Fail)
	fmt.Printf("平均响应时间: %.2f ms\n", results.Average)
	fmt.Printf("最快响应时间: %.2f ms\n", results.Fastest)
	fmt.Printf("最慢响应时间: %.2f ms\n", results.Slowest)
	fmt.Printf("RPS: %.2f\n", results.Rps)
}

高级功能

自定义负载模式

config := &runner.Config{
	URL:         "http://example.com/api",
	Method:      "POST",
	Concurrency: 50,
	Requests:    5000,
	Timeout:     5 * time.Second,
	Headers:     []string{"Content-Type: application/json"},
	Body:        `{"test":"data"}`,
	LoadType:    runner.LoadTypeLinear, // 或 LoadTypeExponential
	Duration:    60 * time.Second,     // 测试持续时间
}

分布式测试

Cassowary支持分布式测试模式,可以在多台机器上同时运行测试:

  1. 启动主节点:
cassowary master -p 8080
  1. 启动工作节点:
cassowary worker -m http://master-ip:8080
  1. 在主节点上运行测试:
cassowary run -u http://example.com/api -c 100 -n 10000 --master

结果可视化

Cassowary支持将结果输出为JSON或CSV格式,方便后续分析:

cassowary run -u http://example.com/api -c 10 -n 1000 -o json > results.json

性能优化建议

  1. 根据目标服务器配置合理设置并发数
  2. 从低并发开始测试,逐步增加
  3. 监控服务器资源使用情况
  4. 测试时间不宜过短,建议至少30秒
  5. 在测试环境中进行,避免影响生产环境

与其他工具对比

Cassowary相比其他负载测试工具(如ab、wrk、JMeter)有以下优势:

  • 轻量级,易于安装和使用
  • 支持分布式测试
  • 提供程序化API
  • 跨平台支持
  • 结果输出格式多样

Cassowary非常适合中小规模的API性能测试和持续集成中的自动化测试场景。

回到顶部