Skip to content

base: base class to collect any user-defined attribute in user class - #55

Merged
HaoZeke merged 4 commits into
airspeed-velocity:mainfrom
hominhquan:dynamic_attrs
Oct 16, 2026
Merged

HaoZeke merged 4 commits into
airspeed-velocity:mainfrom
hominhquan:dynamic_attrs

Conversation

@hominhquan

Copy link
Copy Markdown

To enable users to add any further custom attribute in their benchmark .py scripts, e.g.

def track_mem_bw(....)
   ...

track_mem_bw.unit = "MB/s"
track_mem_bw.higher_is_better = True

@hominhquan
hominhquan requested a review from HaoZeke as a code owner September 15, 2026 13:18
Signed-off-by: Minh Quan Ho <minh-quan.ho@sipearl.com>
hominhquan pushed a commit to hominhquan/asv that referenced this pull request Sep 15, 2026
- Depends on airspeed-velocity/asv_runner#55 (asv-runner >= 0.3.2)
- Closes airspeed-velocity#1617

Signed-off-by: Minh Quan Ho <minh-quan.ho@sipearl.com>
hominhquan pushed a commit to hominhquan/asv that referenced this pull request Sep 15, 2026
- Depends on airspeed-velocity/asv_runner#55 (asv-runner >= 0.3.2)
- Closes airspeed-velocity#1617

Signed-off-by: Minh Quan Ho <minh-quan.ho@sipearl.com>

@HaoZeke HaoZeke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The collect helper is defined at module scope then called as self._collect_dynamic_attrs(func). That is an AttributeError on every Benchmark(...). Hang it on the class, or call _collect_dynamic_attrs(self, func).

already_set = set(vars(self)) runs after self.unit = "unit", so func.unit is never copied. Track still gets unit from _get_first_attr. higher_is_better would copy once the call actually works.

vars(func) misses a class-level higher_is_better = True that attr_sources already walks. The 1617 example is on the function so it is fine; walk attr_sources if you want the class default.

Drop the French comment. Add a test that TrackBenchmark constructs and that higher_is_better lands on the instance (discovery dumps __dict__ bools into the JSON asv 1621 reads).

@hominhquan

Copy link
Copy Markdown
Author
  • ok to move _collect_dynamic_attrs inside the Benchmark class, and will add a test.

already_set = set(vars(self)) runs after self.unit = "unit", so func.unit is never copied. Track still gets unit from _get_first_attr.

Can you develop if this comment describes the behavior before (or after) this PR ? And what it should be like ?
Anyway, the current logic is func.unit is still copied in track.py, after Benchmark.__init__(), and this PR does not change it.

higher_is_better would copy once the call actually works

I'm not sure to understand which call you are mentioning. The goal of this PR is not to add an explicit new higher_is_better, but to accept any arbitrary user-defined attribute in their benchmark script. Detecting (and not limited to) higher_is_better is then a result of this PR.

vars(func) misses a class-level higher_is_better = True that attr_sources already walks. The 1617 example is on the function so it is fine; walk attr_sources if you want the class default.

I don't think a class-level higher_is_better makes sense. A class can have multiple track_*() functions. They can track time or latency (lower-is-better metrics) or memory bandwidth and mflops (higher-is-better metrics). Supporting per-function dynamic attribute seems to be a more flexible way IMO.

@hominhquan

Copy link
Copy Markdown
Author

@HaoZeke can you give a look at the updated branch pls ? and tell me if it addressed your raised concerns.

@HaoZeke HaoZeke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the constructor works now and the new tests pass (4 new, 30 in the suite). Two things left.

ruff format --check fails on asv_runner/benchmarks/_base.py: the blank line above class Benchmark is gone, and the new if is over the line length with single quotes.

already_set only has instance attributes, so a plain value on the function can shadow a method on the class:

class Bench:
    def track_x(self):
        return 1

    track_x.run = "oops"

With that, TrackBenchmark("track_x", b.track_x, [b.track_x, b]).run is the string, and .run() raises TypeError: 'str' object is not callable. Skipping anything type(self) already has fixes it:

already_set = set(vars(self))
for attr_name, value in getattr(func, "__dict__", {}).items():
    if (
        attr_name.startswith("_")
        or attr_name in already_set
        or hasattr(type(self), attr_name)
        or callable(value)
    ):
        continue
    setattr(self, attr_name, value)

getattr(func, "__dict__", {}) also covers callables without a __dict__, which vars() raises on. With that change, a track_mem_bw.run = "not a method" line in the dummy, and a test that self.benchmark.run is still callable, the suite passes (31) and ruff format --check is clean.

Per-function attributes over a class default is fine by me, that matches #1617.

Comment thread asv_runner/benchmarks/_base.py Resolved
Comment thread asv_runner/benchmarks/_base.py Resolved
Comment thread asv_runner/benchmarks/_base.py Resolved
@hominhquan

Copy link
Copy Markdown
Author

Thanks @HaoZeke , I pushed a new commit including your suggestion.

@HaoZeke HaoZeke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the Oct 12 commit covers the shadowing case from the last review. hasattr(type(self), attr_name) and getattr(func, "__dict__", {}) are in place, and the existing tests still pass the points I care about (construct, copy user attrs, leave unit alone, skip callables and privates).

One small follow-up if you have a moment: add track_mem_bw.run = "not a method" on the dummy and assert self.benchmark.run is still callable. That is the exact case that used to break, and it is not in test_dynamic_attrs.py yet. Happy to merge without it if you prefer a separate PR.

@hominhquan

hominhquan commented Oct 14, 2026 •

Copy link
Copy Markdown
Author

@HaoZeke I pushed a a new test on track_mem_bw.run not interfering to the class run(). Thanks for reminding and feel free to merge.

I also expect a new tag asv-runner-0.3.2 released, so airspeed-velocity/asv#1621 can be unblocked, too. Thanks.

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Documentation preview for this PR: documentation.zip. Unzip and open index.html in a browser (Sphinx HTML under the zip root).

@HaoZeke
HaoZeke merged commit 8ec5064 into airspeed-velocity:main Oct 16, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants