import pickle
import pandas as pd


def get_average_active_secs(df, repeat=1):
    mask = (df > 0.005) & (df < 0.020)
    active = df[mask]
    secs = len(active) / 1000
    return secs / repeat


def analyze_benchmark(benchmark):
    with open(f"output/test_3_{benchmark}_ckpt.pickle", "rb") as f:
        d = pickle.load(f)
    
    measured_init_times = []

    for messages in d["debug_outputs"]:
        for message in messages:
            if message.startswith("cycle"):
                cycle = message.split()[1]
                cycle = int(cycle)
                freq = 10 * 1000 * 1000
                time = cycle / freq
                measured_init_times.append(time)
    
    return measured_init_times[0]


def do_analysis(benchmarks):
    results = []
    for benchmark in benchmarks:
        try:
            init_time = analyze_benchmark(benchmark)
        except:
            continue
        results.append({
            "benchmark": benchmark,
            "init time (s)": init_time,
        })
    d = pd.DataFrame(results)

    avg_init_time = d["init time (s)"].mean()

    print(d)
    print("")
    print(f"average init time: {avg_init_time:.5f} s")


def main():
    benchmarks = ["vFFT", "vConv2d", "vCrc", "vBasicMath", "vSha", "vStringSearch", "vAes", "vMatMul"]
    benchmarks = ["vFFT", "vCrc", "vAes", "vSha"]
    do_analysis(benchmarks)


if __name__ == "__main__":
    main()