-
Notifications
You must be signed in to change notification settings - Fork 36
/
memconn_bench_test.go
133 lines (117 loc) · 3.34 KB
/
memconn_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package memconn_test
import (
"fmt"
"net"
"os"
"testing"
"time"
"github.com/akutz/memconn"
)
func BenchmarkMemb(b *testing.B) {
addr := fmt.Sprintf("%d", time.Now().UnixNano())
lis := serve(b, memconn.Listen, "memb", addr, 0, 0, false)
benchmarkNetConnParallel(b, lis, memconn.Dial)
}
func BenchmarkMembWithDeadline(b *testing.B) {
laddr := fmt.Sprintf("%d", time.Now().UnixNano())
lis := serve(b, memconn.Listen, "memb", laddr, 0, 0, false)
benchmarkNetConnParallel(b, lis,
func(network, addr string) (net.Conn, error) {
client, err := memconn.Dial(network, addr)
if err != nil {
return nil, err
}
deadline := time.Now().Add(time.Duration(1) * time.Minute)
client.SetDeadline(deadline)
return client, nil
})
}
func BenchmarkMemu(b *testing.B) {
addr := fmt.Sprintf("%d", time.Now().UnixNano())
lis := serve(b, memconn.Listen, "memu", addr, 0, 0, false)
benchmarkNetConnParallel(b, lis, memconn.Dial)
}
func BenchmarkMemuWithDeadline(b *testing.B) {
laddr := fmt.Sprintf("%d", time.Now().UnixNano())
lis := serve(b, memconn.Listen, "memu", laddr, 0, 0, false)
benchmarkNetConnParallel(b, lis,
func(network, addr string) (net.Conn, error) {
client, err := memconn.Dial(network, addr)
if err != nil {
return nil, err
}
deadline := time.Now().Add(time.Duration(1) * time.Minute)
client.SetDeadline(deadline)
return client, nil
})
}
func BenchmarkTCP(b *testing.B) {
lis := serve(b, net.Listen, "tcp", "127.0.0.1:", 0, 0, false)
benchmarkNetConnParallel(b, lis, net.Dial)
}
func BenchmarkTCPWithDeadline(b *testing.B) {
lis := serve(b, net.Listen, "tcp", "127.0.0.1:", 0, 0, false)
benchmarkNetConnParallel(b, lis,
func(network, addr string) (net.Conn, error) {
client, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
deadline := time.Now().Add(time.Duration(1) * time.Minute)
client.SetDeadline(deadline)
return client, nil
})
}
func BenchmarkUNIX(b *testing.B) {
sockFile := getTempSockFile(b)
defer os.RemoveAll(sockFile)
lis := serve(b, net.Listen, "unix", sockFile, 0, 0, false)
benchmarkNetConnParallel(b, lis, dialUNIX)
}
func BenchmarkUNIXWithDeadline(b *testing.B) {
sockFile := getTempSockFile(b)
defer os.RemoveAll(sockFile)
lis := serve(b, net.Listen, "unix", sockFile, 0, 0, false)
benchmarkNetConnParallel(b, lis,
func(network, addr string) (net.Conn, error) {
client, err := dialUNIX(network, addr)
if err != nil {
return nil, err
}
deadline := time.Now().Add(time.Duration(1) * time.Minute)
client.SetDeadline(deadline)
return client, nil
})
}
var fixedData = []byte{0, 1, 2, 3, 4, 5, 6, 7}
func benchmarkNetConnParallel(
b *testing.B,
lis net.Listener,
dial func(string, string) (net.Conn, error)) {
defer lis.Close()
network, addr := lis.Addr().Network(), lis.Addr().String()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
writeBenchmarkData(b, network, addr, dial)
}
})
}
func writeBenchmarkData(
logger hasLoggerFuncs,
network, addr string,
dial func(string, string) (net.Conn, error)) {
client, err := dial(network, addr)
if err != nil {
logger.Fatal(err)
}
defer client.Close()
if n, err := client.Write(fixedData); err != nil {
logger.Fatal(err)
} else if n != dataLen {
logger.Fatalf("wrote != %d bytes: %d", dataLen, n)
}
if c, ok := client.(*net.TCPConn); ok {
c.SetLinger(0)
}
}