<s> """<STR_LIT>""" <EOL> import logging <EOL> from pprint import pformat <EOL> from google . appengine . ext . appstats import recording <EOL> import cleanup <EOL> import unformatter <EOL> import util <EOL> class Profile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> recording . config . MAX_REPR = <NUM_LIT> <EOL> self . recorder = None <EOL> def results ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . recorder : <EOL> logging . warn ( "<STR_LIT>" ) <EOL> return { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> } <EOL> total_call_count = <NUM_LIT:0> <EOL> total_time = <NUM_LIT:0> <EOL> calls = [ ] <EOL> service_totals_dict = { } <EOL> likely_dupes = False <EOL> end_offset_last = <NUM_LIT:0> <EOL> requests_set = set ( ) <EOL> appstats_key = long ( self . recorder . start_timestamp * <NUM_LIT:1000> ) <EOL> for trace in self . recorder . traces : <EOL> total_call_count += <NUM_LIT:1> <EOL> total_time += trace . duration_milliseconds ( ) <EOL> if trace . start_offset_milliseconds ( ) < end_offset_last : <EOL> total_time -= ( end_offset_last - trace . start_offset_milliseconds ( ) ) <EOL> end_offset_last = trace . start_offset_milliseconds ( ) + trace . duration_milliseconds ( ) <EOL> service_prefix = trace . service_call_name ( ) <EOL> if "<STR_LIT:.>" in service_prefix : <EOL> service_prefix = service_prefix [ : service_prefix . find ( "<STR_LIT:.>" ) ] <EOL> if service_prefix not in service_totals_dict : <EOL> service_totals_dict [ service_prefix ] = { <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> } <EOL> service_totals_dict [ service_prefix ] [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> service_totals_dict [ service_prefix ] [ "<STR_LIT>" ] += trace . duration_milliseconds ( ) <EOL> stack_frames_desc = [ ] <EOL> for frame in trace . call_stack_list ( ) : <EOL> stack_frames_desc . append ( "<STR_LIT>" % <EOL> ( util . short_rpc_file_fmt ( frame . class_or_file_name ( ) ) , <EOL> frame . line_number ( ) , <EOL> frame . function_name ( ) ) ) <EOL> request = trace . request_data_summary ( ) <EOL> response = trace . response_data_summary ( ) <EOL> likely_dupe = request in requests_set <EOL> likely_dupes = likely_dupes or likely_dupe <EOL> requests_set . add ( request ) <EOL> request_short = request_pretty = None <EOL> response_short = response_pretty = None <EOL> miss = <NUM_LIT:0> <EOL> try : <EOL> request_object = unformatter . unformat ( request ) <EOL> response_object = unformatter . unformat ( response ) <EOL> request_short , response_short , miss = cleanup . cleanup ( request_object , response_object ) <EOL> request_pretty = pformat ( request_object ) <EOL> response_pretty = pformat ( response_object ) <EOL> except Exception , e : <EOL> logging . warning ( "<STR_LIT>" , <EOL> e , request , response , exc_info = True ) <EOL> service_totals_dict [ service_prefix ] [ "<STR_LIT>" ] += miss <EOL> calls . append ( { <EOL> "<STR_LIT>" : trace . service_call_name ( ) , <EOL> "<STR_LIT>" : util . milliseconds_fmt ( trace . start_offset_milliseconds ( ) ) , <EOL> "<STR_LIT>" : util . milliseconds_fmt ( trace . duration_milliseconds ( ) ) , <EOL> "<STR_LIT>" : request_pretty or request , <EOL> "<STR_LIT>" : response_pretty or response , <EOL> "<STR_LIT>" : request_short or cleanup . truncate ( request ) , <EOL> "<STR_LIT>" : response_short or cleanup . truncate ( response ) , <EOL> "<STR_LIT>" : stack_frames_desc , <EOL> "<STR_LIT>" : likely_dupe , <EOL> } ) <EOL> service_totals = [ ] <EOL> for service_prefix in service_totals_dict : <EOL> service_totals . append ( { <EOL> "<STR_LIT>" : service_prefix , <EOL> "<STR_LIT>" : service_totals_dict [ service_prefix ] [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : service_totals_dict [ service_prefix ] [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : util . milliseconds_fmt ( service_totals_dict [ service_prefix ] [ "<STR_LIT>" ] ) , <EOL> } ) <EOL> service_totals = sorted ( service_totals , reverse = True , key = lambda service_total : float ( service_total [ "<STR_LIT>" ] ) ) <EOL> return { <EOL> "<STR_LIT>" : total_call_count , <EOL> "<STR_LIT>" : util . milliseconds_fmt ( total_time ) , <EOL> "<STR_LIT>" : calls , <EOL> "<STR_LIT>" : service_totals , <EOL> "<STR_LIT>" : likely_dupes , <EOL> "<STR_LIT>" : appstats_key , <EOL> } <EOL> def wrap ( self , app ) : <EOL> """<STR_LIT>""" <EOL> def wrapped_appstats_app ( environ , start_response ) : <EOL> if recording . recorder_proxy . has_recorder_for_current_request ( ) : <EOL> self . recorder = recording . recorder_proxy . get_for_current_request ( ) <EOL> return app ( environ , start_response ) <EOL> return recording . appstats_wsgi_middleware ( wrapped_appstats_app ) </s>
