Commits

tuck  committed e8bb9cb

start to add sql average time measure

  • Participants
  • Parent commits a297b5b
  • Branches 0.2

Comments (0)

Files changed (4)

File pyramid_perfstat/__init__.py

 def perf_tween_factory(handler, registry):
     """
     """
-    
     PerfDbManager(None).init_db()
     
     def perf_tween(request, getLogger=logging.getLogger):
         """
-        TODO : should avoid it's own url
-        TODO : get route_name
-        TODO : get view name (or full path,function name ...)
         """
         t0=time.time()
         result = handler(request)
     config.add_route('pyramid_perfstat.reporting.url_detail', '/stat/{id_session}/{id_view}/{id_route}',
                      custom_predicates=(to_int('id_session', 'id_view', 'id_route'),))
     
-    config.add_view('pyramid_perfstat.views.reporting',
-                    route_name='pyramid_perfstat.reporting',
-                    renderer='pyramid_perfstat:templates/reporting.mako')
-
-    config.add_view('pyramid_perfstat.views.reporting',
-                    route_name='pyramid_perfstat.reporting.session_detail',
-                    renderer='pyramid_perfstat:templates/reporting.mako')
-
-    config.add_view('pyramid_perfstat.views.reporting',
-                    route_name='pyramid_perfstat.reporting.view_detail',
-                    renderer='pyramid_perfstat:templates/reporting.mako')
-
-    config.add_view('pyramid_perfstat.views.reporting',
-                    route_name='pyramid_perfstat.reporting.url_detail',
-                    renderer='pyramid_perfstat:templates/reporting.mako')
-
-    config.add_view('pyramid_perfstat.views.reset_db',
-                    route_name='pyramid_perfstat.reset')
     
-
 def includeme(config):
     """
     Set up am implicit :term:`tween` to log performance information of each
     config.add_tween('pyramid_perfstat.perf_tween_factory', under=EXCVIEW)
     config.include(include_perf_routes, route_prefix=ROUTE_PREFIX)
 
+    # scan views
+    config.scan('pyramid_perfstat.views')
+

File pyramid_perfstat/perf_serializer.py

 from pyramid.events import NewRequest
 from pyramid_perfstat.utils import ROUTE_PREFIX
 
+try:
+    from sqlalchemy import event
+    from sqlalchemy.engine.base import Engine
+
+    @event.listens_for(Engine, "before_cursor_execute")
+    def _before_cursor_execute(conn, cursor, stmt, params, context, execmany):
+        setattr(conn, 'pfstb_start_timer', time.time())
+
+    @event.listens_for(Engine, "after_cursor_execute")
+    def _after_cursor_execute(conn, cursor, stmt, params, context, execmany):
+        stop_timer = time.time()
+        request = get_current_request()
+        duration = stop_timer - conn.pfstb_start_timer,
+
+        if request is not None:
+            with lock:
+                duration_queries = getattr(request, 'pfstb_duration_queries', [])
+                duration_queries.append(duration)
+                setattr(request, 'pfstb_duration_queries', duration_queries)
+        delattr(conn, 'pfstb_start_timer')
+    has_sqla = True
+except ImportError:
+    has_sqla = False
+
+
 def new_perfstat_request_subscriber(event):
     request = event.request
     request._PerfDbManager__perfstat_db = sqlite3.connect('perf_app.db')
         c.close()
         return measure, fetch_count
 
-
     def get_last_view_mean_perf(self):
         """
           return last value inserted in pyramid_agg_route_perf
                                                    self.session_perf_id,
                                                    self.view_name))         
         c.close()
-        
+    
+    def cumulative_upkeep(self, last_resume, last_cpt, perf):
+        """
+        :param last_resume: the last recorded average
+        :param last_cpt: last average number of elements having served for the calculation
+        :param perf: new record
+        """
+        result = None
+        if last_resume is None :
+           result = perf
+        else :
+           result = (last_resume+(perf/float(last_cpt)))*(float(last_cpt)/float(last_cpt+1))
+        return result
+    
     def insert_data(self, perf):
         """
         insert the perf data in db
                if url dispatched si used and request.matched_route is fill
         :param pef: the time measure (should be ms)
         """
+
+        if has_sqla and hasattr(self.request,'pfstb_duration_queries') :
+              duration_queries_liste = self.request.pfstb_duration_queries
+              sigma_duration = sum(duration_queries_liste)
+        else :
+              sigma_duration = None
+
         # we forget pyramid_perfstat self routes
         if self.matched_route_name is not None and self.matched_url.count(ROUTE_PREFIX) == 0 :
              c = self.conn.cursor()
                                                         datetime.datetime.now(),))
      
              last_record_resume, last_cpt = self.get_last_route_mean_perf()
