-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_test.go
78 lines (66 loc) · 2.06 KB
/
benchmark_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
package quic
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"math/rand"
"net"
"time"
"github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/testdata"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Benchmarks", func() {
dataLen := 50 /* MB */ * (1 << 20)
data := make([]byte, dataLen)
rand.Seed(time.Now().UnixNano())
for i := range protocol.SupportedVersions {
version := protocol.SupportedVersions[i]
Context(fmt.Sprintf("with version %d", version), func() {
Measure("transferring a file", func(b Benchmarker) {
rand.Read(data) // no need to check for an error. math.Rand.Read never errors
var ln Listener
serverAddr := make(chan net.Addr)
// start the server
go func() {
defer GinkgoRecover()
var err error
ln, err = ListenAddr("localhost:0", &Config{TLSConfig: testdata.GetTLSConfig()})
Expect(err).ToNot(HaveOccurred())
serverAddr <- ln.Addr()
sess, err := ln.Accept()
Expect(err).ToNot(HaveOccurred())
str, err := sess.OpenStream()
Expect(err).ToNot(HaveOccurred())
_, err = str.Write(data)
Expect(err).ToNot(HaveOccurred())
err = str.Close()
Expect(err).ToNot(HaveOccurred())
}()
// start the client
conf := &Config{
TLSConfig: &tls.Config{InsecureSkipVerify: true},
}
addr := <-serverAddr
sess, err := DialAddr(addr.String(), conf)
Expect(err).ToNot(HaveOccurred())
str, err := sess.AcceptStream()
Expect(err).ToNot(HaveOccurred())
buf := &bytes.Buffer{}
// measure the time it takes to download the dataLen bytes
// note we're measuring the time for the transfer, i.e. excluding the handshake
runtime := b.Time("transfer time", func() {
_, err := io.Copy(buf, str)
Expect(err).NotTo(HaveOccurred())
})
// this is *a lot* faster than Expect(buf.Bytes()).To(Equal(data))
Expect(bytes.Equal(buf.Bytes(), data)).To(BeTrue())
b.RecordValue("transfer rate [MB/s]", float64(dataLen)/1e6/runtime.Seconds())
ln.Close()
sess.Close(nil)
}, 6)
})
}
})