diff --git a/engine_test.go b/engine_test.go index 8be18c6..334d674 100644 --- a/engine_test.go +++ b/engine_test.go @@ -1,7 +1,9 @@ package liquid import ( + "bytes" "fmt" + "io" "testing" "github.com/stretchr/testify/require" @@ -63,3 +65,19 @@ func TestEngine_ParseAndRenderString_struct(t *testing.T) { require.NoError(t, err) require.Equal(t, "hello", str) } + +func BenchmarkEngine_Parse(b *testing.B) { + engine := NewEngine() + buf := new(bytes.Buffer) + for i := 0; i < 1000; i++ { + io.WriteString(buf, `if{% if true %}true{% elsif %}elsif{% else %}else{% endif %}`) + io.WriteString(buf, `loop{% for item in array %}loop{% break %}{% endfor %}`) + io.WriteString(buf, `case{% case value %}{% when a %}{% when b %{% endcase %}`) + io.WriteString(buf, `expr{{ a and b }}{{ a add: b }}`) + } + s := buf.Bytes() + b.ResetTimer() + for i := 0; i < b.N; i++ { + engine.ParseTemplate(s) + } +} diff --git a/template_test.go b/template_test.go index 18c7baa..1f16449 100644 --- a/template_test.go +++ b/template_test.go @@ -95,3 +95,16 @@ func TestTemplate_Render_race(t *testing.T) { } wg2.Wait() } + +func BenchmarkTemplate_Render(b *testing.B) { + engine := NewEngine() + bindings := Bindings{"a": "string value"} + tpl, err := engine.ParseString(`{% for i in (1..1000) %}{% if i > 500 %}{{a}}{% else %}0{% endif %}{% endfor %}`) + if err != nil { + b.Fatal(err) + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + tpl.Render(bindings) + } +}