Skip to content

Commit 4265fdf

Browse files
author
sam2x
committed
adding benchmarks
1 parent 6be8a0f commit 4265fdf

File tree

3 files changed

+280
-0
lines changed

3 files changed

+280
-0
lines changed

benchmark/bm.js

+250
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
/*
2+
Installation : npm install --save-dev fastbench async
3+
*/
4+
5+
nohm = require('../lib/nohm').Nohm;
6+
7+
var bench = require('fastbench'),
8+
models = require('./bm_models'),
9+
rclient = require('redis').createClient(),
10+
async = require('async'),
11+
childProcess = require('child_process'),
12+
os = require('os');
13+
14+
// Params
15+
16+
var u_ids = null;
17+
var u_instances = null;
18+
var item = null;
19+
var has_zlink = false;
20+
var opt = {score: +new Date()};
21+
var ncreate = 2000;
22+
23+
nohm.setPrefix('benchmark');
24+
rclient.select(15);
25+
26+
rclient.on("ready", function (err) {
27+
nohm.setClient(rclient);
28+
29+
var pathToRedis = require.resolve('redis');
30+
pathToRedis = pathToRedis.substring(0, pathToRedis.lastIndexOf("/"));
31+
32+
/* copied from https://github.com/luin/ioredis/blob/master/benchmarks/single_node.js */
33+
console.log('==========================');
34+
console.log('CPU: ' + os.cpus().length);
35+
console.log('Nohm version: ' + require('../package.json').version);
36+
console.log('Redis client version: ' + require(pathToRedis + '/package.json').version);
37+
console.log('Redis server version: ' + rclient.server_info.redis_version);
38+
console.log('OS: ' + os.platform() + ' ' + os.arch());
39+
console.log('node version: ' + process.version);
40+
console.log('current commit: ' + childProcess.execSync('git rev-parse --short HEAD').slice(0, -1));
41+
console.log('==========================');
42+
43+
run();
44+
})
45+
46+
// Models
47+
48+
var UserMockup = models.user;
49+
var ItemMockup = models.item;
50+
51+
// Tests
52+
53+
function run()
54+
{
55+
var prepare_create = function(done){
56+
console.log('[+] Creating ', ncreate, ' nohm objects');
57+
done()
58+
}
59+
var run_create = bench([
60+
function modelCreateManual (done) {
61+
var u = new models.user();
62+
u.p('username', 'test')
63+
u.save(function(){
64+
done()
65+
})
66+
},
67+
function modelCreateFactory (done) {
68+
var u = nohm.factory('UserMockup')
69+
u.p('username', 'test')
70+
u.save(function(){
71+
done()
72+
})
73+
},
74+
], ncreate)
75+
76+
var prepare_read = function(done){
77+
UserMockup.find({}, function(err, ids){
78+
u_ids = ids;
79+
console.log('\n[+] Reading ', u_ids.length, ' uids');
80+
done()
81+
})
82+
}
83+
84+
var run_read = bench([
85+
function modelReadLoad (done) {
86+
async.each(u_ids, function(uid, next){
87+
UserMockup.load(uid, function(){
88+
next()
89+
})
90+
}, function(){
91+
done();
92+
})
93+
},
94+
function modelReadFactory (done) {
95+
async.each(u_ids, function(uid, next){
96+
nohm.factory('UserMockup', uid, function(){
97+
next()
98+
})
99+
}, function(){
100+
done();
101+
})
102+
},
103+
], 1)
104+
105+
var prepare_update = function(done){
106+
UserMockup.findAndLoad({}, function(err, instances){
107+
u_instances = instances;
108+
console.log('\n[+] Updating ', u_instances.length, ' objects');
109+
done()
110+
})
111+
}
112+
113+
var run_update = bench([
114+
function modelUpdate (done) {
115+
async.each(u_instances, function(user, next){
116+
user.p('username', 'aaaaaaaa');
117+
user.save(function(){
118+
next();
119+
})
120+
}, function(){
121+
done();
122+
})
123+
},
124+
], 1)
125+
126+
var prepare_link = function(done){
127+
console.log('\n[+] Linking ', u_instances.length, ' objects to same object')
128+
if (u_instances[0].zlink) has_zlink = true;
129+
item = nohm.factory('ItemMockup')
130+
item.p('name', 'item blabla')
131+
item.save(function(){
132+
done()
133+
})
134+
}
135+
136+
var run_link = bench([
137+
function modelLink (done) {
138+
async.each(u_instances, function(user, next){
139+
user.link(item)
140+
user.save(function(err, link_err){
141+
next();
142+
})
143+
}, function(err){
144+
done();
145+
})
146+
},
147+
function modelZlink (done) {
148+
if (has_zlink)
149+
{
150+
async.each(u_instances, function(user, next){
151+
user.zlink(item, opt);
152+
user.save(function(){
153+
next();
154+
})
155+
}, function(){
156+
done();
157+
})
158+
}
159+
else
160+
{
161+
console.log('zlink feature not detected, passing test')
162+
done();
163+
}
164+
},
165+
], 1)
166+
167+
var prepare_unlink = function(done){
168+
console.log('\n[+] Unlinking ', u_instances.length, ' links');
169+
done()
170+
}
171+
172+
var run_unlink = bench([
173+
function modelUnlink(done) {
174+
async.each(u_instances, function(user, next){
175+
user.unlink(item)
176+
user.save(function(){
177+
next();
178+
})
179+
}, function(){
180+
done();
181+
})
182+
},
183+
function modelZunlink(done) {
184+
if (has_zlink)
185+
{
186+
async.each(u_instances, function(user, next){
187+
user.zunlink(item);
188+
user.save(function(){
189+
next();
190+
})
191+
}, function(){
192+
done();
193+
})
194+
}
195+
else
196+
{
197+
console.log('zunlink feature not detected, passing test')
198+
done();
199+
}
200+
},
201+
], 1)
202+
203+
var prepare_delete = function(done){
204+
console.log('\n[+] Deleting ', u_instances.length, ' objects');
205+
done()
206+
}
207+
var run_delete = bench([
208+
function modelRemove (done) {
209+
async.each(u_instances, function(user, next){
210+
user.remove(function(){
211+
next();
212+
})
213+
}, function(){
214+
done();
215+
})
216+
},
217+
], 1)
218+
219+
var clean_link = function(done){
220+
item.remove(function(){
221+
done()
222+
})
223+
}
224+
225+
// run them two times
226+
async.series([
227+
prepare_create,
228+
run_create,
229+
230+
prepare_read,
231+
run_read,
232+
233+
prepare_update,
234+
run_update,
235+
236+
prepare_link,
237+
run_link,
238+
239+
prepare_unlink,
240+
run_unlink,
241+
242+
prepare_delete,
243+
run_delete,
244+
245+
clean_link,
246+
], function(){
247+
console.log('test ended!')
248+
rclient.quit();
249+
})
250+
}

benchmark/bm_models.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Models
2+
exports.user = nohm.model('UserMockup', {
3+
properties: {
4+
username: {
5+
type: 'string',
6+
defaultValue: 'test',
7+
validations: [
8+
'notEmpty'
9+
]
10+
},
11+
key: {
12+
type: 'integer',
13+
index: true
14+
}
15+
}
16+
});
17+
18+
exports.item = nohm.model('ItemMockup', {
19+
properties: {
20+
name: {
21+
type: 'string',
22+
defaultValue: 'my item',
23+
validations: [
24+
'notEmpty'
25+
]
26+
},
27+
}
28+
});

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
"traverse": "^0.6.6"
2727
},
2828
"devDependencies": {
29+
"async": "^1.5.2",
30+
"fastbench": "^1.0.1",
2931
"nodeunit": "^0.9.1"
3032
},
3133
"contributors": [

0 commit comments

Comments
 (0)