forked from mcuadros/go-syslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_bench_test.go
105 lines (91 loc) · 2.22 KB
/
server_bench_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
93
94
95
96
97
98
99
100
101
102
103
104
105
package syslog
import (
"bufio"
"io"
"net"
"testing"
"time"
"gopkg.in/mcuadros/go-syslog.v2/format"
)
type noopFormatter struct{}
func (noopFormatter) Parse() error {
return nil
}
func (noopFormatter) Dump() format.LogParts {
return format.LogParts{}
}
func (noopFormatter) Location(*time.Location) {}
func (n noopFormatter) GetParser(l []byte) format.LogParser {
return n
}
func (n noopFormatter) GetSplitFunc() bufio.SplitFunc {
return nil
}
type handlerCounter struct {
expected int
current int
done chan struct{}
}
func (s *handlerCounter) Handle(logParts format.LogParts, msgLen int64, err error) {
s.current++
if s.current == s.expected {
close(s.done)
}
}
type fakePacketConn struct {
*io.PipeReader
}
func (c *fakePacketConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
n, err = c.PipeReader.Read(b)
return
}
func (c *fakePacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
return 0, nil
}
func (c *fakePacketConn) Close() error {
return nil
}
func (c *fakePacketConn) LocalAddr() net.Addr {
return nil
}
func (c *fakePacketConn) SetDeadline(t time.Time) error {
return nil
}
func (c *fakePacketConn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *fakePacketConn) SetWriteDeadline(t time.Time) error {
return nil
}
func BenchmarkDatagramNoFormatting(b *testing.B) {
handler := &handlerCounter{expected: b.N, done: make(chan struct{})}
server := NewServer()
defer server.Kill()
server.SetFormat(noopFormatter{})
server.SetHandler(handler)
reader, writer := io.Pipe()
server.goReceiveDatagrams(&fakePacketConn{PipeReader: reader})
server.goParseDatagrams()
msg := []byte(exampleSyslog + "\n")
b.SetBytes(int64(len(msg)))
for i := 0; i < b.N; i++ {
writer.Write(msg)
}
<-handler.done
}
func BenchmarkTCPNoFormatting(b *testing.B) {
handler := &handlerCounter{expected: b.N, done: make(chan struct{})}
server := NewServer()
defer server.Kill()
server.SetFormat(noopFormatter{})
server.SetHandler(handler)
server.ListenTCP("127.0.0.1:0")
server.Boot()
conn, _ := net.DialTimeout("tcp", server.listeners[0].Addr().String(), time.Second)
msg := []byte(exampleSyslog + "\n")
b.SetBytes(int64(len(msg)))
for i := 0; i < b.N; i++ {
conn.Write(msg)
}
<-handler.done
}