Skip to content

Commit 1a0f4e9

Browse files
authored
add benchmark for text/template (valyala#76)
1 parent e459510 commit 1a0f4e9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/templates_timing_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"html/template"
77
"log"
88
"testing"
9+
texttemplate "text/template"
910

1011
"github.com/valyala/quicktemplate"
1112
"github.com/valyala/quicktemplate/testdata/templates"
1213
)
1314

1415
var tpl = template.Must(template.ParseFiles("../testdata/templates/bench.tpl"))
16+
var textTpl = texttemplate.Must(texttemplate.ParseFiles("../testdata/templates/bench.tpl"))
1517

1618
func init() {
1719
// make sure that both template engines generate the same result
@@ -80,6 +82,32 @@ func benchmarkHTMLTemplate(b *testing.B, rowsCount int) {
8082
})
8183
}
8284

85+
func BenchmarkTextTemplate1(b *testing.B) {
86+
benchmarkTextTemplate(b, 1)
87+
}
88+
89+
func BenchmarkTextTemplate10(b *testing.B) {
90+
benchmarkTextTemplate(b, 10)
91+
}
92+
93+
func BenchmarkTextTemplate100(b *testing.B) {
94+
benchmarkTextTemplate(b, 100)
95+
}
96+
97+
func benchmarkTextTemplate(b *testing.B, rowsCount int) {
98+
rows := getBenchRows(rowsCount)
99+
b.RunParallel(func(pb *testing.PB) {
100+
bb := quicktemplate.AcquireByteBuffer()
101+
for pb.Next() {
102+
if err := textTpl.Execute(bb, rows); err != nil {
103+
b.Fatalf("unexpected error: %s", err)
104+
}
105+
bb.Reset()
106+
}
107+
quicktemplate.ReleaseByteBuffer(bb)
108+
})
109+
}
110+
83111
func getBenchRows(n int) []templates.BenchRow {
84112
rows := make([]templates.BenchRow, n)
85113
for i := 0; i < n; i++ {

0 commit comments

Comments
 (0)