Path: blob/dev/pkg/templates/compile_bench_test.go
2843 views
package templates_test12import (3"bytes"4"io"5"os"6"testing"78"github.com/projectdiscovery/nuclei/v3/pkg/templates"9)1011func BenchmarkParse(b *testing.B) {12filePath := "tests/match-1.yaml"1314setup()15b.ResetTimer()16b.ReportAllocs()1718for b.Loop() {19_, err := templates.Parse(filePath, nil, executerOpts)20if err != nil {21b.Fatalf("could not parse template: %s", err)22}23}24}2526func BenchmarkParseTemplateFromReader(b *testing.B) {27filePath := "tests/match-1.yaml"2829file, err := os.Open(filePath)30if err != nil {31b.Fatalf("could not open template file: %s", err)32}33defer func() {34_ = file.Close()35}()3637content, err := io.ReadAll(file)38if err != nil {39b.Fatalf("could not read template file: %s", err)40}4142setup()4344// Prepare the options with template path set.45//46// TODO(dwisiswant0): ParseTemplateFromReader should ideally work with just47// a reader without requiring path information, making it more flexible for48// in-memory templates or templates from non-file sources, the function49// unnecessarily couples the parsing logic to filepath info when it should50// primarily care about the content because it only needs a reader, but it51// actually requires path information in the options.52//53// The current implementation fails with a confusing error about template54// format detection, "no template name field provided", rather than55// explicitly stating that a path is required.56opts := executerOpts.Copy()57opts.TemplatePath = filePath5859b.ResetTimer()60b.ReportAllocs()6162for b.Loop() {63reader := bytes.NewReader(content)64_, err := templates.ParseTemplateFromReader(reader, nil, opts)65if err != nil {66b.Fatalf("could not parse template from reader: %s", err)67}68}69}707172