bench

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 9 Imported by: 1

Documentation

Index

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 NewHTTPBench

func NewHTTPBench(url string) *HTTPBench

NewHTTPBench 创建新的HTTPBench实例

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

func (b *HTTPBench) ProgressTick(d time.Duration) *HTTPBench

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) SetBody

func (b *HTTPBench) SetBody(body []byte) *HTTPBench

SetBody 设置请求体

func (*HTTPBench) SetConcurrency

func (b *HTTPBench) SetConcurrency(concurrency int) *HTTPBench

SetConcurrency 设置并发数

func (*HTTPBench) SetDuration

func (b *HTTPBench) SetDuration(duration time.Duration) *HTTPBench

SetDuration 设置测试时长

func (*HTTPBench) SetHeaders

func (b *HTTPBench) SetHeaders(headers map[string]string) *HTTPBench

SetHeaders 设置请求头

func (*HTTPBench) SetMethod

func (b *HTTPBench) SetMethod(method string) *HTTPBench

SetMethod 设置HTTP方法

func (*HTTPBench) SetNumber

func (b *HTTPBench) SetNumber(number int64) *HTTPBench

SetNumber 设置请求数

func (*HTTPBench) SetQPSLimit

func (b *HTTPBench) SetQPSLimit(qps int) *HTTPBench

SetQPSLimit 设置QPS限制

func (*HTTPBench) SetTimeout

func (b *HTTPBench) SetTimeout(timeout time.Duration) *HTTPBench

SetTimeout 设置超时时间

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL