Skip to content

Commit 227948a

Browse files
authored
Add auparse and aucoalesce benchmark (#109)
Add benchmarks based on logs from testdata/. goos: darwin goarch: amd64 pkg: github.com/elastic/go-libaudit/v2/aucoalesce cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz BenchmarkCoalesceMessages-12 1655539 3612 ns/op 2718 B/op 13 allocs/op PASS ok github.com/elastic/go-libaudit/v2/aucoalesce 9.707s goos: darwin goarch: amd64 pkg: github.com/elastic/go-libaudit/v2/auparse cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz BenchmarkParseAuditHeader-12 50767164 115.8 ns/op 21 B/op 3 allocs/op BenchmarkParseAuditHeaderRegex-12 11188456 539.0 ns/op 128 B/op 2 allocs/op BenchmarkParseLogLine-12 18334200 330.6 ns/op 374 B/op 4 allocs/op
1 parent e4c5ac6 commit 227948a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

aucoalesce/coalesce_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"testing"
3030

3131
"github.com/stretchr/testify/assert"
32+
"github.com/stretchr/testify/require"
3233
"gopkg.in/yaml.v2"
3334

3435
"github.com/elastic/go-libaudit/v2/auparse"
@@ -208,3 +209,19 @@ func normalizeEvent(t testing.TB, event testEventOutput) map[string]interface{}
208209
}
209210
return out
210211
}
212+
213+
func BenchmarkCoalesceMessages(b *testing.B) {
214+
files, err := filepath.Glob("testdata/*.yaml")
215+
require.NoError(b, err)
216+
var events []testEvent
217+
for _, f := range files {
218+
events = append(events, readEventsFromYAML(b, f)...)
219+
}
220+
221+
b.ResetTimer()
222+
for i := 0; i < b.N; i++ {
223+
if _, err := CoalesceMessages(events[i%len(events)].messages); err != nil {
224+
b.Fatal(err)
225+
}
226+
}
227+
}

auparse/auparse_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ import (
2727
"path/filepath"
2828
"regexp"
2929
"strconv"
30+
"strings"
3031
"testing"
3132
"time"
3233

3334
"github.com/stretchr/testify/assert"
35+
"github.com/stretchr/testify/require"
3436
)
3537

3638
var update = flag.Bool("update", false, "update .golden files")
@@ -319,6 +321,30 @@ func BenchmarkParseAuditHeaderRegex(b *testing.B) {
319321
}
320322
}
321323

324+
func BenchmarkParseLogLine(b *testing.B) {
325+
// Build corpus of valid messages from the test data.
326+
files, err := filepath.Glob("testdata/*.log")
327+
require.NoError(b, err)
328+
var msgs []string
329+
for _, f := range files {
330+
data, err := ioutil.ReadFile(f)
331+
require.NoError(b, err)
332+
for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") {
333+
if _, err = ParseLogLine(line); err == nil {
334+
msgs = append(msgs, line)
335+
}
336+
}
337+
}
338+
339+
b.ResetTimer()
340+
for i := 0; i < b.N; i++ {
341+
msg := msgs[i%len(msgs)]
342+
if _, err = ParseLogLine(msg); err != nil {
343+
b.Fatal(err)
344+
}
345+
}
346+
}
347+
322348
// ExampleParseLogLine demonstrates parsing a log line from auditd and shows
323349
// what the parsed data looks like.
324350
func ExampleParseLogLine() {

0 commit comments

Comments
 (0)