Commits

Robert Brewer committed d91d130

Touchups to cpstats.py

Comments (0)

Files changed (2)

cherrypy/lib/cpstats.py

 
 In addition to scalars, values in a namespace MAY be a (third-layer)
 dict, or a list, called a "collection". For example, the CherryPy StatsTool
-keeps track of what each worker thread is doing (or has most recently done)
-in a 'Worker Threads' collection, where each key is a thread ID; each
+keeps track of what each request is doing (or has most recently done)
+in a 'Requests' collection, where each key is a thread ID; each
 value in the subdict MUST be a fourth dict (whew!) of statistical data about
 each thread. We call each subdict in the collection a "record". Similarly,
 the StatsTool also keeps a list of slow queries, where each record contains
     'Bytes Read/Request': lambda s: (
         (s['Total Bytes Read'] / float(s['Total Requests']))
         if s['Total Requests'] else 0.0),
-    'Bytes Read/Second': lambda s: s['Total Bytes Read'] / (time.time() - s['Start Time']),
+    'Bytes Read/Second': lambda s: s['Total Bytes Read'] / s['Uptime'](s),
     'Bytes Written/Request': lambda s: (
         (s['Total Bytes Written'] / float(s['Total Requests']))
         if s['Total Requests'] else 0.0),
-    'Bytes Written/Second': lambda s: s['Total Bytes Written'] / (time.time() - s['Start Time']),
+    'Bytes Written/Second': lambda s: s['Total Bytes Written'] / s['Uptime'](s),
     'Current Time': lambda s: time.time(),
-    'Current Workers': 0,
-    'Idle Workers': lambda s: cherrypy.server.thread_pool - s['Current Workers'],
-    'Requests/Second': lambda s: float(s['Total Requests']) / (time.time() - s['Start Time']),
+    'Current Requests': 0,
+    'Requests/Second': lambda s: float(s['Total Requests']) / s['Uptime'](s),
     'Server Version': cherrypy.__version__,
     'Start Time': time.time(),
     'Total Bytes Read': 0,
     'Total Requests': 0,
     'Total Time': 0,
     'Uptime': lambda s: time.time() - s['Start Time'],
-    'Worker Threads': {},
+    'Requests': {},
     })
 
 proc_time = lambda s: time.time() - s['Start Time']
-idle_time = lambda s: time.time() - s['End Time']
 
 
 class ByteCountWrapper(object):
 
 
 class StatsTool(cherrypy.Tool):
-    """Record various information about the current worker thread."""
+    """Record various information about the current request."""
     
     def __init__(self):
         cherrypy.Tool.__init__(self, 'on_end_request', self.record_stop)
         request = cherrypy.serving.request
         if not hasattr(request.rfile, 'bytes_read'):
             request.rfile = ByteCountWrapper(request.rfile)
+            request.body.fp = request.rfile
         
         r = request.remote
         
-        appstats['Current Workers'] += 1
+        appstats['Current Requests'] += 1
         appstats['Total Requests'] += 1
-        appstats['Worker Threads'][threading._get_ident()] = {
+        appstats['Requests'][threading._get_ident()] = {
             'Bytes Read': None,
             'Bytes Written': None,
             'Client': '%s:%s' % (r.ip, r.port),
             'End Time': None,
-            'Idle Time': 0,
             'Processing Time': proc_time,
             'Request-Line': request.request_line,
             'Response Status': None,
     def record_stop(self, uriset=None, slow_queries=1.0, slow_queries_count=100,
                     debug=False, **kwargs):
         """Record the end of a request."""
-        w = appstats['Worker Threads'][threading._get_ident()]
+        w = appstats['Requests'][threading._get_ident()]
         
         r = cherrypy.request.rfile.bytes_read
         w['Bytes Read'] = r
         p = w['End Time'] - w['Start Time']
         w['Processing Time'] = p
         appstats['Total Time'] += p
-        w['Idle Time'] = idle_time
         
-        appstats['Current Workers'] -= 1
+        appstats['Current Requests'] -= 1
         
         if debug:
             cherrypy.log('Stats recorded: %s' % repr(w), 'TOOLS.CPSTATS')
             'Uptime': '%.3f',
             'Slow Queries': {
                 'End Time': None,
-                'Idle Time': None,
                 'Processing Time': '%.3f',
                 'Start Time': iso_format,
                 },
                 'Min': '%.3f',
                 'Sum': '%.3f',
                 },
-            'Worker Threads': {
-                'Bytes Read': None,
-                'Bytes Written': None,
+            'Requests': {
+                'Bytes Read': '%s',
+                'Bytes Written': '%s',
                 'End Time': None,
-                'Idle Time': '%.3f',
                 'Processing Time': '%.3f',
                 'Start Time': None,
                 },
     
     def get_dict_collection(self, v, formatting):
         """Return ([headers], [rows]) for the given collection."""
-        # E.g., the 'Worker Threads' dict.
+        # E.g., the 'Requests' dict.
         headers = []
         for record in v.itervalues():
             for k3 in record:

cherrypy/tutorial/tut03_get_and_post.py

 """
 
 import cherrypy
-from cherrypy.lib import cpstats
+
 
 class WelcomePage:
 
-    cpstats = cpstats.StatsPage()
-    
     def index(self):
         # Ask for the user's name.
         return '''
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.