-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
stream_timing_test.go
70 lines (62 loc) · 1.33 KB
/
stream_timing_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
package fasthttp
import (
"bufio"
"io"
"testing"
"time"
)
func BenchmarkStreamReader1(b *testing.B) {
benchmarkStreamReader(b, 1)
}
func BenchmarkStreamReader10(b *testing.B) {
benchmarkStreamReader(b, 10)
}
func BenchmarkStreamReader100(b *testing.B) {
benchmarkStreamReader(b, 100)
}
func BenchmarkStreamReader1K(b *testing.B) {
benchmarkStreamReader(b, 1000)
}
func BenchmarkStreamReader10K(b *testing.B) {
benchmarkStreamReader(b, 10000)
}
func benchmarkStreamReader(b *testing.B, size int) {
src := createFixedBody(size)
b.SetBytes(int64(size))
b.RunParallel(func(pb *testing.PB) {
dst := make([]byte, size)
ch := make(chan error, 1)
sr := NewStreamReader(func(w *bufio.Writer) {
for pb.Next() {
if _, err := w.Write(src); err != nil {
ch <- err
return
}
if err := w.Flush(); err != nil {
ch <- err
return
}
}
ch <- nil
})
for {
if _, err := sr.Read(dst); err != nil {
if err == io.EOF {
break
}
b.Fatalf("unexpected error when reading from stream reader: %v", err)
}
}
if err := sr.Close(); err != nil {
b.Fatalf("unexpected error when closing stream reader: %v", err)
}
select {
case err := <-ch:
if err != nil {
b.Fatalf("unexpected error from stream reader: %v", err)
}
case <-time.After(time.Second):
b.Fatalf("timeout")
}
})
}