-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcompile_bench_test.go
More file actions
70 lines (58 loc) · 1.68 KB
/
compile_bench_test.go
File metadata and controls
70 lines (58 loc) · 1.68 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
package templates_test
import (
"bytes"
"io"
"os"
"testing"
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
)
func BenchmarkParse(b *testing.B) {
filePath := "tests/match-1.yaml"
setup()
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, err := templates.Parse(filePath, nil, executerOpts)
if err != nil {
b.Fatalf("could not parse template: %s", err)
}
}
}
func BenchmarkParseTemplateFromReader(b *testing.B) {
filePath := "tests/match-1.yaml"
file, err := os.Open(filePath)
if err != nil {
b.Fatalf("could not open template file: %s", err)
}
defer func() {
_ = file.Close()
}()
content, err := io.ReadAll(file)
if err != nil {
b.Fatalf("could not read template file: %s", err)
}
setup()
// Prepare the options with template path set.
//
// TODO(dwisiswant0): ParseTemplateFromReader should ideally work with just
// a reader without requiring path information, making it more flexible for
// in-memory templates or templates from non-file sources, the function
// unnecessarily couples the parsing logic to filepath info when it should
// primarily care about the content because it only needs a reader, but it
// actually requires path information in the options.
//
// The current implementation fails with a confusing error about template
// format detection, "no template name field provided", rather than
// explicitly stating that a path is required.
opts := executerOpts.Copy()
opts.TemplatePath = filePath
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
reader := bytes.NewReader(content)
_, err := templates.ParseTemplateFromReader(reader, nil, opts)
if err != nil {
b.Fatalf("could not parse template from reader: %s", err)
}
}
}