-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdsl_benchmark_test.go
More file actions
161 lines (137 loc) · 3.33 KB
/
dsl_benchmark_test.go
File metadata and controls
161 lines (137 loc) · 3.33 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package dsl
import (
"fmt"
"strings"
"testing"
"time"
"github.com/projectdiscovery/govaluate"
)
func BenchmarkDSLCaching(b *testing.B) {
// simple function that has some computational cost
computeFunc := func(args ...interface{}) (interface{}, error) {
time.Sleep(time.Microsecond)
return "computed value", nil
}
funcs := make(map[string]dslFunction)
funcs["cached_func"] = dslFunction{
IsCacheable: true,
Name: "cached_func",
NumberOfArgs: 1,
Signatures: nil,
ExpressionFunction: computeFunc,
}
funcs["uncached_func"] = dslFunction{
IsCacheable: false,
Name: "uncached_func",
NumberOfArgs: 1,
Signatures: nil,
ExpressionFunction: computeFunc,
}
exprs := make(map[string]govaluate.ExpressionFunction)
for name, fn := range funcs {
exprs[name] = fn.Exec
}
resultCache.Purge()
evaluateForBenchmark := func(b *testing.B, fn string, args ...any) any {
res, err := exprs[fn](args...)
if err != nil {
b.Fatalf("Error evaluating expression: %v", err)
}
return res
}
b.Run("Cached-single", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
evaluateForBenchmark(b, "cached_func", 1)
}
})
b.Run("Uncached-single", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
evaluateForBenchmark(b, "uncached_func", 1)
}
})
// test cache key generation
b.Run("Cached-diff", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
evaluateForBenchmark(b, "cached_func", i%100)
}
})
b.Run("Uncached-diff", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
evaluateForBenchmark(b, "uncached_func", i%100)
}
})
// test cache hit
b.Run("Cached-repeat", func(b *testing.B) {
// Pre-warm
for i := 0; i < 10; i++ {
evaluateForBenchmark(b, "cached_func", i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
evaluateForBenchmark(b, "cached_func", i%10)
}
})
b.Run("Uncached-repeat", func(b *testing.B) {
// Pre-warm
for i := 0; i < 10; i++ {
evaluateForBenchmark(b, "uncached_func", i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
evaluateForBenchmark(b, "uncached_func", i%10)
}
})
}
func BenchmarkDSLFunctionHash(b *testing.B) {
name := "test_func"
args := []any{
"arg1",
int(12345),
int8(-12),
int16(1234),
int32(-123456),
int64(1234567890),
uint(54321),
uint8(250),
uint16(54321),
uint32(1234567890),
uint64(9876543210),
float32(123.456),
float64(123.4567890123),
true,
"a-very-long-argument-to-make-hashing-non-trivial",
[]byte("some bytes"),
}
// NOTE(dwisiswant0): Copied from https://github.com/projectdiscovery/dsl/blob/3a6a901037affe56a31a3345573d8384d0ff5128/func.go#L70-L83
oldDSLFunctionHasher := func(name string, args ...interface{}) string {
var sb strings.Builder
_, _ = sb.WriteString(name)
_, _ = sb.WriteString("-")
for i, arg := range args {
_, _ = sb.WriteString(fmt.Sprintf("%v", arg))
if i < len(args)-1 {
_, _ = sb.WriteString(",")
}
}
return sb.String()
}
b.Run("builder", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
oldDSLFunctionHasher(name, args...)
}
})
b.Run("fnv", func(b *testing.B) {
d := dslFunction{Name: name}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = d.hash(args...)
}
})
}