From 5adeb1bba6ce6e8bd6bf09eb8e6ceeac09eddba9 Mon Sep 17 00:00:00 2001 From: Wuvist Date: Sat, 6 Jul 2019 19:13:45 +0800 Subject: [PATCH] Update bench test --- README.md | 28 ++++++++++++++-------------- tests/bench_test.go | 26 ++++++++++++++++++++++++++ tests/tpl/bench.tpl | 12 ++++++++++++ tests/tpl/index.go | 15 +++++++++------ tests/tpl/index.gohtml | 7 ++++--- 5 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 tests/tpl/bench.tpl diff --git a/README.md b/README.md index e0ff786..f7017f8 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tests/bench_test.go b/tests/bench_test.go index 2785031..127a901 100644 --- a/tests/bench_test.go +++ b/tests/bench_test.go @@ -1,10 +1,13 @@ package tests import ( + "bytes" "fmt" + "log" "reflect" "strings" "testing" + "text/template" "unsafe" "github.com/sipin/gorazor/tests/data" @@ -12,6 +15,29 @@ import ( "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) } diff --git a/tests/tpl/bench.tpl b/tests/tpl/bench.tpl new file mode 100644 index 0000000..426ce15 --- /dev/null +++ b/tests/tpl/bench.tpl @@ -0,0 +1,12 @@ + + test + + + + diff --git a/tests/tpl/index.go b/tests/tpl/index.go index 0c888c3..2d9750a 100644 --- a/tests/tpl/index.go +++ b/tests/tpl/index.go @@ -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 { @@ -18,18 +19,20 @@ func Index(rows []data.BenchRow) string { } func RenderIndex(_buffer io.StringWriter, rows []data.BenchRow) { - _buffer.WriteString("\n\n\ttest \n\t\n\t\t