added more cache stats

This commit is contained in:
javi santana 2011-10-13 16:20:29 +02:00
parent 7f3a3a6150
commit b4a0732120
2 changed files with 103 additions and 67 deletions

View File

@ -61,7 +61,7 @@ var CartodbWindshaft = function(serverOptions) {
*/
ws.get('/cache', function(req, res){
if(serverOptions.cacheStats) {
res.send(serverOptions.cacheStats());
res.send(serverOptions.cacheStats(req.query.tile_info, req.query.sort_by));
} else {
res.send("Cache no enabled")
}

View File

@ -6,6 +6,8 @@
var LRUCache = require('./lru'),
CacheValidator = require('./cache_validator')
var BLANK_TILE_SIZE = 334; // totally transparent tile size in bytes
module.exports.NoCache = function() {
var me = {}
@ -53,6 +55,7 @@ function GenericCache (cache_policy, cache_validator) {
}
me.beforeTileRender = function(req, res, callback) {
req.windshaft_start = new Date().getTime()/1000.0;
var key = cache_key(req);
var tile = me.cache.get(key);
if(tile) {
@ -64,6 +67,10 @@ function GenericCache (cache_policy, cache_validator) {
callback(null);
} else {
me.cache_hits++;
var timestamp = new Date().getTime()/1000.0;
var delta = timestamp - req.windshaft_start;
tile.cache_time = delta;
tile.hits++;
res.header('X-Cache-hit', 'true');
res.send(tile.tile, tile.headers, 200);
}
@ -76,7 +83,13 @@ function GenericCache (cache_policy, cache_validator) {
me.afterTileRender = function(req, res, tile, headers, callback) {
var timestamp = new Date().getTime()/1000.0;
me.cache.put(cache_key(req), { tile: tile, headers: headers, timestamp: timestamp});
var delta = timestamp - req.windshaft_start;
me.cache.put(cache_key(req), {
tile: tile,
headers: headers,
timestamp: timestamp,
render_time: delta,
hits: 0});
update_items(me.cache.size || 0);
callback(null, tile, headers);
}
@ -88,13 +101,32 @@ function GenericCache (cache_policy, cache_validator) {
});
}
me.getStats = function() {
me.getStats = function(include_tile_info, sort_by) {
var total = me.cache_hits + me.cache_misses;
var mem = 0;
var blank_tile_count = 0;
var tile_info = []
me.cache.forEach(function(key, value) {
if(value.tile.length !== undefined) {
mem += value.tile.length;
if(value.tile.length == BLANK_TILE_SIZE) {
blank_tile_count++;
}
}
if(include_tile_info) {
tile_info.push({
key: key,
length: value.tile.length,
hits: value.hits,
render_time: value.render_time,
cache_time: value.cache_time
});
}
});
console.log(sort_by);
sort_by = sort_by || 'hits';
tile_info.sort(function(a, b) {
return b[sort_by] - a[sort_by];
});
return {
cache_hits: me.cache_hits,
@ -103,7 +135,11 @@ function GenericCache (cache_policy, cache_validator) {
max_items: me.max_items,
memory: mem,
memory_per_item: total ? mem/total: 0,
ratio: total ? me.cache_hits/total: 0
ratio: total ? me.cache_hits/total: 0,
blank_tile_count: blank_tile_count,
blank_tile_size: blank_tile_count*BLANK_TILE_SIZE,
blank_items_ratio: total? blank_tile_count/total: 0,
tiles: tile_info
};
}