test: add benchmark

This commit is contained in:
harttle
2019-02-23 21:57:02 +08:00
parent 37d12a14eb
commit 279458c670
11 changed files with 150 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
{
"rules": {
"no-unused-expressions": "off"
}
}
+49
View File
@@ -0,0 +1,49 @@
import * as Benchmark from 'benchmark'
import Liquid from 'src/liquid'
import TagToken from 'src/parser/tag-token'
import Scope from 'src/scope/scope'
const engine = new Liquid({
root: __dirname,
extname: '.liquid'
})
engine.registerTag('header', {
parse: function (token: TagToken) {
const [key, val] = token.args.split(':')
this[key] = val
},
render: function (scope: Scope) {
const title = this.liquid.evalValue(this.content, scope)
return `<h1>${title}</h1>`
}
})
const ctx = {
todos: ['fork and clone', 'make it better', 'make a pull request'],
title: 'Welcome to liquidjs!'
}
const template = `
{%header content: "welcome to liquid" | capitalize%}
<ul>
{% for todo in todos %}
<li>{{forloop.index}} - {{todo}}</li>
{% endfor %}
</ul>
`
export default function () {
console.log('--- demo ---')
return new Promise(resolve => {
new Benchmark.Suite('demo')
.add('demo', {
defer: true,
fn: (d: any) => engine.parseAndRender(template, ctx).then(x => d.resolve(x))
})
.on('cycle', (event: any) => console.log(String(event.target)))
.on('complete', resolve)
.run({ 'async': true })
})
}
+11
View File
@@ -0,0 +1,11 @@
import output from './output'
import tag from './tag'
import demo from './demo'
async function main () {
await output()
await tag()
await demo()
}
main()
+26
View File
@@ -0,0 +1,26 @@
import * as Benchmark from 'benchmark'
import Liquid from '../src/liquid'
const liquid = new Liquid()
export default function () {
console.log('--- output ---')
return new Promise(resolve => {
new Benchmark.Suite('output')
.add('literal', test('{{false}}{{"foo"}}{{32.322}}'))
.add('truncate', test('{{"foobar" | truncate: 3}}'))
.add('date', test('{{"now" | date: "%d%Y%m"}}'))
.add('escape', test('{{"1<2" | escape}}'))
.add('default', test('{{"" | default: 3}}'))
.on('cycle', (event: any) => console.log(String(event.target)))
.on('complete', resolve)
.run({ 'async': true })
})
}
function test (str: string) {
return {
defer: true,
fn: (d: any) => liquid.parseAndRender(str).then(x => d.resolve(x))
}
}
+30
View File
@@ -0,0 +1,30 @@
import * as Benchmark from 'benchmark'
import Liquid from '../src/liquid'
const liquid = new Liquid()
export default function () {
console.log('--- tag ---')
return new Promise(resolve => {
new Benchmark.Suite('tag')
.add('if', test('{% if "foobar" %}foo{% endif %}'))
.add('unless', test('{%unless "foo"%}true{%else%}false{%endunless%}'))
.add('for', test('{% for i in (1..3) %}{{fooloop.index}}{% endfor %}'))
.add('switch', test('{%case 3%}{% when 1 %}1{% when 2 %}2{% when 3 %}3{%endcase%}'))
.add('assign', test('{%assign a="foo bar"%}'))
.add('capture', test('{%capture foo%}what is this{%endcapture%}'))
.add('increment', test('{%increment a%}'))
.add('decrement', test('{%decrement a%}'))
.add('tablerow', test('{%tablerow i in (1..10) cols:3%}{%endtablerow%}'))
.on('cycle', (event: any) => console.log(String(event.target)))
.on('complete', resolve)
.run({ 'async': true })
})
}
function test (str: string) {
return {
defer: true,
fn: (d: any) => liquid.parseAndRender(str).then(x => d.resolve(x))
}
}