Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/common/variables/variables_bench_test.go
2843 views
1
package variables
2
3
import (
4
"fmt"
5
"testing"
6
7
"github.com/projectdiscovery/nuclei/v3/pkg/utils"
8
)
9
10
func BenchmarkVariableEvaluate(b *testing.B) {
11
// Setup variables with chained references and DSL functions
12
variables := &Variable{
13
LazyEval: true,
14
InsertionOrderedStringMap: *utils.NewEmptyInsertionOrderedStringMap(5),
15
}
16
variables.Set("base", "testvalue")
17
variables.Set("derived1", "{{base}}_suffix")
18
variables.Set("derived2", "{{md5(derived1)}}")
19
variables.Set("derived3", "prefix_{{derived2}}")
20
variables.Set("final", "{{derived3}}_end")
21
22
inputValues := map[string]interface{}{
23
"BaseURL": "http://example.com",
24
"Host": "example.com",
25
"Path": "/api/v1",
26
}
27
28
b.Run("Evaluate", func(b *testing.B) {
29
b.Run("5Variables", func(b *testing.B) {
30
b.ReportAllocs()
31
for b.Loop() {
32
_ = variables.Evaluate(inputValues)
33
}
34
})
35
36
b.Run("Parallel", func(b *testing.B) {
37
b.ReportAllocs()
38
b.RunParallel(func(pb *testing.PB) {
39
for pb.Next() {
40
_ = variables.Evaluate(inputValues)
41
}
42
})
43
})
44
})
45
}
46
47
func BenchmarkVariableEvaluateScaling(b *testing.B) {
48
// Test how the optimization scales with different variable counts
49
inputValues := map[string]interface{}{
50
"BaseURL": "http://example.com",
51
"Host": "example.com",
52
}
53
54
benchmarkSizes := []int{1, 5, 10, 20}
55
56
for _, size := range benchmarkSizes {
57
variables := &Variable{
58
LazyEval: true,
59
InsertionOrderedStringMap: *utils.NewEmptyInsertionOrderedStringMap(size),
60
}
61
62
// Create chain of variables
63
for i := range size {
64
varName := fmt.Sprintf("var%d", i)
65
if i == 0 {
66
variables.Set(varName, "initial")
67
} else {
68
prevVarName := fmt.Sprintf("var%d", i-1)
69
variables.Set(varName, fmt.Sprintf("{{%s}}_step", prevVarName))
70
}
71
}
72
73
b.Run(fmt.Sprintf("Variables-%d", size), func(b *testing.B) {
74
b.ReportAllocs()
75
for b.Loop() {
76
_ = variables.Evaluate(inputValues)
77
}
78
})
79
}
80
}
81
82