<s> import os <EOL> import time <EOL> import cProfile <EOL> import gprof2dot <EOL> from pstats import Stats <EOL> import pyprof2calltree <EOL> from nailgun . settings import settings <EOL> class ProfilerMiddleware ( object ) : <EOL> def __init__ ( self , app ) : <EOL> self . _app = app <EOL> def __call__ ( self , environ , start_response ) : <EOL> response_body = [ ] <EOL> def catching_start_response ( status , headers , exc_info = None ) : <EOL> start_response ( status , headers , exc_info ) <EOL> return response_body . append <EOL> def runapp ( ) : <EOL> appiter = self . _app ( environ , catching_start_response ) <EOL> response_body . extend ( appiter ) <EOL> if hasattr ( appiter , '<STR_LIT>' ) : <EOL> appiter . close ( ) <EOL> handler_name = environ . get ( '<STR_LIT>' ) . strip ( '<STR_LIT:/>' ) . replace ( '<STR_LIT:/>' , '<STR_LIT:.>' ) or '<STR_LIT:root>' <EOL> profiler = Profiler ( environ [ '<STR_LIT>' ] , handler_name ) <EOL> profiler . profiler . runcall ( runapp ) <EOL> body = b'<STR_LIT>' . join ( response_body ) <EOL> profiler . save_data ( ) <EOL> return [ body ] <EOL> class Profiler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method = '<STR_LIT>' , handler_name = '<STR_LIT>' ) : <EOL> self . method = method <EOL> self . handler_name = handler_name <EOL> if not os . path . exists ( settings . <EOL> LOAD_TESTS_PATHS [ '<STR_LIT>' ] ) : <EOL> os . makedirs ( settings . LOAD_TESTS_PATHS [ '<STR_LIT>' ] ) <EOL> self . profiler = cProfile . Profile ( ) <EOL> self . profiler . enable ( ) <EOL> self . start = time . time ( ) <EOL> def save_data ( self ) : <EOL> elapsed = time . time ( ) - self . start <EOL> pref_filename = os . path . join ( <EOL> settings . LOAD_TESTS_PATHS [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' . <EOL> format ( <EOL> method = self . method , <EOL> handler_name = self . handler_name or '<STR_LIT:root>' , <EOL> elapsed_time = elapsed * <NUM_LIT> , <EOL> t_time = time . time ( ) ) ) <EOL> tree_file = pref_filename + '<STR_LIT>' <EOL> stats_file = pref_filename + '<STR_LIT>' <EOL> callgraph_file = pref_filename + '<STR_LIT>' <EOL> with open ( stats_file , '<STR_LIT:w>' ) as file_o : <EOL> stats = Stats ( self . profiler , stream = file_o ) <EOL> stats . sort_stats ( '<STR_LIT:time>' , '<STR_LIT>' ) . print_stats ( ) <EOL> parser = gprof2dot . PstatsParser ( self . profiler ) <EOL> def get_function_name ( arg ) : <EOL> filename , line , name = arg <EOL> module = os . path . splitext ( filename ) [ <NUM_LIT:0> ] <EOL> module_pieces = module . split ( os . path . sep ) <EOL> return "<STR_LIT>" . format ( <EOL> module = "<STR_LIT:/>" . join ( module_pieces [ - <NUM_LIT:4> : ] ) , <EOL> line = line , <EOL> name = name ) <EOL> parser . get_function_name = get_function_name <EOL> gprof = parser . parse ( ) <EOL> with open ( callgraph_file , '<STR_LIT:w>' ) as file_o : <EOL> dot = gprof2dot . DotWriter ( file_o ) <EOL> theme = gprof2dot . TEMPERATURE_COLORMAP <EOL> dot . graph ( gprof , theme ) <EOL> call_tree = pyprof2calltree . CalltreeConverter ( stats ) <EOL> with open ( tree_file , '<STR_LIT:wb>' ) as file_o : <EOL> call_tree . output ( file_o ) </s>
