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

@ -1,73 +1,73 @@
var _ = require('underscore')
, Step = require('step')
, Windshaft = require('windshaft')
, Cache = require('./tile_cache');
, Windshaft = require('windshaft')
, Cache = require('./tile_cache');
var CartodbWindshaft = function(serverOptions) {
// set cache if requested
if(serverOptions.lru_cache) {
var lru_cache = Cache.LRUcache(serverOptions.lru_cache_size || 10000, serverOptions.redis);
_.extend(serverOptions, {
beforeTileRender: lru_cache.beforeTileRender,
afterTileRender: lru_cache.afterTileRender,
cacheStats: lru_cache.getStats,
afterStateChange: lru_cache.afterStateChange
})
}
// set cache if requested
if(serverOptions.lru_cache) {
var lru_cache = Cache.LRUcache(serverOptions.lru_cache_size || 10000, serverOptions.redis);
_.extend(serverOptions, {
beforeTileRender: lru_cache.beforeTileRender,
afterTileRender: lru_cache.afterTileRender,
cacheStats: lru_cache.getStats,
afterStateChange: lru_cache.afterStateChange
})
}
// boot
var ws = new Windshaft.Server(serverOptions);
/**
* Helper to allow access to the layer to be used in the maps infowindow popup.
*/
ws.get(serverOptions.base_url + '/infowindow', function(req, res){
Step(
function(){
serverOptions.getInfowindow(req, this);
},
function(err, data){
if (err){
res.send(err.message, 400);
} else {
res.send({infowindow: data}, 200);
}
}
);
});
// boot
var ws = new Windshaft.Server(serverOptions);
/**
* Helper to allow access to the layer to be used in the maps infowindow popup.
*/
ws.get(serverOptions.base_url + '/infowindow', function(req, res){
Step(
function(){
serverOptions.getInfowindow(req, this);
},
function(err, data){
if (err){
res.send(err.message, 400);
} else {
res.send({infowindow: data}, 200);
}
}
);
});
/**
* Helper to allow access to metadata to be used in embedded maps.
*/
ws.get(serverOptions.base_url + '/map_metadata', function(req, res){
Step(
function(){
serverOptions.getMapMetadata(req, this);
},
function(err, data){
if (err){
res.send(err.message, 400);
} else {
res.send({map_metadata: data}, 200);
}
}
);
});
/**
* Helper to allow access to metadata to be used in embedded maps.
*/
ws.get(serverOptions.base_url + '/map_metadata', function(req, res){
Step(
function(){
serverOptions.getMapMetadata(req, this);
},
function(err, data){
if (err){
res.send(err.message, 400);
} else {
res.send({map_metadata: data}, 200);
}
}
);
});
/**
* tile cache stats
*/
ws.get('/cache', function(req, res){
if(serverOptions.cacheStats) {
res.send(serverOptions.cacheStats());
} else {
res.send("Cache no enabled")
}
});
return ws;
/**
* tile cache stats
*/
ws.get('/cache', function(req, res){
if(serverOptions.cacheStats) {
res.send(serverOptions.cacheStats(req.query.tile_info, req.query.sort_by));
} else {
res.send("Cache no enabled")
}
});
return ws;
}
module.exports = CartodbWindshaft;

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 !== 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,9 +135,13 @@ 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
};
}
return me;
return me;
}