| 1 |
"""Some simple performance benchmarks for beets.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import cProfile |
| 6 |
import timeit |
| 7 |
from typing import TYPE_CHECKING, Protocol |
| 8 |
|
| 9 |
from beets import importer, plugins, ui |
| 10 |
from beets.autotag import Source, tag_album |
| 11 |
from beets.plugins import BeetsPlugin |
| 12 |
from beets.util.pathformats import PF_KEY_DEFAULT |
| 13 |
from beetsplug._utils import vfs |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from collections.abc import Sequence |
| 17 |
|
| 18 |
from beets.library import Item, Library |
| 19 |
|
| 20 |
|
| 21 |
class BenchAunique(Protocol): |
| 22 |
profile: bool |
| 23 |
|
| 24 |
|
| 25 |
class BenchMatch(Protocol): |
| 26 |
profile: bool |
| 27 |
id: str | None |
| 28 |
|
| 29 |
|
| 30 |
def aunique_benchmark( |
| 31 |
lib: Library, opts: BenchAunique, args: list[str] |
| 32 |
) -> None: |
| 33 |
def _build_tree() -> None: |
| 34 |
vfs.libtree(lib) |
| 35 |
|
| 36 |
# Measure path generation performance with %aunique{} included. |
| 37 |
lib.path_formats = [ |
| 38 |
(PF_KEY_DEFAULT, "$albumartist/$album%aunique{}/$track $title") |
| 39 |
] |
| 40 |
if opts.profile: |
| 41 |
cProfile.runctx( |
| 42 |
"_build_tree()", |
| 43 |
{}, |
| 44 |
{"_build_tree": _build_tree}, |
| 45 |
"paths.withaunique.prof", |
| 46 |
) |
| 47 |
else: |
| 48 |
interval = timeit.timeit(_build_tree, number=1) |
| 49 |
print("With %aunique:", interval) |
| 50 |
|
| 51 |
# And with %aunique replaced with a "cheap" no-op function. |
| 52 |
lib.path_formats = [ |
| 53 |
(PF_KEY_DEFAULT, "$albumartist/$album%lower{}/$track $title") |
| 54 |
] |
| 55 |
if opts.profile: |
| 56 |
cProfile.runctx( |
| 57 |
"_build_tree()", |
| 58 |
{}, |
| 59 |
{"_build_tree": _build_tree}, |
| 60 |
"paths.withoutaunique.prof", |
| 61 |
) |
| 62 |
else: |
| 63 |
interval = timeit.timeit(_build_tree, number=1) |
| 64 |
print("Without %aunique:", interval) |
| 65 |
|
| 66 |
|
| 67 |
def match_benchmark(lib: Library, opts: BenchMatch, args: list[str]) -> None: |
| 68 |
# If no album ID is provided, we'll match against a suitably huge |
| 69 |
# album. |
| 70 |
id_ = opts.id or "9c5c043e-bc69-4edb-81a4-1aaf9c81e6dc" |
| 71 |
|
| 72 |
# Get an album from the library to use as the source for the match. |
| 73 |
items: Sequence[Item] = i.items() if (i := lib.albums(args).get()) else [] |
| 74 |
|
| 75 |
# Ensure fingerprinting is invoked (if enabled). |
| 76 |
plugins.send( |
| 77 |
"import_task_start", |
| 78 |
task=importer.ImportTask(None, None, items), |
| 79 |
session=importer.ImportSession(lib, None, None, None), |
| 80 |
) |
| 81 |
|
| 82 |
# Run the match. |
| 83 |
def _run_match() -> None: |
| 84 |
source = Source.from_items(items) |
| 85 |
tag_album(source, search_ids=[id_]) |
| 86 |
|
| 87 |
if opts.profile: |
| 88 |
cProfile.runctx( |
| 89 |
"_run_match()", {}, {"_run_match": _run_match}, "match.prof" |
| 90 |
) |
| 91 |
else: |
| 92 |
interval = timeit.timeit(_run_match, number=1) |
| 93 |
print("match duration:", interval) |
| 94 |
|
| 95 |
|
| 96 |
class BenchmarkPlugin(BeetsPlugin): |
| 97 |
"""A plugin for performing some simple performance benchmarks.""" |
| 98 |
|
| 99 |
def commands(self) -> list[ui.Subcommand]: |
| 100 |
aunique_bench_cmd = ui.Subcommand( |
| 101 |
"bench_aunique", help="benchmark for %aunique{}" |
| 102 |
) |
| 103 |
aunique_bench_cmd.parser.add_option( |
| 104 |
"-p", |
| 105 |
"--profile", |
| 106 |
action="store_true", |
| 107 |
default=False, |
| 108 |
help="performance profiling", |
| 109 |
) |
| 110 |
aunique_bench_cmd.func = aunique_benchmark |
| 111 |
|
| 112 |
match_bench_cmd = ui.Subcommand( |
| 113 |
"bench_match", help="benchmark for track matching" |
| 114 |
) |
| 115 |
match_bench_cmd.parser.add_option( |
| 116 |
"-p", |
| 117 |
"--profile", |
| 118 |
action="store_true", |
| 119 |
default=False, |
| 120 |
help="performance profiling", |
| 121 |
) |
| 122 |
match_bench_cmd.parser.add_option( |
| 123 |
"-i", "--id", default=None, help="album ID to match against" |
| 124 |
) |
| 125 |
match_bench_cmd.func = match_benchmark |
| 126 |
|
| 127 |
return [aunique_bench_cmd, match_bench_cmd] |