forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
62 lines (52 loc) · 1.25 KB
/
benchmark_test.go
File metadata and controls
62 lines (52 loc) · 1.25 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package waf
import (
"bytes"
"os"
"testing"
gojson "github.com/goccy/go-json"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
)
// newWAFLogContent reads the testdata/valid_log.json file, and creates
// a byte array with nLogs line, in which each line is the compacted
// log.
func newWAFLogContent(b *testing.B, nLogs int) []byte {
data, err := os.ReadFile("testdata/valid_log.json")
require.NoError(b, err)
compacted := bytes.NewBuffer([]byte{})
err = gojson.Compact(compacted, data)
require.NoError(b, err)
compactedBytes := compacted.Bytes()
result := make([][]byte, nLogs)
for i := range nLogs {
result[i] = compactedBytes
}
return bytes.Join(result, []byte{'\n'})
}
func BenchmarkUnmarshalLogs(b *testing.B) {
tests := map[string]struct {
nLogs int
}{
"1_log": {
nLogs: 1,
},
"1000_logs": {
nLogs: 1_000,
},
}
u := WafLogUnmarshaler{
buildInfo: component.BuildInfo{},
}
for name, test := range tests {
data := newWAFLogContent(b, test.nLogs)
b.Run(name, func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, err := u.UnmarshalAWSLogs(bytes.NewReader(data))
require.NoError(b, err)
}
})
}
}