-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
syncthing_test.go
139 lines (118 loc) · 2.51 KB
/
syncthing_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
134
135
136
137
138
139
package utp
import (
"io"
"net"
"sync"
"testing"
"github.com/stretchr/testify/require"
)
func getTCPConnectionPair() (net.Conn, net.Conn, error) {
lst, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, nil, err
}
var conn0 net.Conn
var err0 error
done := make(chan struct{})
go func() {
conn0, err0 = lst.Accept()
close(done)
}()
conn1, err := net.Dial("tcp", lst.Addr().String())
if err != nil {
return nil, nil, err
}
<-done
if err0 != nil {
return nil, nil, err0
}
return conn0, conn1, nil
}
func getUTPConnectionPair() (net.Conn, net.Conn, error) {
lst, err := NewSocket("udp", "127.0.0.1:0")
if err != nil {
return nil, nil, err
}
defer lst.Close()
var conn0 net.Conn
var err0 error
done := make(chan struct{})
go func() {
conn0, err0 = lst.Accept()
close(done)
}()
conn1, err := Dial(lst.Addr().String())
if err != nil {
return nil, nil, err
}
<-done
if err0 != nil {
return nil, nil, err0
}
return conn0, conn1, nil
}
func requireWriteAll(t testing.TB, b []byte, w io.Writer) {
n, err := w.Write(b)
require.NoError(t, err)
require.EqualValues(t, len(b), n)
}
func requireReadExactly(t testing.TB, b []byte, r io.Reader) {
n, err := io.ReadFull(r, b)
require.NoError(t, err)
require.EqualValues(t, len(b), n)
}
func benchConnPair(b *testing.B, c0, c1 net.Conn) {
b.ReportAllocs()
request := make([]byte, 52)
response := make([]byte, (128<<10)+8)
b.SetBytes(int64(len(request) + len(response)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
c, s := func() (net.Conn, net.Conn) {
if i%2 == 0 {
return c0, c1
} else {
return c1, c0
}
}()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
requireWriteAll(b, request, c)
requireReadExactly(b, response[:8], c)
requireReadExactly(b, response[8:], c)
}()
go func() {
defer wg.Done()
requireReadExactly(b, request[:8], s)
requireReadExactly(b, request[8:], s)
requireWriteAll(b, response, s)
}()
wg.Wait()
}
}
func BenchmarkSyncthingTCP(b *testing.B) {
conn0, conn1, err := getTCPConnectionPair()
if err != nil {
b.Fatal(err)
}
defer conn0.Close()
defer conn1.Close()
benchConnPair(b, conn0, conn1)
}
func BenchmarkSyncthingUDPUTP(b *testing.B) {
conn0, conn1, err := getUTPConnectionPair()
if err != nil {
b.Fatal(err)
}
defer conn0.Close()
defer conn1.Close()
benchConnPair(b, conn0, conn1)
}
func BenchmarkSyncthingInprocUTP(b *testing.B) {
c0, c1 := connPair()
defer c0.Close()
defer c1.Close()
benchConnPair(b, c0, c1)
}