apicheck-go 是一个基于 Go 语言的轻量级接口自动化测试工具,支持通过 YAML 配置文件定义测试用例,实现快速、可维护的接口验证和压力测试。
git clone https://cnb.cool/zhiqiangwang/apicheck-go
cd apicheck-go
go build -o apicheck main.go
./apicheck flow config.yml
./apicheck ab --url http://example.com --concurrency 10 --total 1000 --timeout 3s --keep-alive
variables:
api_url: "https://httpbin.org"
username: "apicheck"
password: "secure123"
token: "abc123xyz"
tasks:
- name: get_ip
method: GET
url: "{{api_url}}/ip"
assert:
http_status: 200
json_fields:
origin: "present"
- name: post_data
method: POST
url: "{{api_url}}/post"
headers:
Content-Type: "application/json"
Authorization: "Bearer {{token}}"
body: '{"username":"{{username}}","password":"{{password}}","email":"{{username}}@example.com"}'
assert:
http_status: 200
json_fields:
json: "present"
headers: "present"
- name: get_headers
method: GET
url: "{{api_url}}/headers"
headers:
X-Custom-Header: "test-value"
User-Agent: "apicheck-go/1.0"
assert:
http_status: 200
json_fields:
headers: "present"
- name: put_update
method: PUT
url: "{{api_url}}/put"
body: '{"updated_field":"new_value"}'
assert:
http_status: 200
- name: delete_test
method: DELETE
url: "{{api_url}}/delete"
assert:
http_status: 200
json_fields:
json: "present"
type ABTask struct {
URL string // 压测地址
Concurrency int // 并发数
Total int // 总请求数
Timeout time.Duration // 单请求超时
KeepAlive bool // 是否开启 KeepAlive
}
type ABResult struct {
Total int // 总请求数
Success int // 成功请求数
Failed int // 失败请求数
Min float64 // 最小延迟(ms)
Max float64 // 最大延迟(ms)
Mean float64 // 平均延迟(ms)
Median float64 // 中位数延迟(ms)
P90 float64 // 90%请求的延迟(ms)
P95 float64 // 95%请求的延迟(ms)
P99 float64 // 99%请求的延迟(ms)
QPS float64 // 每秒请求数
Duration float64 // 总耗时(秒)
Start time.Time // 开始时间
End time.Time // 结束时间
StatusCodes map[int]int // 状态码统计
}
apicheck-go/ ├── conf/ # 配置相关 │ └── config.go ├── flow/ # 测试流程控制 │ ├── flow.go │ └── result.go ├── task/ # 任务类型实现 │ ├── ab_task.go # AB 压力测试任务 │ ├── http_task.go # HTTP 测试任务 │ ├── task.go # 任务接口定义 │ └── variable.go # 变量管理 ├── config.yml # 测试用例配置示例 ├── main.go # 入口文件 ├── go.mod ├── go.sum └── README.md
AB 压力测试功能支持高并发 HTTP 请求,提供详细的性能指标:
func (t *ABTask) newHTTPClient() *http.Client {
transport := &http.Transport{
MaxIdleConns: t.Concurrency * 2,
MaxIdleConnsPerHost: t.Concurrency,
MaxConnsPerHost: t.Concurrency,
IdleConnTimeout: 90 * time.Second,
DisableKeepAlives: !t.KeepAlive,
TLSHandshakeTimeout: t.Timeout,
ResponseHeaderTimeout: t.Timeout,
}
return &http.Client{
Transport: transport,
Timeout: t.Timeout,
}
}