Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional statistics for Redis queries. #208

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var redis = require('../config/database')
, database = redis.connect()

exports.database = database

exports.AbstractSerializer = require('./serializers/abstract_serializer').addSerializer()
exports.Serializer = require("./serializers/serializer").addSerializer()

Expand Down
10 changes: 10 additions & 0 deletions app/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ var findUser = function(req, res, next) {
module.exports = function(app) {
app.use(require('express').static(__dirname + '/../public'))

app.use(function(req, res, next) {
if (config.redis.analyze_performance && req.method != "OPTIONS") {
models.database.reset_statistics()
req.on("end", function() {
models.database.report_statistics(req.originalUrl, app.logger)
})
}
next()
})

app.options('/*', function(req, res) { res.status(200).send({}) })

SessionRoute.addRoutes(app)
Expand Down
56 changes: 54 additions & 2 deletions config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,63 @@
var Promise = require('bluebird')
, redis = require('redis')
, config = require('./config').load()
, _ = require('lodash')

Promise.promisifyAll(redis.RedisClient.prototype)
Promise.promisifyAll(redis.Multi.prototype)

var database = redis.createClient(config.redis.port, config.redis.host, {})
function isUUID(arg) {
return arg.length == 36 && arg[8] == '-' && arg[13] == '-' && arg[18] == '-' && arg[23] == '-'
}

function getPerformanceStatisticsKey(command, args) {
if (_.isString(args[0])) {
var commandArgs = args[0].split(":").map(function(arg) {
if (isUUID(arg)) {
return '*'
}
return arg
})
return command + " " + commandArgs.join(":")
}
return command
}

function initDb() {
var db = redis.createClient(config.redis.port, config.redis.host, {})

if (config.redis.analyze_performance) {
var old_send_command = db.send_command
db.send_command = function(command, args, callback) {
var stats = db.redis_statistics
if (stats) {
var key = getPerformanceStatisticsKey(command, args)
var oldCount = stats[key] || 0
stats[key] = oldCount + 1
}
old_send_command.apply(db, arguments)
}

db.reset_statistics = function() {
this.redis_statistics = {}
}

db.report_statistics = function(url, logger) {
if (this.redis_statistics && _.keys(this.redis_statistics).length > 0) {
logger.info("Statistics for " + url + ":")
var total = 0
_.forEach(this.redis_statistics, function(v, k) {
logger.info(" " + k + ": " + v)
total += v
})
logger.info(" -- TOTAL: " + total)
}
}
}
return db
}

var database = initDb()

exports.selectDatabase = function() {
return new Promise(function(resolve, reject) {
Expand All @@ -17,7 +69,7 @@ exports.selectDatabase = function() {
}

exports.connect = function() {
if (!database) database = redis.createClient(config.redis.port, config.redis.host, {})
if (!database) database = initDb()
return database
}

Expand Down
3 changes: 2 additions & 1 deletion config/environments/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ exports.getConfig = function() {

config.redis = {
host: 'localhost',
port: 6379
port: 6379,
analyze_performance: true
}

return config
Expand Down