Skip to content

gh-239: Add metadata["phase"]#241

Open
maurycy wants to merge 7 commits into
psf:mainfrom
maurycy:metadata-phase
Open

gh-239: Add metadata["phase"]#241
maurycy wants to merge 7 commits into
psf:mainfrom
maurycy:metadata-phase

Conversation

@maurycy
Copy link
Copy Markdown
Contributor

@maurycy maurycy commented May 27, 2026

Hooks today receive metadata but cannot tell which _compute_values just ran. The PR adds phase to the metadata.

phase when
("calibration", "warmup") calibrate_warmups(), including --recalibrate-warmups
("calibration", "loops") calibrate_loops(), including --recalibrate-loops
("measurement", "warmup") compute_warmups_values() warmup samples (skipped when --warmups 0)
("measurement", "value") compute_warmups_values() value samples

Recalibration is implicit and hooks can use metadata["recalibrate_warmups"] or metadata["recalibrate_loops"].

phase is a tuple, but after a JSON round-trip it is a list

As a bonus, benchmarks.runs.metadata[] is now explicit:

{
  "benchmarks": [
    {
      "runs": [
        {
          "metadata": {
            "calibrate_loops": 67108864,
            "date": "2026-05-27 23:54:30.678184",
            "duration": 0.7158581660478376,
            "mem_max_rss": 27230208,
            "phase": [
              "calibration",
              "loops"
            ],
            "uptime": 634803.6837940216
          },
          "warmups": [[1, 2.92e-07], "... (elided)", [67108864, 2.65e-09]]
        },
        {
          "metadata": {
            "date": "2026-05-27 23:54:31.261364",
            "duration": 0.536709374981001,
            "mem_max_rss": 27426816,
            "phase": [
              "measurement",
              "value"
            ],
            "uptime": 634804.2667930126
          },
          "values": [
            2.5949900301497353E-9,
            2.633662268304582E-9
          ],
          "warmups": [
            [
              67108864,
              2.6324198845045776E-9
            ]
          ]
        }
      ]
    }
  ],
  "metadata": {
    "boot_time": "2026-05-20 15:34:27",
    "...": "(removed for brevity)"
  },
  "version": "1.0"
}

Feature requested by gc-monitor in #239.

Please see #239 (comment) for a possible future direction in the hook API.

cr @sergey-miryanov @vstinner
resolve #239

@sergey-miryanov
Copy link
Copy Markdown
Contributor

Should we include a test case validating that phase indicators are correctly passed to the hook's teardown callback?

@maurycy
Copy link
Copy Markdown
Contributor Author

maurycy commented May 27, 2026

Should we include a test case validating that phase indicators are correctly passed to the hook's teardown callback?

@sergey-miryanov Good catch. What do you think?

https://github.com/psf/pyperf/pull/241/changes#diff-b62e5031e2825ba28b8458f67cbacc002970cbe1409fdd12f34bf86e295359f2R522-R562

Comment thread doc/api.rst
* ``unit``: Unit of values: ``byte``, ``integer`` or ``second``
* ``phase`` (tuple or list of two non-empty strings): run phase and sample
type, set to one of ``("calibration", "loops")``, ``("calibration",
"warmup")``, ``("measurement", "warmup")`` or ``("measurement", "value")``.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, the user will never see ("measurement", "warmup") in the output JSON. It would be good to note this. Or we could keep only the four states in the HookBase.teardown docs and note the difference.

for value in (('measurement', 'value'), ['calibration', 'loops']):
with self.subTest(value=value):
run = pyperf.Run([1.0], metadata={'phase': value},
collect_metadata=False)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we test for collect_metadata=True here?

@vstinner
Copy link
Copy Markdown
Member

I don't think that putting a phase in metadata makes sense: #239 (comment).

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.

Extend hook's teardown metadata with warmup/calibration flags

3 participants