# Little script to change the working directory if the module is executed from this file.

if __name__ == "__main__" :
    from os import chdir
    from os import getcwd
    from os.path import abspath
    from os.path import dirname
    from sys import path
    chdir(dirname(abspath(__file__)))
    chdir( ".." )
    path.append(getcwd())

# This script is used to generate the benchmark for the route function. You can replace the strings 
# and change the function if you need to benchmark something else.

from functions import route
from LazyFrame import LazyFrame
from pathlib import Path
from time import time

directory = "/home/mkel/Documents/FAC/M1/TPE/ais-operations/ressources/BD-AIS/FichiersCSV-Exemples/"
results = dict()
benchmark = open("/home/mkel/Documents/FAC/M1/TPE/ais-operations/resources/data/benchmark.dat", "w")

# For each file.
for file in Path(directory).iterdir():
    totalTime = 0

    # Do it 10 times to have an average.
    for i in range(10):
        lazyFrame = LazyFrame(str(file))
        startTime = time()
        lazyFrame = route(lazyFrame)
        lazyFrame.save()
        endTime = time()
        totalTime += endTime - startTime
    results[file.stat().st_size] = totalTime / 10

# Sort the results by size of the file.
for key, value in sorted(results.items()):
    benchmark.write(str(key) + " " + str(value) + "\n")
benchmark.close()