Created
January 19, 2017 15:53
-
-
Save guewen/9942971c9d08e1eb46021c08a8f9505c to your computer and use it in GitHub Desktop.
Log Odoo cache statistics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Copyright 2016 Camptocamp SA | |
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) | |
import logging | |
import random | |
import threading | |
from collections import defaultdict | |
from openerp import models | |
from openerp.modules.registry import RegistryManager | |
from openerp.tools.cache import STAT | |
_logger = logging.getLogger('monitoring.cache') | |
class IrHttp(models.AbstractModel): | |
_inherit = 'ir.http' | |
@staticmethod | |
def log_ormcache_stats(): | |
""" Log statistics of ormcache usage by database, model, and method. | |
""" | |
me = threading.currentThread() | |
me_dbname = me.dbname | |
entries = defaultdict(int) | |
for dbname, reg in RegistryManager.registries.iteritems(): | |
for key in reg.cache.iterkeys(): | |
entries[(dbname,) + key[:2]] += 1 | |
for key, count in sorted(entries.items()): | |
dbname, model_name, method = key | |
me.dbname = dbname | |
stat = STAT[key] | |
info = { | |
'dbname': dbname, | |
'model_name': model_name, | |
'method': method.__name__, | |
'count': count, | |
'hit': stat.hit, | |
'miss': stat.miss, | |
'err': stat.err, | |
'ratio': stat.ratio, | |
} | |
_logger.info(info) | |
me.dbname = me_dbname | |
def _dispatch(self): | |
response = super(IrHttp, self)._dispatch() | |
if random.random() < 0.01: | |
self.log_ormcache_stats() | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment