could you please provide me a python script in order to analyze an .har file against…
- User
could you please provide me a python script in order to analyze an .har file against greenIT?
- Assistant
- User
yes could you please add more comprehensive analysis, you could:
- Analyze specific types of resources (e.g., images, scripts, CSS) for optimization opportunities.
- Assess the use of modern, efficient formats (e.g., WebP for images, AVIF).
- Evaluate the efficiency of the application’s API calls (e.g., batch requests, unnecessary data retrieval).
- Assistant
- User
could you please fix your “ and make it " to allow copy paste?
- Assistant
Certainly! Here's the revised version of the script with corrected quotation marks for easy copy-pasting:
import json def analyze_har_file(file_path): total_size = 0 total_requests = 0 resource_types = {} efficient_format_opportunities = [] with open(file_path, 'r') as har_file: har_data = json.load(har_file) entries = har_data['log']['entries'] for entry in entries: total_requests += 1 size = entry['response']['bodySize'] + entry['response']['headersSize'] total_size += size mime_type = entry['response']['content']['mimeType'].split(';')[0] url = entry['response']['_transferSize'] if '_transferSize' in entry['response'] else entry['request']['url'] if mime_type not in resource_types: resource_types[mime_type] = {'count': 0, 'size': 0} resource_types[mime_type]['count'] += 1 resource_types[mime_type]['size'] += size # Check for non-optimal formats if 'image/jpeg' in mime_type or 'image/png' in mime_type: efficient_format_opportunities.append(url) # Summary print(f"Total Requests: {total_requests}") print(f"Total Size: {total_size / 1024} KB") print("\nResource Breakdown (Type: Count, Size (KB)):") for type, data in resource_types.items(): print(f"{type}: {data['count']}, {data['size'] / 1024} KB") if efficient_format_opportunities: print("\nOpportunities to use more efficient formats for:") for url in efficient_format_opportunities: print(url) # Additional analyses could go here... if __name__ == "__main__": har_file_path = 'path/to/your/file.har' # Update this path analyze_har_file(har_file_path)