/
index.js
97 lines (84 loc) · 2.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Copyright (c) 2012-2013 LevelUP contributors
* See list at <https://github.com/rvagg/node-levelup#contributing>
* MIT +no-false-attribs License <https://github.com/rvagg/node-levelup/blob/master/LICENSE>
*/
const DB_ROOT = __dirname
var path = require('path')
, Benchmark = require('benchmark')
, rimraf = require('rimraf')
, async = require('async')
, engines = require('./engines/')
, tests = require('./tests/')
, dbidx = 0
, printableEngineName = function (engineName) {
var len = Object.keys(engines).reduce(function (m, c) { return Math.max(c.length, m) }, 0)
while (engineName.length < len) engineName += ' '
return engineName
}
, mklocation = function () {
return path.join(DB_ROOT, '_benchdb_' + dbidx++)
}
, mkdb = function (engine, location, callback) {
rimraf(location, engine.createDb.bind(null, location, callback))
}
, rmdb = function (engine, db, location, callback) {
engine.closeDb(db, rimraf.bind(null, location, callback))
}
, run = function (db, name, fn, color, cb) {
var exec = function () {
new Benchmark(name, {
'defer': true,
'fn': function (deferred) {
fn(db, deferred.resolve.bind(deferred))
}
})
.on('complete', function(event) {
console.log(String(event.target)[color].bold)
cb()
})
.run({ async: true })
}
if (fn.setup) {
fn.setup(db, function (err) {
if (err) return cb(err)
exec()
})
} else
exec()
}
, runTest = function (testName, callback) {
async.forEachSeries(
Object.keys(engines)
, function (engineKey, callback) {
var engine = engines[engineKey]
var location = mklocation()
mkdb(engine, location, function (err, db) {
if (err) return callback(err)
if (!tests[testName][engineKey]) { console.log('Skipping for', testName, engineKey); return callback() }
run(
db
, printableEngineName(engineKey) + ' ' + testName
, tests[testName][engineKey]
, engine.color
, function (err) {
rmdb(engine, db, location, function (_err) {
callback(err || _err)
})
}
)
})
}
, function () {
console.log()
callback.apply(null, arguments)
}
)
}
, focusKey = Object.keys(tests).filter(function (k) { return (/\=>/).test(k) })
if (focusKey.length) {
var focusTest = tests[focusKey[0]]
tests = {}
tests[focusKey[0]] = focusTest
}
require('colors')
async.forEachSeries(Object.keys(tests), runTest)