Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address performance #42

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,76 @@ var toml = require('./index');
var fs = require('fs');
var data = fs.readFileSync('./test/example.toml', 'utf8');

var iterations = 1000;
const NS_PER_SEC = 1e9;
const toNs = function([a, b]) {
return a * NS_PER_SEC + b
}

const times = {
parse: 0,
compile: 0,
}

const pending = {}

function enter(rule) {
pending[rule] = pending[rule] || [];
pending[rule].push(process.hrtime());
}

function fail(rule) {
const last = pending[rule].pop();
const delta = process.hrtime(last);
const key = `${rule}.fail`
times[key] = times[key] || {count: 0, time: 0};
times[key].time += toNs(delta)
times[key].count++
}

function match(rule) {
const last = pending[rule].pop();
const delta = process.hrtime(last);
const key = `${rule}.match`
times[key] = times[key] || {count: 0, time: 0};
times[key].time += toNs(delta)
times[key].count++
}

const tracer = {
trace: function(evt) {
switch(evt.type) {
case 'rule.enter':
enter(evt.rule);
break;
case 'rule.fail':
fail(evt.rule);
break;
case 'rule.match':
match(evt.rule);
break;
}
}
}

var ITERATIONS = 1000;
var start = new Date();
for(var i = 0; i < iterations; i++) {
toml.parse(data);
for(var i = 0; i < ITERATIONS; i++) {
const info = toml._trace(data, tracer);
times.parse += info.parseTime
times.compile += info.compileTime
}
var end = new Date();
console.log("%s iterations in %sms", iterations, end - start);
console.log("%s iterations in %sms", ITERATIONS, end - start);
console.log(`Time spent parsing: ${times.parse}`)
console.log(`Time spent compiling: ${times.compile}`)
console.log('\npeg.js trace data:\n')
console.log('rule,type,count,total_time,avg_time')
Object.keys(times).forEach(key => {
if (typeof times[key] !== 'object') {
return;
}

const [rule, type] = key.split('.');
const {time, count} = times[key];
console.log(`${rule},${type},${count},${time},${time/count}`);
})
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,21 @@ module.exports = {
parse: function(input) {
var nodes = parser.parse(input.toString());
return compiler.compile(nodes);
},

_trace: function(input, tracer) {
var startParse = process.hrtime();
var nodes = parser.parse(input.toString(), {tracer: tracer});
var parseDelta = process.hrtime(startParse);

var startCompile = process.hrtime();
var result = compiler.compile(nodes);
var compileDelta = process.hrtime(startCompile);

return {
result: result,
parseTime: (parseDelta[0] * 1e9 + parseDelta[1]) / 1000 / 1000,
compileTime: (compileDelta[0] * 1e9 + compileDelta[1]) / 1000 / 1000
}
}
};
Loading