#NEWLINE# def merge_by_average( hars ) : #NEWLINE# #INDENT# '\n  Returns a har file representing the average of multiple har files, each reprenting the same page request. The har\n  files must represent loads of the same page for the result to make any sense. Page load times and resource load times\n  are taken from the median result to ensure that an HTTP waterfall graph makes sense.\n  ' #NEWLINE# logs = map ( ( lambda har : har [ 'log' ] ) , hars ) #NEWLINE# os_info_first = logs [ 0 ] [ '_os' ] #NEWLINE# browser_info_first = logs [ 0 ] [ 'browser' ] #NEWLINE# creator_first = avg_har [ 0 ] [ 'creator' ] #NEWLINE# assert all ( ( ( ( log [ '_os' ] == os_info_first ) and ( log [ 'browser' ] == browser_info_first ) and ( log [ 'creator' ] == creator_first ) ) for log in logs ) ) #NEWLINE# avg_har = { #NEWLINE# #INDENT# 'log' : { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# avg_log = avg_har [ 'log' ] #NEWLINE# avg_log [ '_os' ] = os_info_first #NEWLINE# avg_log [ 'creator' ] = creator_first #NEWLINE# avg_log [ 'browser' ] = browser_info_first #NEWLINE# avg_log [ 'version' ] = logs [ 0 ] [ 'version' ] #NEWLINE# sorted ( logs , key = ( lambda log : log [ 'pages' ] [ 0 ] [ 'pageTimings' ] [ 'onLoad' ] ) ) #NEWLINE# median_onload_log = logs [ ( len ( logs ) / 2 ) ] #NEWLINE# avg_log [ 'entries' ] = median_onload_log [ 'entries' ] #NEWLINE# avg_log [ 'pages' ] = [ ] #NEWLINE# avg_log [ 'pages' ] . append ( { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# avg_log [ 'pages' ] [ 0 ] [ '_cssStats' ] = avg_css_stats ( [ log [ 'pages' ] [ 0 ] [ '_cssStats' ] for log in logs ] ) #NEWLINE# avg_log [ 'pages' ] [ 0 ] [ '_memoryStats' ] = avg_memory_stats ( [ log [ 'pages' ] [ 0 ] [ '_memoryStats' ] for log in logs ] ) #NEWLINE# avg_log [ 'pages' ] [ 0 ] [ '_eventStats' ] = avg_event_stats ( [ log [ 'pages' ] [ 0 ] [ '_eventStats' ] for log in logs ] ) #NEWLINE# avg_log [ 'pages' ] [ 0 ] [ '_domNodeStats' ] = median_onload_log [ 'pages' ] [ 0 ] [ '_domNodeStats' ] #NEWLINE# avg_log [ 'pages' ] [ 0 ] [ 'pageTimings' ] = median_onload_log [ 'pages' ] [ 0 ] [ 'pageTimings' ] #NEWLINE# for ( field , value ) in median_onload_log [ 'pages' ] [ 0 ] . iteritems ( ) : #NEWLINE# #INDENT# if ( ( type ( value ) == type ( '' ) ) or ( type ( value ) == type ( '' ) ) ) : #NEWLINE# #INDENT# avg_log [ 'pages' ] [ 0 ] [ field ] = value #NEWLINE# #UNINDENT# #UNINDENT# return avg_har