wordpos/bench/wordpos-bench.js

131 lines
2.5 KiB
JavaScript
Raw Normal View History

/**
* wordpos-bench.js
*
* Copyright (c) 2012-2016 mooster@42at.com
* https://github.com/moos/wordpos
*
* Released under MIT license
*/
var Bench = require('mini-bench'),
2012-05-04 19:23:28 +00:00
fs = require('fs'),
_ = require('underscore')._,
WordPOS = require('../src/wordpos'),
wordpos = new WordPOS();
2012-05-02 23:18:10 +00:00
2016-01-18 08:09:56 +00:00
suite = new Bench.Suite({
2012-05-02 23:18:10 +00:00
type: 'fixed',
2016-01-18 08:09:56 +00:00
iterations: 1,
async: false, // important!
2012-05-04 19:23:28 +00:00
2012-05-02 23:18:10 +00:00
start: function(tests){
console.log('starting %d tests', tests.length);
},
result: function(name, stats){
var persec = 1000 / stats.elapsed
, ops = .5 + stats.iterations * persec;
2012-05-04 19:23:28 +00:00
2012-05-02 23:18:10 +00:00
console.log(' \033[90m%s : \033[36m%d \033[90mops/s\033[0m', name, ops | 0, stats);
2012-05-04 19:23:28 +00:00
pos && console.log(out(pos));
2012-05-02 23:18:10 +00:00
},
2012-05-04 19:23:28 +00:00
2012-05-02 23:18:10 +00:00
done: function(time){
2016-01-18 08:09:56 +00:00
console.log('looked up %d words, %d found', nwords, found);
2012-05-02 23:18:10 +00:00
console.log('done in %d msecs', time );
},
2012-05-04 19:23:28 +00:00
2012-05-02 23:18:10 +00:00
section: function(name, stats) {
2012-05-04 19:23:28 +00:00
console.log('\033[35m%s\033[0m',name);
2012-05-02 23:18:10 +00:00
}
});
function out(res){
2016-01-18 08:09:56 +00:00
return _(res).keys().map(function(k){
return k + ':' + res[k].length
});
2012-05-02 23:18:10 +00:00
}
2016-01-18 08:09:56 +00:00
var
text = fs.readFileSync('text-512.txt', 'utf8'),
parsedText = wordpos.parse(text),
nwords = parsedText.length,
2012-05-04 19:23:28 +00:00
pos;
2012-05-02 23:18:10 +00:00
function getPOS(next){
2016-01-18 08:09:56 +00:00
wordpos.getPOS(text, function(res){
2012-05-04 19:23:28 +00:00
pos = res;
next();
});
2012-05-02 23:18:10 +00:00
}
function getNouns(next){
2012-05-04 19:23:28 +00:00
wordpos.getNouns(text, function(res){
pos = {nouns: res};
next();
});
2012-05-02 23:18:10 +00:00
}
function getVerbs(next){
2012-05-04 19:23:28 +00:00
wordpos.getVerbs(text, function(res){
pos = {verbs: res};
next();
});
2012-05-02 23:18:10 +00:00
}
function getAdjectives(next){
2012-05-04 19:23:28 +00:00
wordpos.getAdjectives(text, function(res){
pos = {adjectives: res};
next();
});
2012-05-02 23:18:10 +00:00
}
function getAdverbs(next){
2012-05-04 19:23:28 +00:00
wordpos.getAdverbs(text, function(res){
pos = {adverbs: res};
next();
});
2012-05-02 23:18:10 +00:00
}
2016-01-18 08:09:56 +00:00
function lookup(next){
var count = nwords;
found = 0;
parsedText.forEach(function(word) {
wordpos.lookup(word, function (res) {
res.length && ++found;
if (--count === 0) next();
});
});
}
function lookupNoun(next){
var count = nwords;
found = 0;
parsedText.forEach(function(word) {
wordpos.lookupNoun(word, function (res) {
res.length && ++found;
if (--count === 0) next();
});
});
}
2012-05-02 23:18:10 +00:00
suite.section('--512 words--', function(next){
2012-05-04 19:23:28 +00:00
suite.options.iterations = 1;
next();
2012-05-02 23:18:10 +00:00
});
2012-05-02 23:18:10 +00:00
suite.bench('getPOS', getPOS);
suite.bench('getNouns', getNouns);
suite.bench('getVerbs', getVerbs);
suite.bench('getAdjectives', getAdjectives);
suite.bench('getAdverbs', getAdverbs);
2016-01-18 08:09:56 +00:00
suite.bench('lookup', lookup);
suite.bench('lookupNoun', lookupNoun);
2012-05-02 23:18:10 +00:00
suite.run();