-
Notifications
You must be signed in to change notification settings - Fork 46
/
benchmark_test.go
42 lines (37 loc) · 1.01 KB
/
benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package tcpx
import (
"testing"
)
// Benchmark_PerRequest_OnMessage-4 2000000 643 ns/op 1368 B/op 5 allocs/op
func Benchmark_PerRequest_OnMessage(b *testing.B) {
srv := NewTcpX(nil)
srv.OnMessage = func(c *Context) {
}
runTCPBench(b, srv)
}
// Benchmark_PerRequest_Mux-4 2000000 761 ns/op 1368 B/op 5 allocs/op
func Benchmark_PerRequest_Mux(b *testing.B) {
srv := NewTcpX(nil)
srv.AddHandler(1, func(c *Context) {
})
runTCPBench(b, srv)
}
// Benchmark_PerRequest_Middleware-4 2000000 768 ns/op 1368 B/op 5 allocs/op
func Benchmark_PerRequest_Middleware(b *testing.B) {
srv := NewTcpX(nil)
srv.UseGlobal(func(c *Context) {})
srv.Use("mid-1", func(c *Context) {})
srv.AddHandler(1, func(c *Context) {}, func(c *Context) {
})
runTCPBench(b, srv)
}
func runTCPBench(b *testing.B, srv *TcpX) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
ctx := &Context{Stream: PackStuff(1)}
handleMiddleware(ctx, srv)
}()
}
}