Merge pull request #22 from gutenbergtools/diagnostics

Add Diagnostics
diagnostics
eshellman 2019-07-29 18:30:10 -04:00 committed by GitHub
commit d37fb1f095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 0 deletions

View File

@ -39,6 +39,7 @@ from SearchPage import BookSearchPage, AuthorSearchPage, SubjectSearchPage, Book
from BibrecPage import BibrecPage
import CoverPages
import QRCodePage
import diagnostics
import Sitemap
import Formatters
@ -256,6 +257,9 @@ def main():
d.connect('iplimit', r'/iplimit/',
controller=Page.NullPage())
d.connect('diagnostics', r'/diagnostics/',
controller=diagnostics.DiagnosticsPage())
d.connect('stats', r'/stats/',
controller=Page.NullPage(), _static=True)

67
diagnostics.py Executable file
View File

@ -0,0 +1,67 @@
#!/usr/bin/env python
# -*- mode: python; indent-tabs-mode: nil; -*- coding: utf-8 -*-
"""
diagnostics.py
Copyright 2019 by Eric Hellman
Distributable under the GNU General Public License Version 3 or newer.
"""
import json
import resource
import sys
from collections import Mapping, Container
from sys import getsizeof
import cherrypy
from cherrypy.lib.sessions import RamSession
from Page import Page
def deep_getsizeof(o, ids):
"""Find the memory footprint of a Python object
This is a recursive function that rills down a Python object graph
like a dictionary holding nested ditionaries with lists of lists
and tuples and sets.
The sys.getsizeof function does a shallow size of only. It counts each
object inside a container as pointer only regardless of how big it
really is.
:param o: the object
:param ids:
:return:
https://github.com/the-gigi/deep/blob/master/deeper.py
"""
d = deep_getsizeof
if id(o) in ids:
return 0
r = getsizeof(o)
ids.add(id(o))
if isinstance(o, str):
return r
if isinstance(o, Mapping):
return r + sum(d(k, ids) + d(v, ids) for k, v in o.items())
if isinstance(o, Container):
return r + sum(d(x, ids) for x in o)
return r
class DiagnosticsPage (Page):
""" Python health. """
@cherrypy.tools.json_out()
def index (self, **dummy_kwargs):
""" return stats. """
stats = {}
stats['sessions'] = len(RamSession.cache)
stats['sessions_storage'] = deep_getsizeof(RamSession.cache, set())
stats['allocated_blocks'] = sys.getallocatedblocks()
stats['rusage_self'] = resource.getrusage(resource.RUSAGE_SELF)
stats['rusage_children'] = resource.getrusage(resource.RUSAGE_CHILDREN)
return stats