Skip to content

Commit

Permalink
Update bench test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuvist committed Jul 6, 2019
1 parent a78d9ed commit 5adeb1b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ $ go test -bench='Benchmark(Razor|RazorQuick|Quick|HTML)Template' -benchmem gith
goos: windows
goarch: amd64
pkg: github.com/valyala/quicktemplate/tests
BenchmarkQuickTemplate1-8 30000000 50.6 ns/op 0 B/op 0 allocs/op
BenchmarkQuickTemplate10-8 5000000 312 ns/op 0 B/op 0 allocs/op
BenchmarkQuickTemplate100-8 500000 2661 ns/op 0 B/op 0 allocs/op
BenchmarkHTMLTemplate1-8 1000000 1433 ns/op 608 B/op 21 allocs/op
BenchmarkHTMLTemplate10-8 200000 7085 ns/op 2834 B/op 111 allocs/op
BenchmarkHTMLTemplate100-8 20000 69906 ns/op 28058 B/op 1146 allocs/op
BenchmarkQuickTemplate1-8 30000000 37.5 ns/op 0 B/op 0 allocs/op
BenchmarkQuickTemplate10-8 10000000 146 ns/op 0 B/op 0 allocs/op
BenchmarkQuickTemplate100-8 1000000 1355 ns/op 0 B/op 0 allocs/op
BenchmarkHTMLTemplate1-8 2000000 708 ns/op 608 B/op 21 allocs/op
BenchmarkHTMLTemplate10-8 500000 3587 ns/op 2834 B/op 111 allocs/op
BenchmarkHTMLTemplate100-8 50000 34619 ns/op 28055 B/op 1146 allocs/op
PASS
ok github.com/valyala/quicktemplate/tests 10.134s
ok github.com/valyala/quicktemplate/tests 10.452s
goos: windows
goarch: amd64
pkg: github.com/sipin/gorazor/tests
BenchmarkRazorTemplate1-8 20000000 117 ns/op 240 B/op 4 allocs/op
BenchmarkRazorTemplate10-8 5000000 362 ns/op 592 B/op 13 allocs/op
BenchmarkRazorTemplate100-8 500000 3160 ns/op 5256 B/op 106 allocs/op
BenchmarkRazorQuickTemplate1-8 30000000 49.8 ns/op 16 B/op 1 allocs/op
BenchmarkRazorQuickTemplate10-8 5000000 254 ns/op 112 B/op 9 allocs/op
BenchmarkRazorQuickTemplate100-8 500000 2555 ns/op 1192 B/op 99 allocs/op
BenchmarkRazorTemplate1-8 20000000 80.4 ns/op 480 B/op 4 allocs/op
BenchmarkRazorTemplate10-8 10000000 188 ns/op 992 B/op 5 allocs/op
BenchmarkRazorTemplate100-8 1000000 1693 ns/op 10208 B/op 9 allocs/op
BenchmarkRazorQuickTemplate1-8 100000000 19.7 ns/op 0 B/op 0 allocs/op
BenchmarkRazorQuickTemplate10-8 20000000 80.1 ns/op 0 B/op 0 allocs/op
BenchmarkRazorQuickTemplate100-8 2000000 746 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/sipin/gorazor/tests 11.109s
ok github.com/sipin/gorazor/tests 11.654s
```

# Usage
Expand Down
26 changes: 26 additions & 0 deletions tests/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
package tests

import (
"bytes"
"fmt"
"log"
"reflect"
"strings"
"testing"
"text/template"
"unsafe"

"github.com/sipin/gorazor/tests/data"
"github.com/sipin/gorazor/tests/tpl"
"github.com/valyala/quicktemplate"
)

var htmltpl = template.Must(template.ParseFiles("tpl/bench.tpl"))

func init() {
// make sure that both template engines generate the same result
rows := getBenchRows(3)

bb1 := &quicktemplate.ByteBuffer{}
if err := htmltpl.Execute(bb1, rows); err != nil {
log.Fatalf("unexpected error: %s", err)
}

bb := quicktemplate.AcquireByteBuffer()
q := &quickStringWriter{}
q.bb = bb
tpl.RenderIndex(q, rows)

if !bytes.Equal(bb1.B, bb.B) {
log.Fatalf("results mismatch:\n%q\n%q", bb1, bb)
quicktemplate.ReleaseByteBuffer(bb)
}
quicktemplate.ReleaseByteBuffer(bb)
}

func BenchmarkRazorTemplate1(b *testing.B) {
benchmarkRazorTemplate(b, 1)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/tpl/bench.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head><title>test</title></head>
<body>
<ul>
{{range .}}
{{if .Print}}
<li>ID={{.ID}}, Message={{.Message}}</li>
{{end}}
{{end}}
</ul>
</body>
</html>
15 changes: 9 additions & 6 deletions tests/tpl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package tpl

import (
"github.com/sipin/gorazor/gorazor"
"github.com/sipin/gorazor/tests/data"
"io"
"strings"

"github.com/sipin/gorazor/gorazor"
"github.com/sipin/gorazor/tests/data"
)

func Index(rows []data.BenchRow) string {
Expand All @@ -18,18 +19,20 @@ func Index(rows []data.BenchRow) string {
}

func RenderIndex(_buffer io.StringWriter, rows []data.BenchRow) {
_buffer.WriteString("\n<html>\n\t<head><title>test</title></head>\n\t<body>\n\t\t<ul>\n\t\t")
_buffer.WriteString("<html>\n\t<head><title>test</title></head>\n\t<body>\n\t\t<ul>\n\t\t\n\t\t\t")
for _, row := range rows {
if row.Print {

_buffer.WriteString("<li>ID=")
_buffer.WriteString("\n\t\t\t\t<li>ID=")
_buffer.WriteString(gorazor.HTMLEscInt(row.ID))
_buffer.WriteString(", Message=")
_buffer.WriteString(gorazor.HTMLEscStr(row.Message))
_buffer.WriteString("</li>")
_buffer.WriteString("</li>\n\t\t\t\n\t\t\n\t\t")

} else {
_buffer.WriteString("\t\n\t\t\n\t\t\t")
}
}
_buffer.WriteString("\n\t\t</ul>\n\t</body>\n</html")
_buffer.WriteString("</ul>\n\t</body>\n</html>\n")

}
7 changes: 4 additions & 3 deletions tests/tpl/index.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
import "github.com/sipin/gorazor/tests/data"

var rows []data.BenchRow
}
<html>
}<html>
<head><title>test</title></head>
<body>
<ul>
@for _, row := range rows {
if row.Print {

<li>ID=@row.ID, Message=@row.Message</li>

}
}
</ul>
</body>
</html
</html>

0 comments on commit 5adeb1b

Please sign in to comment.