1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 07:58:58 +01:00

Benchmarks

This commit is contained in:
Oliver Steele 2017-07-22 08:04:09 -04:00
parent 51517990c9
commit 023fca4617
2 changed files with 31 additions and 0 deletions

View File

@ -1,7 +1,9 @@
package liquid package liquid
import ( import (
"bytes"
"fmt" "fmt"
"io"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -63,3 +65,19 @@ func TestEngine_ParseAndRenderString_struct(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "hello", str) 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)
}
}

View File

@ -95,3 +95,16 @@ func TestTemplate_Render_race(t *testing.T) {
} }
wg2.Wait() 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)
}
}