-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (135 loc) · 4.15 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
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
var async = require('async')
var db = require('level-userdb')
var multilevel = require('multilevel')
var net = require('net')
var ui = require('optimist')
.usage('Usage: $0 -d [database] -r [server:port]')
.alias('d', 'database')
.describe('d', 'Path to database location')
.alias('r', 'remote')
var argv = ui.argv
var CONCURRENCY = 10
// 100000 accounts
var RECORDS = 100000
var baseEmail = '[email protected]'
var password = 'supersecret'
// Counters
var inserts = 0
var changePasswords = 0
var modifies = 0
var reads = 0
var ops = 0
var errs = 0
var dbi
if (argv.remote) {
var host = argv.remote.split(':')[0]
var port = 9998
try {
port = parseInt(argv.remote.split(':')[1])
} catch(e) {
}
console.log("connecting to %s:%d", host, port)
var dbi = multilevel.client()
dbi.pipe(net.connect({port:port, host:host})).pipe(dbi)
dbi = db(dbi)
console.log("connected to %s:%d", host, port)
} else {
dbi = db(argv.database)
}
function randValidEmail() {
var r = Math.floor(Math.random()*(RECORDS-0+1)+0);
return r + '-' + baseEmail
}
var funcs = [
// Find a random user. (no lock)
function(done) {
var email = randValidEmail()
dbi.findUser(email, function(err) { if (err) errs++; reads++; ops++; done() })
},
// Modify a user's data. (goes through lock)
function(done) {
var email = randValidEmail()
dbi.modifyUser(email, {some:"data", foobar:ops}, function(err) { if (typeof err !== 'object') errs++; modifies++; ops++; done()})
},
// Change password of existing user (goes through lock)
function(done) {
var email = randValidEmail()
dbi.changePassword(email, "password"+ops, function(err) { if (typeof err !== 'object') errs++; changePasswords++; ops++; done()}, true)
},
// Insert a new user (no lock)
function(done) {
var email = RECORDS + inserts + '-' + baseEmail
dbi.addUser(email, "password"+ops, {data:ops}, function(err) { if (err) errs++; inserts++; ops++; done()}, true)
},
]
// Populate the database with X accounts
function populate(x, done) {
var i = 0
async.whilst(
function() { return i < x },
function(cb) {
dbi.addUser(i + '-' + baseEmail,
password, {some:"data", field:i}, cb, true)
i++
if (i % 1000 === 0) {
console.log("%s records written", i)
}
},
done)
}
console.log("populating db with %d accounts", RECORDS)
var t1 = process.hrtime()
populate(RECORDS, function(err) {
var diff = process.hrtime(t1)
console.log("populated with %d records in %d nanoseconds", RECORDS, diff[0] * 1e9 + diff[1])
console.log("[%d addUser()s / second]", RECORDS / diff[0])
startBench()
})
function startBench() {
console.log("Starting benchmark with concurrency level %d", CONCURRENCY)
var q = async.queue(function(task, cb) {
task(function() {
cb()
})
}, CONCURRENCY)
var OPS = 1000000
// 10 seconds
var SAMPLE_RATE = 10
var nops = ops
var nchangePasswords = changePasswords
var ninserts = inserts
var nmodifies = modifies
var nreads = reads
var nerrs = errs
var iv = setInterval(function() {
var pops = ops - nops
var pchangePasswords = changePasswords - nchangePasswords
var pinserts = inserts - ninserts
var pmodifies = modifies - nmodifies
var preads = reads - nreads
var perrs = errs - nerrs
console.log("reads: %d/s changePasswords: %d/s inserts: %d/s modifies: %d/s total ops: %d/s errs: %d/s",
preads / SAMPLE_RATE, pchangePasswords / SAMPLE_RATE, pinserts / SAMPLE_RATE, pmodifies / SAMPLE_RATE, pops / SAMPLE_RATE, perrs / SAMPLE_RATE)
nops = ops
nchangePasswords = changePasswords
ninserts = inserts
nmodifies = modifies
nreads = reads
nerrs = errs
t = new Date().getTime()
}, SAMPLE_RATE * 1000)
async.whilst(
function() { return ops < OPS },
function(cb) {
q.push(function(done) {
var idx = Math.floor(Math.random() * 4) + 0
funcs[idx](done)
cb(null)
})
},
function() {
console.log("benchmark completed")
console.log("total reads %d changePasswords: %d inserts: %d modifies: %d ops: %d errs: %d", reads, changePasswords, inserts, modifies, ops, errs)
process.exit(0)
})
}