Attachment 'moin_profile.py'

Download

   1 """
   2 profile - profile moin moin
   3 
   4 Usage:
   5 
   6     python profile.py action [options]
   7 
   8 Actions:
   9 
  10     -a --all            profile pages listed on SystemPagesInEnglishGroup
  11     -p --page pagename  profile pagename  
  12     
  13 Options:
  14 
  15     -r --requests num   how many requests to run for each page
  16                         (default 500)
  17     
  18     -s --sleep seconds  how much time to sleep after each request
  19                         (default 0.1)
  20 
  21 Notes:
  22 
  23     Run the script from the distribution directory, where MoinMoin
  24     package lives. You can can also set the system path.
  25 """
  26 
  27 import sys
  28 import os
  29 import re
  30 import gc
  31 import time
  32 from cStringIO import StringIO
  33 
  34 # choose your paths:
  35 #import sys
  36 #sys.path[0:0]=['/var/www/virtualdomains/moin-main/lib/python',
  37 #               '/var/www/virtualdomains/moin-main/wiki']
  38 
  39 from MoinMoin.request import RequestCLI
  40 
  41 
  42 def memory():
  43     """ Return memory usage of current process in KB """
  44     datafile = os.popen('ps -p %s -o rss' % os.getpid())
  45     line = datafile.readlines()[1] 
  46     datafile.close()
  47     return line.strip()  
  48 
  49 _name_re = None
  50 def englishSystemPages():
  51     """ Return a list of pages in the English system pages group """
  52     global _name_re
  53     if not _name_re:
  54         _name_re = re.compile(r'^ \*\s+(?:\[")?(?P<name>.+?)(?:"\])?$', re.MULTILINE)
  55     groupfile = file('wiki/data/text/SystemPagesInEnglishGroup')
  56     text = groupfile.read()
  57     groupfile.close()
  58     names = [match.group('name') for match in _name_re.finditer(text)]
  59     return names
  60     
  61 def getPage(pagename, output):
  62     """ Get page into output """
  63     request = RequestCLI(pagename=pagename)
  64     request.redirect(output)
  65     request.run()
  66     del request
  67     
  68 def do_profile(options):
  69     """ Test pages """
  70     max = options['requests']
  71     samples = max / 10
  72     sleep = options['sleep']   
  73     
  74     for pagename in options['pagenames']:
  75         print
  76         print pagename
  77         requests = 0
  78         while requests < max:
  79             # Run some requests
  80             for i in range(samples):
  81                 output = StringIO()
  82                 getPage(pagename='FrontPage', output=output)
  83                 time.sleep(sleep)
  84                 del output
  85                 requests += 1
  86                 if requests >= max: break
  87             # Sample
  88             print 'requests:%d  memory:%sKB  collect:%d  objects:%d  garbage:%d' % (
  89                 requests, memory(), gc.collect(), len(gc.get_objects()), len(gc.garbage))
  90 
  91 def do_error(message):
  92     print 'Error:', message
  93     print __doc__
  94     sys.exit(1)
  95     
  96 def main():
  97     import getopt
  98     import warnings
  99 
 100     # Ignore import moin_config did not import warnings
 101     warnings.filterwarnings('ignore',
 102                             r'^import of config .+ default configuration used instead.')
 103     
 104     action = None
 105     options = {'requests': 500, 'sleep': 0.1}
 106 
 107     try:
 108         optlist, args = getopt.getopt(sys.argv[1:], 'ap:r:s:',
 109                                       ['all', 'page=', 'requests=', 'sleep='])           
 110         for opt, val in optlist:
 111             if '-a' in opt:
 112                 options['pagenames'] = englishSystemPages()
 113             elif '-p' in opt:
 114                 options['pagenames'] = [val]
 115             elif '-r' in opt:
 116                 options['requests'] = int(val)
 117             elif '-s' in opt:
 118                 options['sleep'] = float(val)
 119 
 120         if not options.has_key('pagenames'):
 121             do_error('Nothing to do')
 122         do_profile(options)
 123         
 124     except (getopt.GetoptError, ValueError), why:
 125         do_error(str(why))
 126     
 127     
 128 if __name__ == '__main__':
 129     main()
 130     

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2003-12-07 18:15:55, 5.5 KB) [[attachment:difflib.tar.gz]]
  • [get | view] (2004-08-21 11:45:07, 3.5 KB) [[attachment:moin_profile.py]]
  • [get | view] (2008-05-01 19:31:25, 2.5 KB) [[attachment:objproxy.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.