-
Notifications
You must be signed in to change notification settings - Fork 2
/
bench.js
183 lines (149 loc) · 4.08 KB
/
bench.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var pull = require('pull-stream')
var cont = require('cont')
const os = require('os')
const path = require('path')
module.exports = function (create, _N, finalCb) {
console.log({ finalCb })
if (typeof finalCb !== 'function') {
console.log('setting')
finalCb = () => {}
}
console.log("name, ops, opts/second, seconds")
function Timer (name) {
var start = Date.now()
return function (ops) {
var seconds = (Date.now() - start)/1000
console.log([name, ops, ops/seconds, seconds].join(', '))
}
}
function initialize (db, N, _, cb) {
var data = []
for(var i = 0; i < N; i++)
data.push({key: '#'+i, value: {
foo: Math.random(), bar: Date.now()
}})
db.append(data, function (err, offset) {
if(err) throw err
//wait until the view is consistent!
db.index.since(function (v) {
if(v < offset) return
cb(null, N)
})
})
}
function ordered_para (db, N, _, cb) {
//ordered reads
var n = 0
for(var i = 0; i < N; i++) {
db.index.get('#'+i, next)
}
function next (err, v) {
if(++n == N) cb(null, N)
}
}
function ordered_series (db, N, _, cb) {
//ordered reads
var n = 0, i = 0
;(function _next () {
var key = '#'+(i++)
db.index.get(i, function (err, v) {
if(v.key != key) return cb('benchmark failed: incorrect key returned')
if(i == n) cb(null, N)
else setImmediate(_next)
})
})(0)
}
function random_series (db, N, _, cb) {
;(function get(i) {
if(i >= N) return cb(null, N)
db.index.get('#'+~~(Math.random()*N), function (err, data) {
setImmediate(function () { get(i+1) })
})
})(0)
}
function random_para (db, N, _, cb) {
var n = 0
for(var i = 0; i < N; i++)
db.index.get('#'+~~(Math.random()*N), next)
function next (err, v) {
if(err && n >= 0) {
n = -1; cb(err)
}
else if(++n == N)
cb(null, N)
}
}
function random_ranges (db, N, makeOpts, cb) {
if(!db.index.read) return cb(new Error('not supported'))
;(function get(i) {
if(i >= N) return cb(null, N)
pull(
db.index.read(
makeOpts('#'+~~(Math.random()*N))
),
pull.collect(function (err, ary) {
if(err) return cb(err)
setImmediate(function () { get(i + ary.length) })
})
)
})(0)
}
function limit10 (key) {
return {gt: key, limit: 10, keys: false}
}
function limit10reverse (key) {
return {gt: key, limit: 10, keys: false, reverse: true}
}
var seed = Date.now()
var file = path.join(os.tmpdir(), `test-flumeview-index-${seed}`)
var db = create(file, seed)
var N = _N || 50000
function refresh () {
return function (cb) {
db.close(function () {
db = create(file, seed)
var start = Date.now()
var rm = db.index.since(function (v) {
if(v === db.since.value) {
console.error('reload', Date.now()-start)
rm()
cb()
}
})
})
}
}
function run(name, benchmark, opts) {
return function (cb) {
var t = Timer(name)
benchmark(db, N, opts, function (err, n) {
t(err | n)
cb()
})
}
}
cont.series([
run('append', initialize),
run('ordered_para', ordered_para),
run('random_para', random_para),
run('ordered_series', ordered_para),
run('random_series', random_para),
refresh(),
run('ordered_para (cool)', ordered_para),
run('ordered_para (warm)', ordered_para),
refresh(),
run('random_para (cool)', random_para),
run('random_para (warm)', random_para),
refresh(),
run('ordered_series (cool)', ordered_para),
run('ordered_series (warm)', ordered_para),
refresh(),
run('random_series (cool)', random_para),
run('random_series (warm)', random_para),
refresh(),
run('random-ranges', random_ranges, limit10),
run('random-ranges (reverse)', random_ranges, limit10reverse),
].filter(Boolean)) (function () {
db.close(finalCb)
})
}