-
Notifications
You must be signed in to change notification settings - Fork 1
/
syncthing_test.go
103 lines (92 loc) · 2.16 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
package utp
import (
"io"
"net"
"sync"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
func getTCPConnectionPair(t testing.TB) (c1, c2 net.Conn) {
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
eg := errgroup.Group{}
eg.Go(func() (err error) {
c1, err = l.Accept()
return err
})
eg.Go(func() (err error) {
c2, err = net.Dial("tcp", l.Addr().String())
return err
})
require.NoError(t, eg.Wait())
return c1, c2
}
func getUTPConnectionPair(t testing.TB) (c1, c2 net.Conn) {
s1, s2 := newTestSocket(t, newTestUDP(t)), newTestSocket(t, newTestUDP(t))
eg := errgroup.Group{}
eg.Go(func() error {
var err error
c1, err = s1.Accept()
return err
})
eg.Go(func() error {
var err error
c2, err = s2.DialContext(ctx, s1.LocalAddr())
return err
})
return c1, c2
}
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 := getTCPConnectionPair(b)
benchConnPair(b, conn0, conn1)
}
func BenchmarkSyncthingUDPUTP(b *testing.B) {
conn0, conn1 := getUTPConnectionPair(b)
benchConnPair(b, conn0, conn1)
}
func BenchmarkSyncthingInprocUTP(b *testing.B) {
c0, c1 := newTestConnPair(b)
benchConnPair(b, c0, c1)
}