This repository has been archived by the owner on Dec 29, 2017. It is now read-only.
forked from anacrolix/utp
-
Notifications
You must be signed in to change notification settings - Fork 6
/
syncthing_test.go
131 lines (109 loc) · 2.23 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
package utp
import (
"net"
"testing"
)
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 benchConnPair(b *testing.B, c0, c1 net.Conn) {
b.ReportAllocs()
b.SetBytes(128 << 10)
b.ResetTimer()
request := make([]byte, 52)
response := make([]byte, (128<<10)+8)
pair := []net.Conn{c0, c1}
for i := 0; i < b.N; i++ {
if i%2 == 0 {
pair[0] = c0
pair[1] = c1
} else {
pair[0] = c1
pair[1] = c0
}
if _, err := pair[0].Write(request); err != nil {
b.Fatal(err)
}
if _, err := pair[1].Read(request[:8]); err != nil {
b.Fatal(err)
}
if _, err := pair[1].Read(request[8:]); err != nil {
b.Fatal(err)
}
if _, err := pair[1].Write(response); err != nil {
b.Fatal(err)
}
if _, err := pair[0].Read(response[:8]); err != nil {
b.Fatal(err)
}
if _, err := pair[0].Read(response[8:]); err != nil {
b.Fatal(err)
}
}
}
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)
}