Skip to content

Commit

Permalink
Benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jul 22, 2017
1 parent 5151799 commit 023fca4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package liquid

import (
"bytes"
"fmt"
"io"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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)
}
}
13 changes: 13 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 023fca4

Please sign in to comment.