forked from anacrolix/go-libutp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench_test.go
50 lines (46 loc) · 1.01 KB
/
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
package utp
import (
"io"
"io/ioutil"
"net"
"testing"
"github.com/anacrolix/missinggo"
"github.com/bradfitz/iter"
"github.com/stretchr/testify/require"
)
func BenchmarkThroughput(t *testing.B) {
s1, err := NewSocket("udp", "localhost:0")
require.NoError(t, err)
defer s1.Close()
s2, err := NewSocket("udp", "localhost:0")
require.NoError(t, err)
defer s2.Close()
var c2 net.Conn
accepted := make(chan struct{})
go func() {
defer close(accepted)
c2, err = s2.Accept()
require.NoError(t, err)
}()
c1, err := s1.Dial(s2.Addr().String())
require.NoError(t, err)
defer c1.Close()
<-accepted
defer c2.Close()
var n int64 = 100 << 20
t.SetBytes(n)
for range iter.N(t.N) {
// log.Print(i)
doneReading := make(chan struct{})
go func() {
defer close(doneReading)
wn, err := io.CopyN(ioutil.Discard, c2, n)
require.NoError(t, err)
require.EqualValues(t, n, wn)
}()
wn, err := io.CopyN(c1, missinggo.ZeroReader, n)
require.NoError(t, err)
require.EqualValues(t, n, wn)
<-doneReading
}
}