Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/templates/compile_bench_test.go
2843 views
1
package templates_test
2
3
import (
4
"bytes"
5
"io"
6
"os"
7
"testing"
8
9
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
10
)
11
12
func BenchmarkParse(b *testing.B) {
13
filePath := "tests/match-1.yaml"
14
15
setup()
16
b.ResetTimer()
17
b.ReportAllocs()
18
19
for b.Loop() {
20
_, err := templates.Parse(filePath, nil, executerOpts)
21
if err != nil {
22
b.Fatalf("could not parse template: %s", err)
23
}
24
}
25
}
26
27
func BenchmarkParseTemplateFromReader(b *testing.B) {
28
filePath := "tests/match-1.yaml"
29
30
file, err := os.Open(filePath)
31
if err != nil {
32
b.Fatalf("could not open template file: %s", err)
33
}
34
defer func() {
35
_ = file.Close()
36
}()
37
38
content, err := io.ReadAll(file)
39
if err != nil {
40
b.Fatalf("could not read template file: %s", err)
41
}
42
43
setup()
44
45
// Prepare the options with template path set.
46
//
47
// TODO(dwisiswant0): ParseTemplateFromReader should ideally work with just
48
// a reader without requiring path information, making it more flexible for
49
// in-memory templates or templates from non-file sources, the function
50
// unnecessarily couples the parsing logic to filepath info when it should
51
// primarily care about the content because it only needs a reader, but it
52
// actually requires path information in the options.
53
//
54
// The current implementation fails with a confusing error about template
55
// format detection, "no template name field provided", rather than
56
// explicitly stating that a path is required.
57
opts := executerOpts.Copy()
58
opts.TemplatePath = filePath
59
60
b.ResetTimer()
61
b.ReportAllocs()
62
63
for b.Loop() {
64
reader := bytes.NewReader(content)
65
_, err := templates.ParseTemplateFromReader(reader, nil, opts)
66
if err != nil {
67
b.Fatalf("could not parse template from reader: %s", err)
68
}
69
}
70
}
71
72