Repository navigation
Expand file tree
/
Copy pathmetadata.py
More file actions
141 lines (116 loc) · 4.93 KB
/
Copy pathmetadata.py
File metadata and controls
141 lines (116 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from __future__ import annotations
import importlib
import inspect
import json
import pkgutil
from collections.abc import Iterable, Iterator
from pathlib import Path
from typing import Any
import saps.benchmarks
from saps.benchmark import Benchmark
SUITE_TAG_PREFIX = "suite-"
def _record_key(record: dict[str, Any]) -> str:
return record["name"]
def _benchmark_instances() -> Iterator[Benchmark]:
for module_info in pkgutil.iter_modules(saps.benchmarks.__path__):
module = importlib.import_module(f"saps.benchmarks.{module_info.name}")
for _, cls in inspect.getmembers(module, inspect.isclass):
if cls.__module__ != module.__name__:
continue
if (
issubclass(cls, Benchmark)
and cls is not Benchmark
and not inspect.isabstract(cls)
):
yield cls()
def _statistics_records(
statistics_paths: Iterable[Path],
) -> dict[tuple[str, ...], list[dict[str, Any]]]:
records: dict[tuple[str, ...], list[dict[str, Any]]] = {}
for statistics_path in statistics_paths:
document = json.loads(statistics_path.read_text(encoding="utf-8"))
for benchmark in document.get("benchmarks", []):
benchmark_key = (benchmark["name"],)
records.setdefault(benchmark_key, []).append(benchmark)
for generator in benchmark.get("generators", []):
generator_key = (*benchmark_key, generator["name"])
records.setdefault(generator_key, []).append(generator)
for dataset in generator.get("datasets", []):
dataset_key = (*generator_key, dataset["name"])
records.setdefault(dataset_key, []).append(dataset)
return records
def _statistics_tags(
records: dict[tuple[str, ...], list[dict[str, Any]]],
key: tuple[str, ...],
*,
freshness: str | None = None,
) -> set[str]:
tags: set[str] = set()
for record in records.get(key, []):
if freshness is not None and record.get("freshness") not in (None, freshness):
continue
tags.update(record.get("statistics", []))
return tags
def _benchmark_statistics_tags(
records: dict[tuple[str, ...], list[dict[str, Any]]],
benchmark: dict[str, Any],
) -> set[str]:
benchmark_key = (benchmark["name"],)
tags = _statistics_tags(records, benchmark_key)
for generator in benchmark["generators"]:
generator_key = (*benchmark_key, generator["name"])
tags |= _statistics_tags(records, generator_key)
for dataset in generator["datasets"]:
tags |= _statistics_tags(
records,
(*generator_key, dataset["name"]),
freshness=dataset["freshness"],
)
return tags
def _record_tags(record: dict[str, Any], inherited: Iterable[str] = ()) -> list[str]:
return sorted(
{
*inherited,
*(f"{SUITE_TAG_PREFIX}{suite}" for suite in record.get("suites", [])),
*record.get("topics", []),
}
)
def metadata_document(statistics_paths: Iterable[Path] = ()) -> dict[str, Any]:
statistics = _statistics_records(statistics_paths)
records: dict[str, dict[str, Any]] = {}
for benchmark in _benchmark_instances():
record = benchmark.metadata
benchmark_name = record["name"]
benchmark_class = type(benchmark)
asv_module = benchmark_class.__module__.removeprefix("saps.benchmarks.")
record["asv_ids"] = {
metric: f"{asv_module}.{benchmark_class.__name__}.{metric}_{benchmark_name}"
for metric in ("peakmem", "time")
}
source_generators = {
generator.name: generator for generator in benchmark.generators
}
# Statistics tags describe the benchmark as a whole, so every fresh
# trace of any of its datasets tags the benchmark and all its children.
record["tags"] = _record_tags(
record, _benchmark_statistics_tags(statistics, record)
)
for generator in record["generators"]:
generator_name = generator["name"]
generator["cacheable"] = source_generators[generator_name].cacheable
generator["tags"] = _record_tags(generator, record["tags"])
for dataset in generator["datasets"]:
dataset["asv_param"] = f"{generator_name}.{dataset['name']}"
dataset["tags"] = _record_tags(dataset, generator["tags"])
records.setdefault(_record_key(record), record)
document = {"benchmarks": sorted(records.values(), key=_record_key)}
return json.loads(json.dumps(document, sort_keys=True))
def write_metadata_document(
metadata_path: Path, statistics_paths: Iterable[Path] = ()
) -> dict[str, Any]:
document = metadata_document(statistics_paths)
metadata_path.write_text(
json.dumps(document, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
return document