49 lines
1.4 KiB
Python
Executable File
49 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import sqlite3
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from beets.library import Library
|
|
|
|
# Configure your db path
|
|
db_path = Path("./musiclibrary.db")
|
|
idx_name = "items_album_id_idx"
|
|
|
|
|
|
def run_sql_exc(path, query):
|
|
conn = sqlite3.connect(path)
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
|
|
def run_benchmark(lib, indexed=True):
|
|
time_start = time.perf_counter()
|
|
albums = lib.albums()
|
|
items = []
|
|
for album in albums:
|
|
items.extend(album.items())
|
|
duration = time.perf_counter() - time_start
|
|
nA = len(albums)
|
|
nI = len(items)
|
|
|
|
print(
|
|
f"Benchmark with{'out' if indexed else None} index took {duration:.2f} seconds"
|
|
)
|
|
print(f"\tnAlbums: {nA} (avgtime {duration * 1000 / nA} ms per matchquery)")
|
|
print(f"\tnItems: {nI} (avgtime {duration * 1000 / nI:.2f} ms per item)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_sql_exc(db_path, 'DROP INDEX IF EXISTS "{}"'.format(idx_name))
|
|
print("Benchmark without index...")
|
|
run_benchmark(Library(db_path.name, str(db_path.resolve().parent)))
|
|
print("Creating index on items.album_id...")
|
|
run_sql_exc(
|
|
db_path, 'CREATE INDEX IF NOT EXISTS "{}" ON items (album_id)'.format(idx_name)
|
|
)
|
|
print("Benchmark with index...")
|
|
run_benchmark(Library(db_path.name, str(db_path.resolve().parent)))
|
|
print("Done.")
|