-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
gridfs_benchmark.js
105 lines (91 loc) · 4.15 KB
/
gridfs_benchmark.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
var Db = require('../../lib/mongodb').Db,
Server = require('../../lib/mongodb').Server,
ObjectID = require('../../lib/mongodb').ObjectID,
GridStore = require('../../lib/mongodb').GridStore;
var Mongolian = require('mongolian');
var COUNT = 1000;
var currentWritingIndex = 0;
var server = new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize:1, native_parser:true});
var server2 = new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize:1, native_parser:true});
// Read in the test file
var fileData = require('fs').readFileSync("../../test/gridstore/iya_logo_final_bw.jpg");
// ------------------------------------------------------------------------------
// TEST MONGODB NATIVE
// ------------------------------------------------------------------------------
// Open a db for the file
new Db('gridfs_benchmark', server, {}).open(function(err, new_client) {
new_client.dropDatabase(function(err, result) {
new_client.close();
new Db('gridfs_benchmark', server2, {}).open(function(err, client) {
// Start Time
var startTime = new Date().getTime();
// Iterate COUNT times writing file to gridfs
for(var i = 0; i < COUNT; i++) {
var gridStore = new GridStore(client, "foobar" + i, "w");
gridStore.open(function(err, gridStore) {
gridStore.write(fileData, true, function(err, gridStore) {
// Update current write index
currentWritingIndex = currentWritingIndex + 1;
// finish up
if(currentWritingIndex >= COUNT) {
// Start Time
var endTime = new Date().getTime();
var totalTime = (endTime - startTime);
var msPerOperation = totalTime/COUNT;
var operationsPrSecond = 1000/msPerOperation;
var bytesPrSecond = Math.floor(fileData.length * operationsPrSecond);
var mbsPrSecond = (bytesPrSecond/1024)/1024 ;
console.log("-------------------------------------------------- DONE NATIVE")
console.log("total time ms :: " + totalTime);
console.log("ms pr operation :: " + msPerOperation);
console.log("operations pr second :: " + operationsPrSecond);
console.log("bytes pr second :: " + bytesPrSecond);
console.log("MB pr second :: " + mbsPrSecond);
// Close db
client.close();
// Execute mongolian test
executeMongolianTest();
}
})
});
}
});
})
});
// ------------------------------------------------------------------------------
// TEST MONGODB NATIVE
// ------------------------------------------------------------------------------
var executeMongolianTest = function() {
var db = new Mongolian('mongo://localhost/mongolian_test', { log:false })
var gridfs = db.gridfs('testfs')
// Number of executed operations
var currentWritingIndexM = 0;
// Start Time
var startTime = new Date().getTime();
// Execute Mongolian Count times writing data
for(var i = 0; i < COUNT; i++) {
var stream = gridfs.create('foo' + i).writeStream();
stream.on('close', function() {
currentWritingIndexM = currentWritingIndexM + 1;
if(currentWritingIndexM >= COUNT) {
// Start Time
var endTime = new Date().getTime();
var totalTime = (endTime - startTime);
var msPerOperation = totalTime/COUNT;
var operationsPrSecond = 1000/msPerOperation;
var bytesPrSecond = Math.floor(fileData.length * operationsPrSecond);
var mbsPrSecond = (bytesPrSecond/1024)/1024 ;
console.log("-------------------------------------------------- DONE MONGOLIAN")
console.log("total time ms :: " + totalTime);
console.log("ms pr operation :: " + msPerOperation);
console.log("operations pr second :: " + operationsPrSecond);
console.log("bytes pr second :: " + bytesPrSecond);
console.log("MB pr second :: " + mbsPrSecond);
// Close connection
db.server.close()
}
});
// Write file
stream.end(fileData);
}
}