Skip to content

Commit

Permalink
chore: perf diff against latest release
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Oct 2, 2021
1 parent 1689576 commit 0cb520d
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 17 deletions.
2 changes: 1 addition & 1 deletion benchmark/cross-engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const data = require('./data/todolist.json')
const path = require('path')

const engines = {
liquid: require('./engines/liquid'),
handlebars: require('./engines/handlebars'),
liquid: require('./engines/liquid'),
react: require('./engines/react'),
swig: require('./engines/swig')
}
Expand Down
47 changes: 47 additions & 0 deletions benchmark/diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env node
const { performance } = require('perf_hooks')
const path = require('path')

const FILE_LOCAL = process.argv[2]
const FILE_LATEST = process.argv[3]
console.log(`Local: ${FILE_LOCAL}`)
console.log(`Latest: ${FILE_LATEST}`)

const { createEngine } = require('./engines/create-liquid')
const local = createEngine(require(path.resolve(__dirname, '..', FILE_LOCAL)))
const latest = createEngine(require(path.resolve(__dirname, '..', FILE_LOCAL)))
const data = require('./data/todolist.json')
const tpl = path.resolve(__dirname, `templates/todolist`)

const begin = performance.now()
const tplLocal = local.load(tpl)
const tplLatest = latest.load(tpl)
const tasks = [
{
cycles: 0,
time: 0,
fn: () => latest.render(tplLatest, data)
},
{
cycles: 0,
time: 0,
fn: () => local.render(tplLocal, data)
}
]
while (performance.now() - begin < 20e3) {
const task = tasks[Math.floor(Math.random() * 2)]
task.time -= performance.now()
task.fn()
task.time += performance.now()
task.cycles++
}

const [ latestResult, localResult ] = tasks.map(task => {
task.perf = task.cycles * 1000 / task.time
return task
})
const diff = (localResult.perf - latestResult.perf) / latestResult.perf

console.log(`Local: ${localResult.perf.toFixed(3)} ops/s (${localResult.cycles} cycles)`)
console.log(`Latest: ${latestResult.perf.toFixed(3)} ops/s (${latestResult.cycles} cycles)`)
console.log(`Diff: ${(diff * 100).toFixed(3)}%`)
17 changes: 17 additions & 0 deletions benchmark/engines/create-liquid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { readFileSync } = require('fs')
const { join } = require('path')

function createEngine (pkg) {
const liquid = new pkg.Liquid({
root: join(__dirname, '../templates'),
cache: true,
extname: '.liquid'
})
liquid.registerFilter('url', path => `http://example.com${path}`)
return {
load: path => liquid.parse(readFileSync(path + '.liquid', 'utf8')),
render: (tpl, data) => liquid.renderSync(tpl, data)
}
}

module.exports = { createEngine }
17 changes: 2 additions & 15 deletions benchmark/engines/liquid.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
const { Liquid } = require('../..')
const { readFileSync } = require('fs')
const { join } = require('path')
const { createEngine } = require('./create-liquid')

const liquid = new Liquid({
root: join(__dirname, '../templates'),
cache: true,
extname: '.liquid'
})

liquid.registerFilter('url', path => `http://example.com${path}`)

module.exports = {
load: path => liquid.parse(readFileSync(path + '.liquid', 'utf8')),
render: (tpl, data) => liquid.renderSync(tpl, data)
}
module.exports = createEngine(require('../../dist/liquid.node.cjs'))
16 changes: 16 additions & 0 deletions bin/perf-diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/bash

VERSION_LATEST=$(cat package.json | grep '"version":' | awk -F'"' '{print $4}')
FILE_LOCAL=dist/liquid.node.cjs.js
FILE_LATEST=dist/liquid.node.cjs.$VERSION_LATEST.js
URL_LATEST=https://unpkg.com/liquidjs@$VERSION_LATEST/dist/liquid.node.cjs.js

if [ ! -f $FILE_LATEST ]; then
curl $URL_LATEST > $FILE_LATEST
fi

if [ ! -f $FILE_LOCAL ]; then
BUNDLES=cjs npm run build:dist
fi

node benchmark/diff.js $FILE_LOCAL $FILE_LATEST
Empty file added dist/.gitignore
Empty file.
2 changes: 2 additions & 0 deletions docs/themes/navy/layout/partial/all-contributors.swig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
<td align="center"><a href="https://github.com/ilhamdev0"><img src="https://avatars.githubusercontent.com/u/57636145?v=4?s=100" width="100px;" alt=""/></a></td>
<td align="center"><a href="https://github.com/c412216887"><img src="https://avatars.githubusercontent.com/u/29691650?v=4?s=100" width="100px;" alt=""/></a></td>
<td align="center"><a href="https://digitalinspiration.com/"><img src="https://avatars.githubusercontent.com/u/1344071?v=4?s=100" width="100px;" alt=""/></a></td>
<td align="center"><a href="https://n1ru4l.cloud/"><img src="https://avatars.githubusercontent.com/u/14338007?v=4?s=100" width="100px;" alt=""/></a></td>
<td align="center"><a href="https://github.com/mattvague"><img src="https://avatars.githubusercontent.com/u/64985?v=4?s=100" width="100px;" alt=""/></a></td>
</tr>
</table>

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"lint": "eslint \"**/*.ts\" .",
"check": "npm test && npm run lint",
"test": "nyc mocha \"test/**/*.ts\"",
"benchmark": "cd benchmark && npm ci && npm start",
"perf": "cd benchmark && npm ci && npm start",
"perf:diff": "bin/perf-diff.sh",
"perf:engines": "cd benchmark && npm run engines",
"build": "npm run build:dist && npm run build:docs",
"build:dist": "rm -rf dist && rollup -c rollup.config.ts && ls -lh dist",
"build:docs": "bin/build-docs.sh"
Expand Down

0 comments on commit 0cb520d

Please sign in to comment.