read file size and number of samples for benchmark test from flags

The values default to what we used previously.
This commit is contained in:
Marten Seemann
2017-07-25 11:16:58 +07:00
parent d108e10420
commit 30bcc48e52
2 changed files with 68 additions and 53 deletions

View File

@@ -1,6 +1,8 @@
package benchmark package benchmark
import ( import (
"flag"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@@ -11,3 +13,14 @@ func TestBenchmark(t *testing.T) {
RegisterFailHandler(Fail) RegisterFailHandler(Fail)
RunSpecs(t, "Benchmark Suite") RunSpecs(t, "Benchmark Suite")
} }
var (
size int // file size in MB, will be read from flags
samples int // number of samples for Measure, will be read from flags
)
func init() {
flag.IntVar(&size, "size", 50, "data length (in MB)")
flag.IntVar(&samples, "samples", 6, "number of samples")
flag.Parse()
}

View File

@@ -15,63 +15,65 @@ import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
var _ = Describe("Benchmarks", func() { func init() {
dataLen := 50 /* MB */ * (1 << 20) var _ = Describe("Benchmarks", func() {
data := make([]byte, dataLen) dataLen := size * /* MB */ (1 << 20)
rand.Seed(GinkgoRandomSeed()) data := make([]byte, dataLen)
rand.Read(data) // no need to check for an error. math.Rand.Read never errors rand.Seed(GinkgoRandomSeed())
rand.Read(data) // no need to check for an error. math.Rand.Read never errors
for i := range protocol.SupportedVersions { for i := range protocol.SupportedVersions {
version := protocol.SupportedVersions[i] version := protocol.SupportedVersions[i]
Context(fmt.Sprintf("with version %d", version), func() { Context(fmt.Sprintf("with version %d", version), func() {
Measure("transferring a file", func(b Benchmarker) { Measure(fmt.Sprintf("transferring a %d MB file", size), func(b Benchmarker) {
var ln quic.Listener var ln quic.Listener
serverAddr := make(chan net.Addr) serverAddr := make(chan net.Addr)
handshakeChan := make(chan struct{}) handshakeChan := make(chan struct{})
// start the server // start the server
go func() { go func() {
defer GinkgoRecover() defer GinkgoRecover()
var err error var err error
ln, err = quic.ListenAddr("localhost:0", testdata.GetTLSConfig(), nil) ln, err = quic.ListenAddr("localhost:0", testdata.GetTLSConfig(), nil)
Expect(err).ToNot(HaveOccurred())
serverAddr <- ln.Addr()
sess, err := ln.Accept()
Expect(err).ToNot(HaveOccurred())
// wait for the client to complete the handshake before sending the data
// this should not be necessary, but due to timing issues on the CIs, this is necessary to avoid sending too many undecryptable packets
<-handshakeChan
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
addr := <-serverAddr
sess, err := quic.DialAddr(addr.String(), &tls.Config{InsecureSkipVerify: true}, nil)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
serverAddr <- ln.Addr() close(handshakeChan)
sess, err := ln.Accept() str, err := sess.AcceptStream()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
// wait for the client to complete the handshake before sending the data
// this should not be necessary, but due to timing issues on the CIs, this is necessary to avoid sending too many undecryptable packets
<-handshakeChan
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 buf := &bytes.Buffer{}
addr := <-serverAddr // measure the time it takes to download the dataLen bytes
sess, err := quic.DialAddr(addr.String(), &tls.Config{InsecureSkipVerify: true}, nil) // note we're measuring the time for the transfer, i.e. excluding the handshake
Expect(err).ToNot(HaveOccurred()) runtime := b.Time("transfer time", func() {
close(handshakeChan) _, err := io.Copy(buf, str)
str, err := sess.AcceptStream() Expect(err).NotTo(HaveOccurred())
Expect(err).ToNot(HaveOccurred()) })
// this is *a lot* faster than Expect(buf.Bytes()).To(Equal(data))
Expect(bytes.Equal(buf.Bytes(), data)).To(BeTrue())
buf := &bytes.Buffer{} b.RecordValue("transfer rate [MB/s]", float64(dataLen)/1e6/runtime.Seconds())
// 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)
ln.Close() }, samples)
sess.Close(nil) })
}, 6) }
}) })
} }
})