-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathparser_benchmark_test.go
More file actions
98 lines (86 loc) · 3.1 KB
/
Copy pathparser_benchmark_test.go
File metadata and controls
98 lines (86 loc) · 3.1 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
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
97
98
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package cti
import (
"crypto/md5"
"regexp"
"testing"
)
// ---------------------- Benchmarks ----------------------
var benchParseExprIdentifiers = []string{
"cti.a.p.gr.namespace.v1.2~a.p.integrations.datacenters.v2.1",
"cti.a.p.gr.namespace.v1.0~a.p.web_restore_user_links.v1.0",
"cti.a.p.stm.s3_buckets_pool.v1.0~my_vendor.my_app.assests.v1.0",
"cti.a.p.wm.workload.v1.0~a.p.wm.aspect.v1.0~a.p.machine.v1.0",
}
var benchParseExprWildcards = []string{
"cti.a.p.wr.report_config.v1.0~*",
"cti.a.p.gr.namespace.v1.0~a.p.web_restore.*",
"cti.a.p.wr.report_config.v1.0~a.p.mc.alerts_report.v1.0~a.p.mc.alerts_report.v1.*",
}
func Benchmark_Parse_Identifier(b *testing.B) {
p := NewParser()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := p.Parse(benchParseExprIdentifiers[i%len(benchParseExprIdentifiers)])
if err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Parse_Wildcard(b *testing.B) {
p := NewParser()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := p.Parse(benchParseExprWildcards[i%len(benchParseExprWildcards)])
if err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Parse_Query(b *testing.B) {
rawExp := `cti.a.p.am.alert.v1.0[type="cti.a.p.am.alert.v1.0~sophos.endpoint_protection.*"]`
p := NewParser()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := p.Parse(rawExp)
if err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Parse_IdentifierRegExp(b *testing.B) {
// RegExp source: https://github.com/acronis/go-cti/blob/main/metadata/ramlx/spec_v1/cti.raml#L116
regExp := regexp.MustCompile(`^cti\.([a-z][a-z0-9_]*\.[a-z][a-z0-9_]*\.[a-z_][a-z0-9_.]*\.v[\d]+\.[\d]+)(~([a-z][a-z0-9_]*\.[a-z][a-z0-9_]*\.[a-z_][a-z0-9_.]*\.v[\d]+\.[\d]+))*(~[0-9a-f]{8}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{12})?$`)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := benchParseExprIdentifiers[i%len(benchParseExprIdentifiers)]
if !regExp.MatchString(s) {
b.Fatalf("%q is not matched", s)
}
}
}
func Benchmark_Parse_WildcardRegExp(b *testing.B) {
// RegExp source: https://github.com/acronis/go-cti/blob/main/metadata/ramlx/spec_v1/cti.raml#L141
regExp := regexp.MustCompile(`^cti((\.([a-z][a-z0-9_]*))|\.)?(\.([a-z][a-z0-9_]*))?(\.([a-z_][a-z0-9_.]*))?(\.v(\d+|\d*\.\d*|\d*\.)?)?(~(([a-z][a-z0-9_]*)|([a-z][a-z0-9_]*)\.)?(\.([a-z][a-z0-9_]*))?(\.([a-z_][a-z0-9_.]*))?(\.v(\d+|\d*\.\d*|\d*\.)?)?)*\*$|^cti\.([a-z][a-z0-9_]*\.[a-z][a-z0-9_]*\.[a-z_][a-z0-9_.]*\.v[\d]+\.[\d]+)(~([a-z][a-z0-9_]*\.[a-z][a-z0-9_]*\.[a-z_][a-z0-9_.]*\.v[\d]+\.[\d]+))*(~[0-9a-f]{8}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{12})?$`)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := benchParseExprWildcards[i%len(benchParseExprWildcards)]
if !regExp.MatchString(s) {
b.Fatalf("%q is not matched", s)
}
}
}
// BenchmarkMD5 is added just for comparison.
func Benchmark_MD5(b *testing.B) {
expBytes := make([][]byte, len(benchParseExprIdentifiers))
for i := range expBytes {
expBytes[i] = []byte(benchParseExprIdentifiers[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = md5.Sum(expBytes[i%len(expBytes)])
}
}