Documentation
¶
Index ¶
- type BenchResult
- type HTTPBench
- func (b *HTTPBench) OnProgress(fn ProgressFn) *HTTPBench
- func (b *HTTPBench) ProgressTick(d time.Duration) *HTTPBench
- func (b *HTTPBench) Run() (*BenchResult, error)
- func (b *HTTPBench) RunCtx(parent context.Context) (*BenchResult, error)
- func (b *HTTPBench) SetBody(body []byte) *HTTPBench
- func (b *HTTPBench) SetConcurrency(concurrency int) *HTTPBench
- func (b *HTTPBench) SetDuration(duration time.Duration) *HTTPBench
- func (b *HTTPBench) SetHeaders(headers map[string]string) *HTTPBench
- func (b *HTTPBench) SetMethod(method string) *HTTPBench
- func (b *HTTPBench) SetNumber(number int64) *HTTPBench
- func (b *HTTPBench) SetQPSLimit(qps int) *HTTPBench
- func (b *HTTPBench) SetTimeout(timeout time.Duration) *HTTPBench
- type ProgressFn
- type Snapshot
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BenchResult ¶
type BenchResult struct {
URL string
TotalReqs int64
SuccessReqs int64
FailReqs int64
Duration time.Duration
AvgRespTime time.Duration
MinRespTime time.Duration
MaxRespTime time.Duration
ReqsPerSecond float64
BytesPerSecond float64
StatusCodes map[int]int64
}
BenchResult 保存测试结果
func (*BenchResult) PlainString ¶ added in v0.6.0
func (r *BenchResult) PlainString() string
PlainString returns the formatted result without color tags. Use this when writing the result to a file or any non-ccolor sink.
func (*BenchResult) String ¶
func (r *BenchResult) String() string
String returns the formatted result with ccolor tags (<green>...</> etc.). Suitable for printing via ccolor.Print on a terminal. For file output use PlainString to avoid raw tags ending up in the file.
type HTTPBench ¶
type HTTPBench struct {
// 基础配置
URL string
Method string
Headers map[string]string
Body []byte
Timeout time.Duration // 每个请求的超时时间
QPSLimit int // QPS限制 (0表示不限制)
// 测试参数
Number int64 // 总请求数
Concurrency int // 并发数
Duration time.Duration // 测试时长 (如果指定,则忽略Number)
// contains filtered or unexported fields
}
HTTPBench 实现类似 apache bench 的测试功能
func (*HTTPBench) OnProgress ¶ added in v0.6.0
func (b *HTTPBench) OnProgress(fn ProgressFn) *HTTPBench
OnProgress installs a callback invoked periodically (and once at completion) while RunCtx is executing. Pass nil to disable progress reporting (default).
The callback runs in the bench's progress goroutine — keep it non-blocking. The library does NOT render anything itself; callers (such as cmd/gbench) drive their own UI (e.g. cliui/progress) via this hook.
func (*HTTPBench) ProgressTick ¶ added in v0.6.0
ProgressTick controls how often OnProgress fires. Default is 200ms.
func (*HTTPBench) Run ¶
func (b *HTTPBench) Run() (*BenchResult, error)
Run executes the benchmark with a background context. Equivalent to RunCtx(context.Background()).
func (*HTTPBench) RunCtx ¶ added in v0.6.0
func (b *HTTPBench) RunCtx(parent context.Context) (*BenchResult, error)
RunCtx executes the benchmark with the given parent context. Cancel the context to stop the test gracefully — workers finish in-flight requests then exit, and partial results are returned.
func (*HTTPBench) SetConcurrency ¶
SetConcurrency 设置并发数
func (*HTTPBench) SetDuration ¶
SetDuration 设置测试时长
func (*HTTPBench) SetHeaders ¶
SetHeaders 设置请求头
func (*HTTPBench) SetQPSLimit ¶
SetQPSLimit 设置QPS限制
type ProgressFn ¶ added in v0.6.0
type ProgressFn func(s Snapshot)
ProgressFn is the callback invoked on each tick and at completion. Implementations should be non-blocking — it runs in the bench's progress goroutine.
type Snapshot ¶ added in v0.6.0
type Snapshot struct {
Completed int64 // requests dispatched so far (== totalReqs)
Total int64 // target Number (0 if Duration-driven)
Elapsed time.Duration // time since the run started
Duration time.Duration // configured run duration (0 if Number-driven)
Done bool // true on the final snapshot fired after wg.Wait
}
Snapshot is a point-in-time view of bench progress, delivered to OnProgress. All fields are populated from atomically-loaded counters so the callback may run in a separate goroutine.