Skip to content

feat: Add ScopedPauseTiming RAII helper#2157

Open
dmah42 wants to merge 6 commits intomainfrom
feature/scoped-pause-guard
Open

feat: Add ScopedPauseTiming RAII helper#2157
dmah42 wants to merge 6 commits intomainfrom
feature/scoped-pause-guard

Conversation

@dmah42
Copy link
Copy Markdown
Member

@dmah42 dmah42 commented Mar 30, 2026

Adds a new benchmark::ScopedPauseTiming class that provides a convenient RAII-style mechanism for pausing and resuming benchmark timers.

This is less error-prone than manually calling PauseTiming and ResumeTiming, as it guarantees that the timer is resumed when the scope is exited.

  • Added ScopedPauseTiming to include/benchmark/state.h.
  • Added a new test test/scoped_pause_test.cc to verify the functionality and prevent regressions.
  • Updated test/CMakeLists.txt to include the new test.
  • Added documentation for the new feature in docs/user_guide.md.

Adds a new `benchmark::ScopedPauseTiming` class that provides a convenient
RAII-style mechanism for pausing and resuming benchmark timers.

This is less error-prone than manually calling `PauseTiming` and
`ResumeTiming`, as it guarantees that the timer is resumed when the
scope is exited.

- Added `ScopedPauseTiming` to `include/benchmark/state.h`.
- Added a new test `test/scoped_pause_test.cc` to verify the
  functionality and prevent regressions.
- Updated `test/CMakeLists.txt` to include the new test.
- Added documentation for the new feature in `docs/user_guide.md`.
@dmah42 dmah42 force-pushed the feature/scoped-pause-guard branch from 5ba27e4 to d5065cf Compare March 30, 2026 12:59
@dmah42 dmah42 requested a review from LebedevRI March 30, 2026 13:11
}
~ScopedPauseTiming() { state_.ResumeTiming(); }

private:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think, =deleted members should be public, but it probably doesn't really matter.
You might also want to deal with move ctor/assign?

ScopedPauseTiming(ScopedPauseTiming&&) = delete;
void operator=(ScopedPauseTiming&&) = delete;

dependabot bot and others added 5 commits April 1, 2026 09:14
Bumps [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) from 7.6.0 to 8.0.0.
- [Release notes](https://github.com/astral-sh/setup-uv/releases)
- [Commits](astral-sh/setup-uv@37802ad...cec2083)

---
updated-dependencies:
- dependency-name: astral-sh/setup-uv
  dependency-version: 8.0.0
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [lukka/get-cmake](https://github.com/lukka/get-cmake) from 4.3.0 to 4.3.1.
- [Release notes](https://github.com/lukka/get-cmake/releases)
- [Changelog](https://github.com/lukka/get-cmake/blob/main/RELEASE_PROCESS.md)
- [Commits](lukka/get-cmake@b783061...ea83089)

---
updated-dependencies:
- dependency-name: lukka/get-cmake
  dependency-version: 4.3.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: dominic <510002+dmah42@users.noreply.github.com>
Bumps [numpy](https://github.com/numpy/numpy) from 2.4.3 to 2.4.4.
- [Release notes](https://github.com/numpy/numpy/releases)
- [Changelog](https://github.com/numpy/numpy/blob/main/doc/RELEASE_WALKTHROUGH.rst)
- [Commits](numpy/numpy@v2.4.3...v2.4.4)

---
updated-dependencies:
- dependency-name: numpy
  dependency-version: 2.4.4
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: dominic <510002+dmah42@users.noreply.github.com>

// BM_ScopedPause sleeps for 10ms in a ScopedPauseTiming block.
// The reported time should be much less than 10ms.
void BM_ScopedPause(benchmark::State& state) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd strongly suggest to use ManualTime for this, not rely on the system timers doing the right thing.

Copy link
Copy Markdown
Collaborator

@LebedevRI LebedevRI left a comment

Choose a reason for hiding this comment

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

Seems fine to me, thank you!

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