From 53e5a7c34a286dd9126d385d19da3032d3ec8336 Mon Sep 17 00:00:00 2001 From: Cal Henderson Date: Fri, 27 Aug 2010 19:47:16 -0500 Subject: [PATCH] added support for 'stats' operations --- README.md | 7 ++++++ lib/memcache.js | 50 +++++++++++++++++++++++++++++++++++++++++-- test/test-memcache.js | 41 +++++++++++++++++++++++++++++++++-- 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7b17f6e..b4c6c56 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,13 @@ After connecting, you can start to make requests. client.increment('key', value, callback); client.decrement('key', value, callback); + // statistics. the success argument to the callback + // is a key=>value object + client.stats(callback); + client.stats('settings', callback); + client.stats('items', callback); + client.stats('mongeese', callback); + Once you're done, close the connection. client.close(); diff --git a/lib/memcache.js b/lib/memcache.js index dc02c1d..65f6737 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -168,13 +168,27 @@ Client.prototype.decrement = function(key, value, callback) { return this.query('decr ' + key + ' ' + value, 'simple', callback); }; +Client.prototype.stats = function(type, callback){ + + if (typeof(type) == 'function'){ + callback = type; + type = null; + } + + if (type){ + return this.query('stats '+type, 'stats', callback); + }else{ + return this.query('stats', 'stats', callback); + } +} + Client.prototype.handle_received_data = function(){ - while (this.buffer.length > 0) { + while (this.buffer.length > 0){ var result = this.determine_reply_handler(this.buffer); - if (result == null) { + if (result == null){ break; } @@ -239,6 +253,38 @@ Client.prototype.handle_get = function(buffer) { } }; +Client.prototype.handle_stats = function(buffer){ + + // special case - no stats at all + if (buffer.indexOf('END') == 0){ + return [{}, 5]; + } + + // find the terminator + var idx = buffer.indexOf('\r\nEND\r\n'); + if (idx == -1){ + // wait for more data if we don't have an end yet + return null; + } + + // read the lines + var our_data = buffer.substr(0, idx+2); + var out = {}; + var line = null; + var i=0; + while (line = readLine(our_data)){ + our_data = our_data.substr(line.length + 2); + if (line.substr(0, 5) == 'STAT '){ + var idx2 = line.indexOf(' ', 5); + var k = line.substr(5, idx2-5); + var v = line.substr(idx2+1); + out[k] = v; + } + } + + return [out, idx + 7, null]; +}; + Client.prototype.handle_simple = function(buffer){ var line = readLine(buffer); return [line, (line.length + crlf_len), null]; diff --git a/test/test-memcache.js b/test/test-memcache.js index 1738731..49d0603 100644 --- a/test/test-memcache.js +++ b/test/test-memcache.js @@ -170,13 +170,50 @@ mc.addHandler(function() { }; exports['version'] = function(assert, beforeExit){ + var n = 0; mc.version(function(success, error){ - + n++; assert.equal(error, null); assert.length(success, 5); }); - } + beforeExit(function(){ + assert.equal(1, n); + }); + }; + + exports['stats'] = function(assert, beforeExit){ + var n = 0; + + mc.stats(function(success, error){ + n++; + assert.ok(success.pid, "server has a pid"); + }); + + mc.stats('settings', function(success, error){ + n++; + assert.ok(success.maxconns); + }); + + mc.stats('items', function(success, error){ n++; assert.ok(num_keys(success)); }); + mc.stats('sizes', function(success, error){ n++; assert.ok(num_keys(success)); }); + mc.stats('slabs', function(success, error){ n++; assert.ok(num_keys(success)); }); + + mc.stats('notreal', function(success, error){ + n++; + assert.equal(error, 'ERROR'); + }); + + beforeExit(function(){ + assert.equal(6, n); + }); + }; }); + +function num_keys(a){ + var i=0; + for (var k in a) i++; + return i; +}