-
Notifications
You must be signed in to change notification settings - Fork 88
/
benchmarks_test.go
92 lines (76 loc) · 1.87 KB
/
benchmarks_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package easytcp
import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"net"
"testing"
)
// go test -bench="^Benchmark_\w+$" -run=none -benchmem -benchtime=250000x
type mutedLogger struct{}
func (m *mutedLogger) Errorf(_ string, _ ...interface{}) {}
func (m *mutedLogger) Tracef(_ string, _ ...interface{}) {}
func Benchmark_NoHandler(b *testing.B) {
s := NewServer(&ServerOption{
DoNotPrintRoutes: true,
})
go s.Run("127.0.0.1:0") // nolint
defer s.Stop() // nolint
<-s.acceptingC
// client
client, err := net.Dial("tcp", s.Listener.Addr().String())
if err != nil {
panic(err)
}
defer client.Close() // nolint
packedBytes, _ := s.Packer.Pack(NewMessage(1, []byte("ping")))
beforeBench(b)
for i := 0; i < b.N; i++ {
_, _ = client.Write(packedBytes)
}
}
func Benchmark_OneHandler(b *testing.B) {
s := NewServer(&ServerOption{
DoNotPrintRoutes: true,
})
s.AddRoute(1, func(ctx Context) {})
go s.Run("127.0.0.1:0") // nolint
defer s.Stop() // nolint
<-s.acceptingC
// client
client, err := net.Dial("tcp", s.Listener.Addr().String())
if err != nil {
panic(err)
}
defer client.Close() // nolint
packedMsg, _ := s.Packer.Pack(NewMessage(1, []byte("ping")))
beforeBench(b)
for i := 0; i < b.N; i++ {
_, _ = client.Write(packedMsg)
}
}
func Benchmark_DefaultPacker_Pack(b *testing.B) {
packer := NewDefaultPacker()
msg := NewMessage(1, []byte("test"))
beforeBench(b)
for i := 0; i < b.N; i++ {
_, _ = packer.Pack(msg)
}
}
func Benchmark_DefaultPacker_Unpack(b *testing.B) {
packer := NewDefaultPacker()
msg := NewMessage(1, []byte("test"))
dataBytes, err := packer.Pack(msg)
assert.NoError(b, err)
r := bytes.NewReader(dataBytes)
beforeBench(b)
for i := 0; i < b.N; i++ {
_, _ = packer.Unpack(r)
_, _ = r.Seek(0, io.SeekStart)
}
}
func beforeBench(b *testing.B) {
_log = &mutedLogger{}
b.ReportAllocs()
b.ResetTimer()
}