-
Notifications
You must be signed in to change notification settings - Fork 53
/
benchmark_test.go
94 lines (79 loc) · 1.76 KB
/
benchmark_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
package humanlog
import (
"bytes"
"compress/gzip"
"context"
"io"
"io/fs"
"os"
"path/filepath"
"testing"
typesv1 "github.com/humanlogio/api/go/types/v1"
"github.com/stretchr/testify/require"
)
type NopSink struct{}
func (*NopSink) Receive(ctx context.Context, ev *typesv1.LogEvent) error {
return nil
}
func (*NopSink) Close(ctx context.Context) error {
return nil
}
func BenchmarkHarness(b *testing.B) {
ctx := context.Background()
root := "test/benchmark"
des, err := os.ReadDir(root)
if err != nil {
b.Fatal(err)
}
for _, de := range des {
if !de.IsDir() {
continue
}
dir := filepath.Join(root, de.Name())
fileName, err := findfirstMatchedFileName(dir, "*.gz")
require.NoError(b, err)
testCase := dir
b.Run(testCase, func(bb *testing.B) {
p := filepath.Join(dir, fileName)
f, err := os.Open(p)
require.NoError(bb, err)
defer f.Close()
gzipReader, err := gzip.NewReader(f)
require.NoError(bb, err)
src := bytes.NewBuffer(nil)
_, err = io.Copy(src, gzipReader)
require.NoError(bb, err)
sink := &NopSink{}
opt := DefaultOptions()
bb.SetBytes(int64(src.Len()))
for range bb.N {
copy := bytes.NewBuffer(src.Bytes())
bb.StartTimer()
_ = Scan(ctx, copy, sink, opt)
bb.StopTimer()
}
})
}
}
func findfirstMatchedFileName(dirPath string, pattern string) (string, error) {
firstMatched := ""
walkError := filepath.Walk(dirPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fileName := filepath.Base(path)
match, err := filepath.Match(pattern, fileName)
if err != nil {
return err
}
if match {
firstMatched = fileName
return filepath.SkipAll
}
return nil
})
return firstMatched, walkError
}