-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlog_line_reader_test.go
96 lines (79 loc) · 2.27 KB
/
log_line_reader_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
package shuttle
import (
"io"
"sync"
"testing"
)
const (
TestProducerLines = 100000
)
var (
TestData = []byte("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n")
)
type InputProducer struct {
Total, Curr int
TotalBytes int
Data []byte
}
func NewInputProducer(c int) *InputProducer {
return &InputProducer{Total: c, Data: TestData}
}
func (llp *InputProducer) Read(p []byte) (n int, err error) {
if llp.Curr > llp.Total {
return 0, io.EOF
}
llp.Curr++
llp.TotalBytes += len(llp.Data)
return copy(p, llp.Data), nil
}
func (llp InputProducer) Close() error {
return nil
}
type TestConsumer struct {
sync.WaitGroup
}
func (tc *TestConsumer) Consume(in <-chan Batch) {
tc.Add(1)
go func() {
defer tc.Done()
for range in {
}
}()
}
func doBasicLogLineReaderBenchmark(b *testing.B, backBuffSize int) {
b.ResetTimer()
var tb int
var tc TestConsumer
for i := 0; i < b.N; i++ {
b.StopTimer()
batches := make(chan Batch, backBuffSize)
tc.Consume(batches)
s := NewShuttle(NewConfig())
llp := NewInputProducer(TestProducerLines)
rdr := NewLogLineReader(llp, s)
b.StartTimer()
rdr.ReadLines()
tb += llp.TotalBytes
close(batches)
tc.Wait()
}
b.SetBytes(int64(tb / b.N))
}
func BenchmarkLogLineReaderWithBackBuffEqual0(b *testing.B) {
doBasicLogLineReaderBenchmark(b, 0)
}
func BenchmarkLogLineReaderWithBackBuffEqual1(b *testing.B) {
doBasicLogLineReaderBenchmark(b, 1)
}
func BenchmarkLogLineReaderWithBackBuffEqual100(b *testing.B) {
doBasicLogLineReaderBenchmark(b, 100)
}
func BenchmarkLogLineReaderWithBackBuffEqual1000(b *testing.B) {
doBasicLogLineReaderBenchmark(b, 1000)
}
func BenchmarkLogLineReaderWithBackBuffEqual10000(b *testing.B) {
doBasicLogLineReaderBenchmark(b, 10000)
}
func BenchmarkLogLineReaderWithDefaultBackBuff(b *testing.B) {
doBasicLogLineReaderBenchmark(b, DefaultBackBuff)
}