This repository was archived by the owner on Dec 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathwrite-random.js
More file actions
73 lines (58 loc) · 1.86 KB
/
write-random.js
File metadata and controls
73 lines (58 loc) · 1.86 KB
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
const leveldown = require('../')
const crypto = require('crypto')
const fs = require('fs')
const du = require('du')
const uuid = require('uuid')
const path = require('path')
const rimraf = require('rimraf')
const argv = require('optimist').argv
const options = {
db: argv.db || path.join(__dirname, 'db'),
num: argv.num || 10000000,
concurrency: argv.concurrency || 10,
cacheSize: argv.cacheSize || 8,
writeBufferSize: argv.writeBufferSize || 4,
valueSize: argv.valueSize || 100,
out: argv.out || path.join(__dirname, 'write-random.csv')
}
const data = crypto.randomBytes(256) // buffer
rimraf.sync(options.db)
const db = leveldown(options.db)
const timesStream = fs.createWriteStream(options.out, 'utf8')
function report (ms) {
console.log('Wrote', options.num, 'in', Math.floor(ms / 1000) + 's')
timesStream.end()
du(options.db, function (err, size) {
if (err) throw err
console.log('Database size:', Math.floor(size / 1024 / 1024) + 'M')
})
console.log('Wrote times to ', options.out)
}
db.open(function (err) {
if (err) throw err
let inProgress = 0
let totalWrites = 0
const startTime = Date.now()
let writeBuf = ''
function write () {
if (totalWrites % 100000 === 0) console.log(inProgress, totalWrites)
if (totalWrites % 1000 === 0) {
timesStream.write(writeBuf)
writeBuf = ''
}
if (totalWrites++ === options.num) return report(Date.now() - startTime)
if (inProgress >= options.concurrency || totalWrites > options.num) return
var start = process.hrtime()
inProgress++
db.put(uuid.v4(), data, function (err) {
if (err) throw err
var duration = process.hrtime(start)
var nano = (duration[0] * 1e9) + duration[1]
writeBuf += (Date.now() - startTime) + ',' + nano + '\n'
inProgress--
process.nextTick(write)
})
process.nextTick(write)
}
write()
})