From bf9585431c75c5fd4cf621b9dbe9371aa2b6ea4d Mon Sep 17 00:00:00 2001 From: Fabian Liebig Date: Wed, 5 Feb 2025 18:54:37 +0100 Subject: [PATCH] Add Python environment and version metadata to benchmark results --- benchmarks/result/result.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/benchmarks/result/result.py b/benchmarks/result/result.py index 842052c290..8713876455 100644 --- a/benchmarks/result/result.py +++ b/benchmarks/result/result.py @@ -2,8 +2,11 @@ from __future__ import annotations +import sys + +import importlib_metadata from attrs import define, field -from attrs.validators import instance_of +from attrs.validators import deep_mapping, instance_of from pandas import DataFrame from benchmarks.result import ResultMetadata @@ -22,3 +25,19 @@ class Result(BenchmarkSerialization): metadata: ResultMetadata = field(validator=instance_of(ResultMetadata)) """The metadata associated with the benchmark result.""" + + python_env: dict[str, str] = field( + validator=deep_mapping(instance_of(str), instance_of(str), instance_of(dict)) + ) + """The Python environment in which the benchmark was executed.""" + + python_version: str = field(default=sys.version, validator=instance_of(str)) + """The Python version in which the benchmark was executed.""" + + @python_env.default + def _default_python_env(self) -> dict[str, str]: + installed_packages = importlib_metadata.distributions() + package_dict = { + dist.metadata["Name"]: dist.version for dist in installed_packages + } + return package_dict