-     
+             new_record_resume = self.cumulative_upkeep(last_record_resume, last_cpt, perf)
              if last_record_resume is None :
-                last_record_resume = perf
-                self.update_mean_route_perf_value(last_record_resume, insert=True)
+                self.update_mean_route_perf_value(new_record_resume, insert=True)
              else :
-                last_record_resume = (last_record_resume+(perf/float(last_cpt)))*(float(last_cpt)/float(last_cpt+1))
-                self.update_mean_route_perf_value(last_record_resume)
+                self.update_mean_route_perf_value(new_record_resume)
      
              last_record_resume, last_cpt = self.get_last_view_mean_perf()
+             new_record_resume = self.cumulative_upkeep(last_record_resume, last_cpt, perf)
              if last_record_resume is None :
-                last_record_resume = perf
-                self.update_mean_view_perf_value(last_record_resume, insert=True)
+                self.update_mean_view_perf_value(new_record_resume, insert=True)
              else :
-                last_record_resume = (last_record_resume+(perf/float(last_cpt)))*(float(last_cpt)/float(last_cpt+1))
-                self.update_mean_view_perf_value(last_record_resume)
-     
+                self.update_mean_view_perf_value(new_record_resume)
      
              self.conn.commit()
              c.close()

File pyramid_perfstat/views.py

 #-----------------------------------------------------------------------------
 
 from pyramid.httpexceptions import HTTPFound
-
+from pyramid.view import view_config
 from pyramid_perfstat.perf_serializer import PerfDbManager
 from pyramid_perfstat import utils
+from pyramid.security import NO_PERMISSION_REQUIRED
 
+try:
+    from sqlalchemy import event
+    from sqlalchemy.engine.base import Engine
+
+    @event.listens_for(Engine, "before_cursor_execute")
+    def _before_cursor_execute(conn, cursor, stmt, params, context, execmany):
+        setattr(conn, 'pfstb_start_timer', time.time())
+
+    @event.listens_for(Engine, "after_cursor_execute")
+    def _after_cursor_execute(conn, cursor, stmt, params, context, execmany):
+        stop_timer = time.time()
+        request = get_current_request()
+        if request is not None:
+            with lock:
+                duration = stop_timer - conn.pfstb_start_timer,
+                duration_queries = getattr(request, 'pfstb_duration_queries', [])
+                duration_queries.append(duration)
+                setattr(request, 'pfstb_duration_queries', duration_queries)
+        delattr(conn, 'pfstb_start_timer')
+    has_sqla = True
+except ImportError:
+    has_sqla = False
+
+@view_config(route_name='pyramid_perfstat.reporting',
+             renderer='pyramid_perfstat:templates/reporting.mako',
+             permission=NO_PERMISSION_REQUIRED)
+@view_config(route_name='pyramid_perfstat.reporting.session_detail',
+             renderer='pyramid_perfstat:templates/reporting.mako',
+             permission=NO_PERMISSION_REQUIRED)
+@view_config(route_name='pyramid_perfstat.reporting.view_detail',
+             renderer='pyramid_perfstat:templates/reporting.mako',
+             permission=NO_PERMISSION_REQUIRED)
+@view_config(route_name='pyramid_perfstat.reporting.url_detail',
+             renderer='pyramid_perfstat:templates/reporting.mako',
+             permission=NO_PERMISSION_REQUIRED)
 def reporting(request):
     """
     display tables with all reports ...
                 'lst_urls_measures':lst_urls_measures
             }
 
+@view_config(route_name='pyramid_perfstat.reset',
+             permission=NO_PERMISSION_REQUIRED)
 def reset_db(request):
     """
     """
 
 setup(name='pyramid_perfstat',
       license='BSD License',
-      version='0.1.0',
+      version='0.1.1',
       description='Pyramid PerfStat logs and reports statistics about time usage of a pyramid webapp.',
       long_description=long_description,
       author='Bard Stéphane',
       classifiers=[
           'Development Status :: 4 - Beta',
           'Intended Audience :: Developers',
-          'License :: OSI Approved :: MIT License',
+          'License :: OSI Approved :: BSD License',
           'Natural Language :: English',
           'Operating System :: OS Independent',
           'Programming Language :: Python',