Path: blob/dev/pkg/protocols/common/variables/variables_bench_test.go
2843 views
package variables12import (3"fmt"4"testing"56"github.com/projectdiscovery/nuclei/v3/pkg/utils"7)89func BenchmarkVariableEvaluate(b *testing.B) {10// Setup variables with chained references and DSL functions11variables := &Variable{12LazyEval: true,13InsertionOrderedStringMap: *utils.NewEmptyInsertionOrderedStringMap(5),14}15variables.Set("base", "testvalue")16variables.Set("derived1", "{{base}}_suffix")17variables.Set("derived2", "{{md5(derived1)}}")18variables.Set("derived3", "prefix_{{derived2}}")19variables.Set("final", "{{derived3}}_end")2021inputValues := map[string]interface{}{22"BaseURL": "http://example.com",23"Host": "example.com",24"Path": "/api/v1",25}2627b.Run("Evaluate", func(b *testing.B) {28b.Run("5Variables", func(b *testing.B) {29b.ReportAllocs()30for b.Loop() {31_ = variables.Evaluate(inputValues)32}33})3435b.Run("Parallel", func(b *testing.B) {36b.ReportAllocs()37b.RunParallel(func(pb *testing.PB) {38for pb.Next() {39_ = variables.Evaluate(inputValues)40}41})42})43})44}4546func BenchmarkVariableEvaluateScaling(b *testing.B) {47// Test how the optimization scales with different variable counts48inputValues := map[string]interface{}{49"BaseURL": "http://example.com",50"Host": "example.com",51}5253benchmarkSizes := []int{1, 5, 10, 20}5455for _, size := range benchmarkSizes {56variables := &Variable{57LazyEval: true,58InsertionOrderedStringMap: *utils.NewEmptyInsertionOrderedStringMap(size),59}6061// Create chain of variables62for i := range size {63varName := fmt.Sprintf("var%d", i)64if i == 0 {65variables.Set(varName, "initial")66} else {67prevVarName := fmt.Sprintf("var%d", i-1)68variables.Set(varName, fmt.Sprintf("{{%s}}_step", prevVarName))69}70}7172b.Run(fmt.Sprintf("Variables-%d", size), func(b *testing.B) {73b.ReportAllocs()74for b.Loop() {75_ = variables.Evaluate(inputValues)76}77})78}79}808182