Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
added support for 'stats' operations
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcal committed Aug 28, 2010
1 parent 973c883 commit 53e5a7c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
50 changes: 48 additions & 2 deletions lib/memcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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];
Expand Down
41 changes: 39 additions & 2 deletions test/test-memcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 53e5a7c

Please sign in to comment.