-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
54 lines (46 loc) · 1.26 KB
/
main_test.go
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
package main_test
import (
"io"
"io/ioutil"
"testing"
"github.com/mh-cbon/template-compiler/compiled"
"github.com/mh-cbon/template-compiler/std/text/template"
"github.com/mh-cbon/template-compiler/std/text/template/parse"
)
var aJitTemplate *template.Template
var aCompiledTemplate *template.Compiled
var compiledTemplates *compiled.Registry
var s = []byte("r")
func init() {
var err error
aJitTemplate, err = template.ParseFiles("demo/templates/a.tpl")
if err != nil {
panic(err)
}
compiledTemplates = compiled.NewRegistry()
compiledTemplates.Add("a.tpl", fn0)
compiledTemplates.Add("b.tpl", fn1)
aCompiledTemplate = compiledTemplates.MustGet("a.tpl")
s = []byte("Hello from a!\n")
}
func fn0(t parse.Templater, w io.Writer, data interface {
}) error {
// io.WriteString(w, "Hello from a!\n")
w.Write(s) // write predefined bytes is a good optimization too.
return nil
}
func fn1(t parse.Templater, w io.Writer, data interface {
}) error {
io.WriteString(w, "Hello from b!\n")
return nil
}
func BenchmarkRenderWithCompiledTemplate(b *testing.B) {
for n := 0; n < b.N; n++ {
aCompiledTemplate.Execute(ioutil.Discard, nil)
}
}
func BenchmarkRenderWithJitTemplate(b *testing.B) {
for n := 0; n < b.N; n++ {
aJitTemplate.Execute(ioutil.Discard, nil)
}
}