Skip to content

Instantly share code, notes, and snippets.

@dminkovsky
Created August 25, 2014 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminkovsky/e49134e5e5ad2ee97e05 to your computer and use it in GitHub Desktop.
Save dminkovsky/e49134e5e5ad2ee97e05 to your computer and use it in GitHub Desktop.
var Promise = require('bluebird');
var r = require('rethinkdb');
var conn;
// Manage a 1-connection "pool";
function getConnection() {
if (conn) { return Promise.resolve(conn); }
return r.connect({ db: 'lm' })
.then(function(c) { return conn = c; })
.catch(function(err) { console.log(err.stack || err); })
}
// A timer
function Timer() {}
Timer.prototype.start = function() {
this.start = new Date();
};
Timer.prototype.log = function(name) {
console.log(name, (new Date()).getTime() - this.start.getTime());
};
Timer.timed = function(fn) {
var timer = new this;
return fn(timer);
}
var ids = ["38c2340fd0312913", "5a42f20dda9582c7", "a3699e5ead5918d5", "0cfa623543a66dc0", "772cc45fa0bf8c12",
"38c2340fd0312913", "5a42f20dda9582c7", "a3699e5ead5918d5", "0cfa623543a66dc0", "772cc45fa0bf8c12",
"e4f30d4c2282c563", "38c2340fd0312913", "5a42f20dda9582c7", "a3699e5ead5918d5", "0cfa623543a66dc0",
"5a42f20dda9582c7", "a3699e5ead5918d5", "0cfa623543a66dc0", "772cc45fa0bf8c12", "e4f30d4c2282c563",
"38c2340fd0312913", "5a42f20dda9582c7", "a3699e5ead5918d5", "0cfa623543a66dc0", "772cc45fa0bf8c12"]
function testMap(timer) {
console.log('\n\nSTARTING `testMap`');
return getConnection().then(function(conn) {
var query = r.expr(ids)
.concatMap(function(id) {
return r.table("contents")
.getAll(id, { index: 'revision_id' })
});
timer.start()
return query.run(conn);
})
.then(function(results) {
return timer.log('`testMap` time: ');
})
.catch(function(err) {
console.log(err.stack || err);
});
}
function testGetAll(timer) {
console.log('\n\nSTARTING `testGetAll`');
return getConnection().then(function(conn) {
var query = r.db('lm').table('contents');
query = query.getAll.apply(query, ids.concat({ index: 'revision_id' }));
timer.start()
return query.run(conn);
})
.then(function(cursor) {
timer.log('`testGetAll`, received cursor time:');
return cursor.toArray();
})
.then(function(results) {
return timer.log('`testGetAll`, exhausted cursor time:');
})
.catch(function(err) {
console.log(err.stack || err);
});
}
function testGetAllCoerceToArray(timer) {
console.log('\n\nSTARTING `testGetAllCoerceToArray`');
return getConnection().then(function(conn) {
var query = r.db('lm').table('contents');
query = query.getAll.apply(query, ids.concat({ index: 'revision_id' })).coerceTo('array');
timer.start()
return query.run(conn);
})
.then(function(results) {
return timer.log('`testGetAllCoerceToArray` time: ');
})
.catch(function(err) {
console.log(err.stack || err);
});
}
Timer.timed(testMap)
.then(function() {
return Timer.timed(testGetAll)
})
.then(function() {
return Timer.timed(testGetAllCoerceToArray)
})
.then(function() {
process.exit();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment