[Groonga-commit] droonga/express-droonga at 0b604fa [master] Support getting cache statistics by /cache/statistics

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Mar 24 22:15:26 JST 2014


Kouhei Sutou	2014-03-24 22:15:26 +0900 (Mon, 24 Mar 2014)

  New Revision: 0b604fac50320f5ee1e95cb6e32ea425c8c5ff5f
  https://github.com/droonga/express-droonga/commit/0b604fac50320f5ee1e95cb6e32ea425c8c5ff5f

  Message:
    Support getting cache statistics by /cache/statistics

  Modified files:
    lib/response-cache/cache.js
    lib/response-cache/index.js
    test/response-cache/cache.test.js
    test/response-cache/middleware.test.js

  Modified: lib/response-cache/cache.js (+23 -1)
===================================================================
--- lib/response-cache/cache.js    2014-03-24 14:03:26 +0900 (682739c)
+++ lib/response-cache/cache.js    2014-03-24 22:15:26 +0900 (16597d6)
@@ -40,10 +40,18 @@ function Cache(options) {
     size: options.size
   });
   this.rules = options.rules;
+  this.nGets = 0;
+  this.nHits = 0;
 }
 Cache.prototype = {
   'get': function(key, callback) {
-    return this.cache.get(key, callback);
+    return this.cache.get(key, function(error, cachedResponse) {
+      this.nGets++;
+      if (cachedResponse) {
+        this.nHits++;
+      }
+      callback(error, cachedResponse);
+    }.bind(this));
   },
 
   'set': function(key, value, ttl, callback) {
@@ -60,6 +68,20 @@ Cache.prototype = {
         return foundRule = rule;
     });
     return foundRule;
+  },
+
+  getStatistics: function() {
+    var hitRatio;
+    if (this.nGets == 0) {
+      hitRatio = 0.0;
+    } else {
+      hitRatio = (this.nHits / this.nGets) * 100;
+    }
+    return {
+      "nGets": this.nGets,
+      "nHits": this.nHits,
+      "hitRatio": hitRatio
+    };
   }
 };
 exports = module.exports = Cache;

  Modified: lib/response-cache/index.js (+6 -0)
===================================================================
--- lib/response-cache/index.js    2014-03-24 14:03:26 +0900 (1260312)
+++ lib/response-cache/index.js    2014-03-24 22:15:26 +0900 (7fed076)
@@ -21,6 +21,12 @@ exports = module.exports = function(options) {
   var cache = new Cache(options);
 
   return function(request, response, next) {
+    if (request.url == '/cache/statistics' && request.method == 'GET') {
+      response.jsonp(200, cache.getStatistics());
+      response.end();
+      return;
+    }
+
     var rule = cache.getRule(request);
     if (!rule) {
       next();

  Modified: test/response-cache/cache.test.js (+47 -0)
===================================================================
--- test/response-cache/cache.test.js    2014-03-24 14:03:26 +0900 (bbdbd21)
+++ test/response-cache/cache.test.js    2014-03-24 22:15:26 +0900 (7880fea)
@@ -85,5 +85,52 @@ suite('Response Cache', function() {
       assert.deepEqual(rule.regex, primaryRegex);
     });
   });
+
+  suite('statistics', function() {
+    var cache;
+    setup(function() {
+      cache = new Cache({
+        rules: [
+          { regex: /.*/ }
+        ]
+      });
+    });
+
+    test('nGets', function() {
+      assert.equal(cache.getStatistics().nGets, 0);
+      cache.get('key', function(error, cachedResponse) {
+      });
+      assert.equal(cache.getStatistics().nGets, 1);
+    });
+
+    test('nHits', function() {
+      cache.set('key', 'value');
+      assert.equal(cache.getStatistics().nHits, 0);
+      cache.get('key', function(error, cachedResponse) {
+      });
+      assert.equal(cache.getStatistics().nHits, 1);
+    });
+
+    suite('hitRatio', function() {
+      test('0 gets', function() {
+        assert.equal(cache.getStatistics().hitRatio, 0.0);
+      });
+
+      test('0 hits', function() {
+        cache.get('key', function(error, cachedResponse) {
+        });
+        assert.equal(cache.getStatistics().hitRatio, 0.0);
+      });
+
+      test('1/2 hits', function() {
+        cache.get('key', function(error, cachedResponse) {
+        });
+        cache.set('key', 'value');
+        cache.get('key', function(error, cachedResponse) {
+        });
+        assert.equal(cache.getStatistics().hitRatio, 50.0);
+      });
+    });
+  });
 });
 

  Modified: test/response-cache/middleware.test.js (+24 -0)
===================================================================
--- test/response-cache/middleware.test.js    2014-03-24 14:03:26 +0900 (883cffb)
+++ test/response-cache/middleware.test.js    2014-03-24 22:15:26 +0900 (0f9e11d)
@@ -336,5 +336,29 @@ suite('Response Cache Middleware', function() {
         });
     });
   });
+
+  suite('statistics', function() {
+    test('json', function(done) {
+      client(application)
+      .get('/cache/statistics')
+      .expect(200)
+      .end(function(error, response) {
+        if (error)
+          return done(error);
+
+        var statistics = {
+          nGets: 0,
+          nHits: 0,
+          hitRatio: 0.0
+        };
+        try {
+          assert.deepEqual(response.body, statistics);
+        } catch (error) {
+          return done(error);
+        }
+        done();
+      });
+    });
+  });
 });
 
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index