lolly/internal/middleware/accesslog/accesslog_bench_test.go
xfy 25bdba4e01 test(benchmark): 新增组件级基准测试套件
- 新增 benchmark_context.go 标准化测试上下文构造器
- 新增静态文件处理器基准测试(缓存命中/未命中、try_files)
- 新增访问日志中间件基准测试
- 新增压缩中间件基准测试(gzip/brotli、Pool 复用)
- 新增限流器基准测试(令牌桶、滑动窗口、多客户端并发)
- 新增变量展开基准测试(模板展开、Pool 操作)
2026-04-08 18:25:38 +08:00

70 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package accesslog 提供访问日志中间件的基准测试。
//
// 该文件测试日志记录的性能开销。
//
// 作者xfy
package accesslog
import (
"testing"
"github.com/valyala/fasthttp"
"rua.plus/lolly/internal/config"
)
// BenchmarkAccessLogProcess 测试访问日志中间件处理性能。
func BenchmarkAccessLogProcess(b *testing.B) {
cfg := &config.LoggingConfig{
Access: config.AccessLogConfig{
Path: "/dev/null",
Format: "combined",
},
}
al := New(cfg)
defer al.Close()
mockHandler := func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetBodyString("Hello, World!")
}
handler := al.Process(mockHandler)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod(fasthttp.MethodGet)
ctx.Request.SetRequestURI("/api/test")
ctx.Request.Header.SetHost("example.com")
handler(ctx)
}
}
// BenchmarkAccessLogProcessParallel 测试并发场景下的访问日志性能。
func BenchmarkAccessLogProcessParallel(b *testing.B) {
cfg := &config.LoggingConfig{
Access: config.AccessLogConfig{
Path: "/dev/null",
Format: "combined",
},
}
al := New(cfg)
defer al.Close()
mockHandler := func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetBodyString("OK")
}
handler := al.Process(mockHandler)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod(fasthttp.MethodGet)
ctx.Request.SetRequestURI("/api/test")
handler(ctx)
}
})
}