Files
super-app/.github/actions/macrobenchmark/analyse.py
2025-01-13 06:11:29 +00:00

135 lines
4.4 KiB
Python

import json
import requests
import sys
def find_benchmark_by_name(benchmarks, name_substring):
for benchmark in benchmarks:
if name_substring.lower() in benchmark['name'].lower():
return benchmark
return None
def extract_metrics(benchmark_file):
try:
benchmarks = benchmark_file['benchmarks']
startup_benchmark = find_benchmark_by_name(benchmarks, 'startup')
homepage_benchmark = find_benchmark_by_name(benchmarks, 'scroll')
if not startup_benchmark:
raise ValueError("Startup benchmark not found")
if not homepage_benchmark:
raise ValueError("Homepage scroll benchmark not found")
startup_metrics = startup_benchmark['metrics']['timeToInitialDisplayMs']
min_value = startup_metrics['minimum']
median_value = startup_metrics['median']
max_value = startup_metrics['maximum']
homepage_metrics = homepage_benchmark['sampledMetrics']['frameDurationCpuMs']
p50_value = homepage_metrics['P50']
p90_value = homepage_metrics['P90']
p95_value = homepage_metrics['P95']
p99_value = homepage_metrics['P99']
device_model = benchmark_file['context']['build']['model']
return {
'device_model': device_model,
'min_value': min_value,
'median_value': median_value,
'max_value': max_value,
'p50_value': p50_value,
'p90_value': p90_value,
'p95_value': p95_value,
'p99_value': p99_value
}
except KeyError as e:
raise ValueError(f"Failed to find key in benchmark data: {str(e)}")
except Exception as e:
raise ValueError(f"Error processing benchmark data: {str(e)}")
def send_message_to_metabase(device_model, version_code, run_id, min, median, max, homepage_p50,
homepage_p90, homepage_p95, homepage_p99):
url = "https://janus.prod.navi-tech.in/events/json"
headers = {
"Content-Type": "application/json"
}
data = {
"events": [
{
"event_name": "NaviApp_Startup_Benchmark",
"attributes": {
"min": round(min),
"median": round(median),
"max": round(max),
"run_id": run_id,
"version_code": version_code,
"device_model": device_model,
"source": "android"
}
},
{
"event_name": "NaviApp_HomePage_Scroll_Benchmark",
"attributes": {
"p50_frame_duration": round(homepage_p50),
"p90_frame_duration": round(homepage_p90),
"p95_frame_duration": round(homepage_p95),
"p99_frame_duration": round(homepage_p99),
"run_id": run_id,
"version_code": version_code,
"device_model": device_model,
"source": "android"
}
}
],
"source": "Android-Macrobenchmark"
}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
raise Exception(
f"Request to Metabase API failed with status code {response.status_code}: {response.text}")
else:
print("Message posted successfully.")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: script.py <run_id> <version_code>")
sys.exit(1)
run_id = sys.argv[1]
version_code = sys.argv[2]
try:
with open('macrobenchmark_results.json', 'r') as file_current:
benchmark_file = json.load(file_current)
metrics = extract_metrics(benchmark_file)
send_message_to_metabase(
metrics['device_model'],
version_code,
run_id,
metrics['min_value'],
metrics['median_value'],
metrics['max_value'],
metrics['p50_value'],
metrics['p90_value'],
metrics['p95_value'],
metrics['p99_value']
)
except FileNotFoundError:
print("Error: macrobenchmark_results.json file not found")
sys.exit(1)
except json.JSONDecodeError:
print("Error: Invalid JSON in macrobenchmark_results.json")
sys.exit(1)
except Exception as e:
print(f"Error: {str(e)}")
sys.exit(1)