[Zope] performance concern

zopedan zopedan@hotmail.com
Fri, 5 Apr 2002 15:21:22 -0800


On the topic of performace concerns in general I decided to code a
python version of PHP's PEAR BenchMarkTimer class... does nothing
complex at all and is just a wrapper for what you are already using in
your code but puts it in an external method and gives it a more easy to
type interface ... perhaps might be userfull... I simply found it very
familiar and quick for marking my queries during development


Usage:

declare an external method to newBenchmarkTimer() with id BenchmarkTimer

<dtml-let timer="BenchmarkTimer()">

  <dtml-call "timer.start()">
    <dtml-call expensiveCode>
  <dtml-call "timer.stop()">
  <dtml-var "timer.timeElapsed()">

  <dtml-call "timer.setMarker('my_start')">
    <dtml-call expensiveCode>
  <dtml-call "timer.setMarker('my_stop')">
  <dtml-var "timer.timeElapsed('my_start','my_stop')">

</dtml-let>

can be used in python just the same

==== BEGIN benchmarktimer.py ====
from AccessControl import ClassSecurityInfo
from Acquisition import Implicit
import Globals
import time

def newBenchmarkTimer():
    return BenchmarkTimer()

class BenchmarkTimer(Implicit):
    
    def __init__(self):
        "constructor"
        self.markers = { 'Start' : 0, 'Stop' : 0 }

    security = ClassSecurityInfo()
    security.declareObjectPublic()

    security.declarePublic('start')
    def start(self):
        "set the marker 'Start'"
        self.setMarker('Start')

    security.declarePublic('stop')
    def stop(self):
        "set the marker 'Stop'"
        self.setMarker('Stop')

    security.declarePublic('setMarker')
    def setMarker(self,name):
        "set the specific marker"
        self.markers[name] = time.clock()

    security.declarePublic('timeElapsed')
    def timeElapsed(self, start='Start', end='Stop' ):
        "difference between two markers, order is important"
        return self.markers[end]-self.markers[start]
        
    security.declarePublic('getProfiling')
    def getProfiling(self):
        """
        name  -> name of marker
        time  -> absolute time set in marker
        diff  -> difference between this marker and last marker
        total -> difference between this marker and 'Start' marker
        """
        i       = 0
        total   = 0
        profile = []
        for ( marker, time ) in self.markers:
            if marker == 'Start':
                diff = 0
            else:
                diff  = time - temp;
                total = total + diff;
            profile[i]['name']  = marker
            profile[i]['time']  = time
            profile[i]['diff']  = diff
            profile[i]['total'] = total
            temp = time
            i = i+1
        return result

Globals.InitializeClass(BenchmarkTimer)
==== END benchamrktimer.py ====

> <dtml-call "REQUEST.set('starttime',_.DateTime().millis())">
> Start Time (ms): <dtml-var starttime><br>
> <dtml-call
> "REQUEST.set('testlist',['1','2','3','4','5','6','7','8','9','10'])">
> <dtml-in "testlist">
>   <dtml-var sequence-item>&nbsp;
> </dtml-in>
> <br>
> <dtml-call "REQUEST.set('endtime',_.DateTime().millis())">
> End Time (ms): <dtml-var endtime><br>
> <hr size=1>
> Delay (ms) = <dtml-var "endtime - starttime"><br>
> <dtml-var standard_html_footer>
>
> Any help would be extremely welcome...as is, the site is too slow to
demo to
> potential clients.