|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const keyspace = require('keyspace') |
| 4 | +const ldu = require('../lib/level-du') |
| 5 | +const window = 1000 |
| 6 | +const progressWindow = window * 100 |
| 7 | + |
| 8 | +exports.defaults = { |
| 9 | + benchmark: { |
| 10 | + n: 1e6, |
| 11 | + concurrency: 1, |
| 12 | + valueSize: 100, |
| 13 | + buffers: false, |
| 14 | + keys: 'random', |
| 15 | + values: 'random', |
| 16 | + seed: 'seed' |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +exports.plot = require('./iterate.plot') |
| 21 | + |
| 22 | +exports.run = function (factory, stream, options) { |
| 23 | + if (options.n < window) { |
| 24 | + throw new RangeError('The "n" option must be >= ' + window) |
| 25 | + } else if (options.n % window !== 0) { |
| 26 | + throw new Error('The "n" option must be a multiple of ' + window) |
| 27 | + } |
| 28 | + |
| 29 | + const generator = keyspace(options.n, options) |
| 30 | + |
| 31 | + stream.write('Elapsed (ms), Entries, Bytes, ns/read, CMA MB/s\n') |
| 32 | + |
| 33 | + function start (db) { |
| 34 | + const startTime = Date.now() |
| 35 | + |
| 36 | + let inProgress = 0 |
| 37 | + let totalReads = 0 |
| 38 | + let totalBytes = 0 |
| 39 | + let timesAccum = 0 |
| 40 | + let elapsed |
| 41 | + |
| 42 | + function report () { |
| 43 | + console.log( |
| 44 | + 'Iterated', options.n, 'entries in', |
| 45 | + Math.floor((Date.now() - startTime) / 1e3) + 's,', |
| 46 | + (Math.floor((totalBytes / 1048576) * 100) / 100) + 'MB' |
| 47 | + ) |
| 48 | + |
| 49 | + stream.end() |
| 50 | + |
| 51 | + db.close(function (err) { |
| 52 | + if (err) throw err |
| 53 | + |
| 54 | + ldu(db, function (err, size) { |
| 55 | + if (err) throw err |
| 56 | + if (size) console.log('Database size:', Math.floor(size / 1024 / 1024) + 'M') |
| 57 | + }) |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + function iterate () { |
| 62 | + if (totalReads >= options.n) return report(Date.now() - startTime) |
| 63 | + if (inProgress >= options.concurrency) return |
| 64 | + |
| 65 | + inProgress++ |
| 66 | + |
| 67 | + const it = db.iterator({ |
| 68 | + keyAsBuffer: options.buffers, |
| 69 | + valueAsBuffer: options.buffers |
| 70 | + }) |
| 71 | + |
| 72 | + function loop() { |
| 73 | + if (totalReads >= options.n) return end() |
| 74 | + const start = process.hrtime() |
| 75 | + |
| 76 | + it.next(function (err, key, value) { |
| 77 | + if (err) throw err |
| 78 | + if (key === undefined && value === undefined) return end() |
| 79 | + |
| 80 | + const duration = process.hrtime(start) |
| 81 | + const nano = (duration[0] * 1e9) + duration[1] |
| 82 | + |
| 83 | + timesAccum += nano |
| 84 | + totalBytes += Buffer.byteLength(key) + Buffer.byteLength(value) |
| 85 | + totalReads++ |
| 86 | + |
| 87 | + if (totalReads % progressWindow === 0) { |
| 88 | + console.log('' + inProgress, totalReads, |
| 89 | + Math.round(totalReads / options.n * 100) + '%') |
| 90 | + } |
| 91 | + |
| 92 | + if (totalReads % window === 0) { |
| 93 | + elapsed = Date.now() - startTime |
| 94 | + stream.write( |
| 95 | + elapsed + |
| 96 | + ',' + totalReads + |
| 97 | + ',' + totalBytes + |
| 98 | + ',' + (timesAccum / window).toFixed(3) + |
| 99 | + ',' + ((totalBytes / 1048576) / (elapsed / 1e3)).toFixed(3) + |
| 100 | + '\n') |
| 101 | + timesAccum = 0 |
| 102 | + } |
| 103 | + |
| 104 | + loop() |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + function end () { |
| 109 | + it.end(function (err) { |
| 110 | + if (err) throw err |
| 111 | + inProgress-- |
| 112 | + process.nextTick(iterate) |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + loop() |
| 117 | + } |
| 118 | + |
| 119 | + for (let i = 0; i < options.concurrency; i++) iterate() |
| 120 | + } |
| 121 | + |
| 122 | + factory(function (err, db) { |
| 123 | + if (err) throw err |
| 124 | + |
| 125 | + let entries = 0 |
| 126 | + |
| 127 | + function loop (err) { |
| 128 | + if (err) throw err |
| 129 | + |
| 130 | + console.log('Prep: wrote %d of %d entries', entries, options.n) |
| 131 | + if (entries >= options.n) return setTimeout(() => start(db), 500) |
| 132 | + |
| 133 | + const batch = db.batch() |
| 134 | + |
| 135 | + for (let i = 0; i < 1e3 && entries < options.n; i++) { |
| 136 | + const key = generator.key(entries++) |
| 137 | + const value = generator.value() |
| 138 | + |
| 139 | + batch.put(key, value) |
| 140 | + } |
| 141 | + |
| 142 | + batch.write(loop) |
| 143 | + } |
| 144 | + |
| 145 | + loop() |
| 146 | + }) |
| 147 | +} |
0 commit comments