#!/usr/bin/env python3

import numpy as np

# Open cachegrind.csv

with open('cachegrind.csv', 'r') as f:
    lines = f.readlines()
# Skip the first 2 lines (header)
data_lines = lines[2:]  
# Parse the data
data = []
for line in data_lines:
    parts = line.strip().split(', ')
    if len(parts) == 5:
        func_name = parts[0]
        calls = int(parts[1])
        self_time = float(parts[2].replace(' ', ''))
        total_time = float(parts[3].replace(' ', ''))
        path = parts[4]
        data.append((func_name, calls, self_time, total_time, path))

php_time = sum(row[2] for row in data if row[4] == 'php:internal')
print(f"Total PHP Time: {php_time:.2f} ms")
vendor_time = sum(row[2] for row in data if "/vendor/" in row[4])
print(f"Total Vendor Time: {vendor_time:.2f} ms")
top_script = sorted((row for row in data if row[4] != 'php:internal' and '/vendor/' not in row[4]), key=lambda x: x[2], reverse=True)[:10]
for func_name, calls, self_time, total_time, path in top_script:
    print(f"\t{func_name}, {calls}, {self_time:.2f} ms, {total_time:.2f} ms, {path}")
#print("Function Name, Calls, Self Time (ms), Time (ms), Path")
for func_name, calls, self_time, total_time, path in data:
    pass
    #print(f"{func_name}, {calls}, {self_time:.2f}, {total_time:.2f}, {path}")