<s> """<STR_LIT>""" <EOL> import argparse <EOL> import cProfile <EOL> import pstats <EOL> import os <EOL> import time <EOL> import re <EOL> import sys <EOL> class Profiler ( object ) : <EOL> tests = [ ] <EOL> _setup = None <EOL> _setup_once = None <EOL> name = None <EOL> num = <NUM_LIT:0> <EOL> def __init__ ( self , options ) : <EOL> self . test = options . test <EOL> self . dburl = options . dburl <EOL> self . runsnake = options . runsnake <EOL> self . profile = options . profile <EOL> self . dump = options . dump <EOL> self . callers = options . callers <EOL> self . num = options . num <EOL> self . echo = options . echo <EOL> self . stats = [ ] <EOL> @ classmethod <EOL> def init ( cls , name , num ) : <EOL> cls . name = name <EOL> cls . num = num <EOL> @ classmethod <EOL> def profile ( cls , fn ) : <EOL> if cls . name is None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> cls . tests . append ( fn ) <EOL> return fn <EOL> @ classmethod <EOL> def setup ( cls , fn ) : <EOL> if cls . _setup is not None : <EOL> raise ValueError ( "<STR_LIT>" % cls . _setup ) <EOL> cls . _setup = staticmethod ( fn ) <EOL> return fn <EOL> @ classmethod <EOL> def setup_once ( cls , fn ) : <EOL> if cls . _setup_once is not None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % cls . _setup_once ) <EOL> cls . _setup_once = staticmethod ( fn ) <EOL> return fn <EOL> def run ( self ) : <EOL> if self . test : <EOL> tests = [ fn for fn in self . tests if fn . __name__ == self . test ] <EOL> if not tests : <EOL> raise ValueError ( "<STR_LIT>" % self . test ) <EOL> else : <EOL> tests = self . tests <EOL> if self . _setup_once : <EOL> print ( "<STR_LIT>" ) <EOL> self . _setup_once ( self . dburl , self . echo , self . num ) <EOL> print ( "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( [ t . __name__ for t in tests ] ) ) <EOL> for test in tests : <EOL> self . _run_test ( test ) <EOL> self . stats [ - <NUM_LIT:1> ] . report ( ) <EOL> def _run_with_profile ( self , fn ) : <EOL> pr = cProfile . Profile ( ) <EOL> pr . enable ( ) <EOL> try : <EOL> result = fn ( self . num ) <EOL> finally : <EOL> pr . disable ( ) <EOL> stats = pstats . Stats ( pr ) . sort_stats ( '<STR_LIT>' ) <EOL> self . stats . append ( TestResult ( self , fn , stats = stats ) ) <EOL> return result <EOL> def _run_with_time ( self , fn ) : <EOL> now = time . time ( ) <EOL> try : <EOL> return fn ( self . num ) <EOL> finally : <EOL> total = time . time ( ) - now <EOL> self . stats . append ( TestResult ( self , fn , total_time = total ) ) <EOL> def _run_test ( self , fn ) : <EOL> if self . _setup : <EOL> self . _setup ( self . dburl , self . echo , self . num ) <EOL> if self . profile or self . runsnake or self . dump : <EOL> self . _run_with_profile ( fn ) <EOL> else : <EOL> self . _run_with_time ( fn ) <EOL> @ classmethod <EOL> def main ( cls ) : <EOL> parser = argparse . ArgumentParser ( "<STR_LIT>" ) <EOL> if cls . name is None : <EOL> parser . add_argument ( <EOL> "<STR_LIT:name>" , choices = cls . _suite_names ( ) , help = "<STR_LIT>" ) <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> potential_name = sys . argv [ <NUM_LIT:1> ] <EOL> try : <EOL> suite = __import__ ( __name__ + "<STR_LIT:.>" + potential_name ) <EOL> except ImportError : <EOL> pass <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , type = str , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , type = str , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , type = int , default = cls . num , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" % cls . num <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> args . dump = args . dump or args . callers <EOL> args . profile = args . profile or args . dump or args . runsnake <EOL> if cls . name is None : <EOL> suite = __import__ ( __name__ + "<STR_LIT:.>" + args . name ) <EOL> Profiler ( args ) . run ( ) <EOL> @ classmethod <EOL> def _suite_names ( cls ) : <EOL> suites = [ ] <EOL> for file_ in os . listdir ( os . path . dirname ( __file__ ) ) : <EOL> match = re . match ( r'<STR_LIT>' , file_ ) <EOL> if match : <EOL> suites . append ( match . group ( <NUM_LIT:1> ) ) <EOL> return suites <EOL> class TestResult ( object ) : <EOL> def __init__ ( self , profile , test , stats = None , total_time = None ) : <EOL> self . profile = profile <EOL> self . test = test <EOL> self . stats = stats <EOL> self . total_time = total_time <EOL> def report ( self ) : <EOL> print ( self . _summary ( ) ) <EOL> if self . profile . profile : <EOL> self . report_stats ( ) <EOL> def _summary ( self ) : <EOL> summary = "<STR_LIT>" % ( <EOL> self . test . __name__ , self . test . __doc__ , self . profile . num ) <EOL> if self . total_time : <EOL> summary += "<STR_LIT>" % self . total_time <EOL> if self . stats : <EOL> summary += "<STR_LIT>" % self . stats . total_calls <EOL> return summary <EOL> def report_stats ( self ) : <EOL> if self . profile . runsnake : <EOL> self . _runsnake ( ) <EOL> elif self . profile . dump : <EOL> self . _dump ( ) <EOL> def _dump ( self ) : <EOL> self . stats . sort_stats ( '<STR_LIT:time>' , '<STR_LIT>' ) <EOL> self . stats . print_stats ( ) <EOL> if self . profile . callers : <EOL> self . stats . print_callers ( ) <EOL> def _runsnake ( self ) : <EOL> filename = "<STR_LIT>" % self . test . __name__ <EOL> try : <EOL> self . stats . dump_stats ( filename ) <EOL> os . system ( "<STR_LIT>" % filename ) <EOL> finally : <EOL> os . remove ( filename ) </s>
