-
Notifications
You must be signed in to change notification settings - Fork 8
/
throughput_test.go
148 lines (126 loc) · 3.97 KB
/
throughput_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//go:build !race
// SPDX-License-Identifier: Apache-2.0
package frisbee
import (
"bufio"
"io"
"net"
"testing"
"time"
"github.com/loopholelabs/logging"
"github.com/loopholelabs/testing/conn/pair"
)
func BenchmarkAsyncThroughputLarge(b *testing.B) {
const testSize = 100
emptyLogger := logging.Test(b, logging.Noop, b.Name())
reader, writer, err := pair.New()
if err != nil {
b.Fatal(err)
}
readerConn := NewAsync(reader, emptyLogger)
writerConn := NewAsync(writer, emptyLogger)
b.Run("1MB", throughputRunner(testSize, 1<<20, readerConn, writerConn))
b.Run("2MB", throughputRunner(testSize, 1<<21, readerConn, writerConn))
b.Run("4MB", throughputRunner(testSize, 1<<22, readerConn, writerConn))
b.Run("8MB", throughputRunner(testSize, 1<<23, readerConn, writerConn))
b.Run("16MB", throughputRunner(testSize, 1<<24, readerConn, writerConn))
_ = readerConn.Close()
_ = writerConn.Close()
}
func BenchmarkSyncThroughputLarge(b *testing.B) {
const testSize = 100
emptyLogger := logging.Test(b, logging.Noop, b.Name())
reader, writer, err := pair.New()
if err != nil {
b.Fatal(err)
}
readerConn := NewSync(reader, emptyLogger)
writerConn := NewSync(writer, emptyLogger)
b.Run("1MB", throughputRunner(testSize, 1<<20, readerConn, writerConn))
b.Run("2MB", throughputRunner(testSize, 1<<21, readerConn, writerConn))
b.Run("4MB", throughputRunner(testSize, 1<<22, readerConn, writerConn))
b.Run("8MB", throughputRunner(testSize, 1<<23, readerConn, writerConn))
b.Run("16MB", throughputRunner(testSize, 1<<24, readerConn, writerConn))
_ = readerConn.Close()
_ = writerConn.Close()
}
func BenchmarkTCPThroughput(b *testing.B) {
const testSize = 100
reader, writer, err := pair.New()
if err != nil {
b.Fatal(err)
}
TCPThroughputRunner := func(testSize uint32, packetSize uint32, readerConn net.Conn, writerConn net.Conn) func(*testing.B) {
bufWriter := bufio.NewWriter(writerConn)
bufReader := bufio.NewReader(readerConn)
return func(b *testing.B) {
b.SetBytes(int64(testSize * packetSize))
b.ReportAllocs()
var err error
randomData := make([]byte, packetSize)
readData := make([]byte, packetSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
done := make(chan struct{}, 1)
errCh := make(chan error, 1)
go func() {
for i := uint32(0); i < testSize; i++ {
err := readerConn.SetReadDeadline(time.Now().Add(DefaultDeadline))
if err != nil {
errCh <- err
return
}
_, err = io.ReadAtLeast(bufReader, readData[0:], int(packetSize))
if err != nil {
errCh <- err
return
}
}
done <- struct{}{}
}()
for i := uint32(0); i < testSize; i++ {
select {
case err = <-errCh:
b.Fatal(err)
default:
err = writerConn.SetWriteDeadline(time.Now().Add(DefaultDeadline))
if err != nil {
b.Fatal(err)
}
_, err = bufWriter.Write(randomData)
if err != nil {
b.Fatal(err)
}
}
}
err = writerConn.SetWriteDeadline(time.Now().Add(DefaultDeadline))
if err != nil {
b.Fatal(err)
}
err = bufWriter.Flush()
if err != nil {
b.Fatal(err)
}
select {
case <-done:
continue
case err := <-errCh:
b.Fatal(err)
}
}
b.StopTimer()
}
}
b.Run("32 Bytes", TCPThroughputRunner(testSize, 32, reader, writer))
b.Run("512 Bytes", TCPThroughputRunner(testSize, 512, reader, writer))
b.Run("1024 Bytes", TCPThroughputRunner(testSize, 1024, reader, writer))
b.Run("2048 Bytes", TCPThroughputRunner(testSize, 2048, reader, writer))
b.Run("4096 Bytes", TCPThroughputRunner(testSize, 4096, reader, writer))
b.Run("1MB", TCPThroughputRunner(testSize, 1<<20, reader, writer))
b.Run("2MB", TCPThroughputRunner(testSize, 1<<21, reader, writer))
b.Run("4MB", TCPThroughputRunner(testSize, 1<<22, reader, writer))
b.Run("8MB", TCPThroughputRunner(testSize, 1<<23, reader, writer))
b.Run("16MB", TCPThroughputRunner(testSize, 1<<24, reader, writer))
_ = reader.Close()
_ = writer.Close()
}