pako/benchmark/benchmark.js

162 wiersze
3.7 KiB
JavaScript
Czysty Zwykły widok Historia

2014-02-03 12:24:19 +00:00
#!/usr/bin/env node
'use strict';
2014-02-18 00:32:01 +00:00
var LEVEL = 6;
2014-02-03 12:24:19 +00:00
var path = require('path');
var fs = require('fs');
var util = require('util');
var Benchmark = require('benchmark');
var ansi = require('ansi');
var cursor = ansi(process.stdout);
2014-02-27 08:00:58 +00:00
var pako = require('../');
2014-02-03 12:24:19 +00:00
var IMPLS_DIRECTORY = path.join(__dirname, 'implementations');
var IMPLS_PATHS = {};
var IMPLS = [];
fs.readdirSync(IMPLS_DIRECTORY).sort().forEach(function (name) {
var file = path.join(IMPLS_DIRECTORY, name);
var code = require(file);
2014-02-03 12:24:19 +00:00
IMPLS_PATHS[name] = file;
IMPLS.push({
name: name,
code: code
});
});
var SAMPLES_DIRECTORY = path.join(__dirname, 'samples');
var SAMPLES = [];
fs.readdirSync(SAMPLES_DIRECTORY).sort().forEach(function (sample) {
var filepath = path.join(SAMPLES_DIRECTORY, sample),
extname = path.extname(filepath),
2014-02-27 08:00:58 +00:00
basename = path.basename(filepath, extname);
var content = {}; // raw/compressed data in different formats
content.buffer = fs.readFileSync(filepath);
content.typed = new Uint8Array(content.buffer);
2014-04-08 14:55:17 +00:00
content.string = fs.readFileSync(filepath, 'utf8');
2014-02-27 08:00:58 +00:00
content.deflateTyped = pako.deflate(content.typed, { level: LEVEL });
content.deflateBuffer = new Buffer(content.deflateTyped);
2014-04-08 14:55:17 +00:00
content.deflateString = pako.deflate(content.typed, { level: LEVEL, to: 'string' });
2014-02-27 08:00:58 +00:00
content.deflateRawTyped = pako.deflateRaw(content.typed, { level: LEVEL });
content.deflateRawBuffer = new Buffer(content.deflateRawTyped);
var title = util.format('(%d bytes raw / ~%d bytes compressed)', content.typed.length, content.deflateTyped.length);
2014-02-03 12:24:19 +00:00
function onComplete() {
cursor.write('\n');
}
2014-02-03 13:30:44 +00:00
2014-02-03 12:24:19 +00:00
var suite = new Benchmark.Suite(title, {
2014-02-03 13:30:44 +00:00
onStart: function onStart() {
2014-02-27 08:00:58 +00:00
console.log('\nSample: %s %s', sample, title);
2014-02-03 13:30:44 +00:00
},
2014-02-03 12:24:19 +00:00
onComplete: onComplete
2014-02-03 13:30:44 +00:00
2014-02-03 12:24:19 +00:00
});
2014-02-03 13:30:44 +00:00
2014-02-03 12:24:19 +00:00
IMPLS.forEach(function (impl) {
suite.add(impl.name, {
2014-02-03 13:30:44 +00:00
onCycle: function onCycle(event) {
cursor.horizontalAbsolute();
cursor.eraseLine();
cursor.write(' > ' + event.target);
},
2014-02-03 12:24:19 +00:00
onComplete: onComplete,
2014-02-03 13:30:44 +00:00
2014-02-03 12:24:19 +00:00
defer: !!impl.code.async,
2014-02-03 13:30:44 +00:00
2014-02-03 12:24:19 +00:00
fn: function (deferred) {
if (impl.code.async) {
2014-02-18 00:32:01 +00:00
impl.code.run(content, LEVEL, function() {
2014-02-03 12:24:19 +00:00
deferred.resolve();
return;
});
} else {
2014-02-18 00:32:01 +00:00
impl.code.run(content, LEVEL);
2014-02-03 12:24:19 +00:00
return;
}
}
});
});
2014-02-03 13:30:44 +00:00
2014-02-03 12:24:19 +00:00
SAMPLES.push({
name: basename,
title: title,
content: content,
suite: suite
});
});
function select(patterns) {
var result = [];
if (!(patterns instanceof Array)) {
patterns = [ patterns ];
}
function checkName(name) {
return patterns.length === 0 || patterns.some(function (regexp) {
return regexp.test(name);
});
}
SAMPLES.forEach(function (sample) {
if (checkName(sample.name)) {
result.push(sample);
}
});
return result;
}
function run(files) {
var selected = select(files);
if (selected.length > 0) {
console.log('Selected samples: (%d of %d)', selected.length, SAMPLES.length);
selected.forEach(function (sample) {
console.log(' > %s', sample.name);
});
} else {
2014-02-03 13:30:44 +00:00
console.log('There isn\'t any sample matches any of these patterns: %s', util.inspect(files));
2014-02-03 12:24:19 +00:00
}
selected.forEach(function (sample) {
sample.suite.run();
});
}
module.exports.IMPLS_DIRECTORY = IMPLS_DIRECTORY;
module.exports.IMPLS_PATHS = IMPLS_PATHS;
module.exports.IMPLS = IMPLS;
module.exports.SAMPLES_DIRECTORY = SAMPLES_DIRECTORY;
module.exports.SAMPLES = SAMPLES;
module.exports.select = select;
module.exports.run = run;
run(process.argv.slice(2).map(function (source) {
return new RegExp(source, 'i');
2014-03-14 08:02:01 +00:00
}));