-
Notifications
You must be signed in to change notification settings - Fork 1
/
couchbase.js
84 lines (66 loc) · 2.46 KB
/
couchbase.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
var config = require('./config');
var couchbase = require('couchbase');
var db = new couchbase.Connection({host: config.couchbase.host + ":" + config.couchbase.port, bucket: config.couchbase.bucket, queryhosts: config.couchbase.queryHosts});
// Set up timer
var start = process.hrtime();
var elapsed_time = function(note){
var precision = 3; // 3 decimal places
var elapsed = process.hrtime(start)[1] / 1000000; // divide by a million to get nano to milli
console.log(process.hrtime(start)[0] + " s, " + elapsed.toFixed(precision) + " ms - " + note); // print message + time
start = process.hrtime(); // reset the timer
}
function purgeRecords() {
if(config.purgeBucket) {
var query = db.query("SELECT * FROM " + config.couchbase.bucket, function(err, results) {
if(err)
throw err;
if(config.purgeBucket) {
console.log("Deleting " + results.length + " results");
for(key in results) {
db.remove(key, function(err, results) {
if(err)
throw err;
});
}
}
queryView();
});
}
else
queryView();
}
function queryView() {
console.log("Querying using a view: " + config.couchbase.docName + " | " + config.couchbase.viewName);
start = process.hrtime();
var query = db.view(config.couchbase.docName, config.couchbase.viewName);
query.query(function(err, results) {
console.log("Done querying... " + results.length + " results");
elapsed_time("Querying with view");
if(err)
throw err;
queryN1QL();
});
}
function queryN1QL() {
console.log("======================================");
console.log("Querying with N1QL: " + config.n1qlquery);
start = process.hrtime();
var query = db.query(config.n1qlquery, function(err, results) {
console.log("Done querying... " + results.length + " results");
elapsed_time("Querying with N1QL");
if(err)
throw err;
completeQuery();
});
}
function completeQuery() {
console.log("Completed benchmarking.\n");
process.exit(code=0);
}
function startProc() {
purgeRecords();
}
////////////////////////////////////
// MAKE ALL EDITS BELOW THIS LINE //
////////////////////////////////////
startProc();