-
Notifications
You must be signed in to change notification settings - Fork 445
/
bandwidth_test.go
95 lines (78 loc) · 2.26 KB
/
bandwidth_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
package toxics_test
import (
"bytes"
"io"
"net"
"strings"
"testing"
"time"
"github.com/Shopify/toxiproxy/v2/testhelper"
"github.com/Shopify/toxiproxy/v2/toxics"
)
func TestBandwidthToxic(t *testing.T) {
upstream := testhelper.NewUpstream(t, false)
defer upstream.Close()
proxy := NewTestProxy("test", upstream.Addr())
proxy.Start()
defer proxy.Stop()
client, err := net.Dial("tcp", proxy.Listen)
if err != nil {
t.Fatalf("Unable to dial TCP server: %v", err)
}
upstreamConn := <-upstream.Connections
rate := 1000 // 1MB/s
proxy.Toxics.AddToxicJson(
ToxicToJson(t, "", "bandwidth", "upstream", &toxics.BandwidthToxic{Rate: int64(rate)}),
)
writtenPayload := []byte(strings.Repeat("hello world ", 40000)) // 480KB
go func() {
n, err := client.Write(writtenPayload)
client.Close()
if n != len(writtenPayload) || err != nil {
t.Errorf("Failed to write buffer: (%d == %d) %v", n, len(writtenPayload), err)
}
}()
serverRecvPayload := make([]byte, len(writtenPayload))
start := time.Now()
_, err = io.ReadAtLeast(upstreamConn, serverRecvPayload, len(serverRecvPayload))
if err != nil {
t.Errorf("Proxy read failed: %v", err)
} else if !bytes.Equal(writtenPayload, serverRecvPayload) {
t.Errorf("Server did not read correct buffer from client!")
}
AssertDeltaTime(t,
"Bandwidth",
time.Since(start),
time.Duration(len(writtenPayload))*time.Second/time.Duration(rate*1000),
10*time.Millisecond,
)
}
func BenchmarkBandwidthToxic100MB(b *testing.B) {
upstream := testhelper.NewUpstream(b, true)
defer upstream.Close()
proxy := NewTestProxy("test", upstream.Addr())
proxy.Start()
defer proxy.Stop()
client, err := net.Dial("tcp", proxy.Listen)
if err != nil {
b.Error("Unable to dial TCP server", err)
}
writtenPayload := []byte(strings.Repeat("hello world ", 1000))
proxy.Toxics.AddToxicJson(
ToxicToJson(nil, "", "bandwidth", "upstream", &toxics.BandwidthToxic{Rate: 100 * 1000}),
)
b.SetBytes(int64(len(writtenPayload)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
n, err := client.Write(writtenPayload)
if err != nil || n != len(writtenPayload) {
b.Errorf("%v, %d == %d", err, n, len(writtenPayload))
break
}
}
err = client.Close()
if err != nil {
b.Error("Failed to close TCP connection", err)
}
}