cb66c5d68e
build-and-test / ubuntu-24.04-arm.Debug.static.clang++ (push) Waiting to run
build-and-test / macos-latest.Debug.shared.g++ (push) Waiting to run
build-and-test / macos-latest.Debug.static.g++ (push) Waiting to run
build-and-test / ubuntu-22.04.Debug.static.g++ (push) Waiting to run
build-and-test / ubuntu-24.04.Debug.static.g++ (push) Waiting to run
bazel / bazel.macos-latest (push) Waiting to run
bazel / bazel.windows-latest (push) Waiting to run
build-and-test-min-cmake / macos-latest.min-cmake (push) Waiting to run
build-and-test / macos-latest.Debug.shared.clang++ (push) Waiting to run
build-and-test / ubuntu-22.04.Debug.shared.clang++ (push) Waiting to run
build-and-test / ubuntu-24.04.Debug.shared.clang++ (push) Waiting to run
build-and-test / ubuntu-24.04-arm.Debug.shared.clang++ (push) Waiting to run
build-and-test / macos-latest.Debug.static.clang++ (push) Waiting to run
build-and-test / ubuntu-22.04.Debug.static.clang++ (push) Waiting to run
build-and-test / ubuntu-24.04.Debug.static.clang++ (push) Waiting to run
build-and-test / ubuntu-22.04.Debug.shared.g++ (push) Waiting to run
build-and-test / ubuntu-24.04.Debug.shared.g++ (push) Waiting to run
build-and-test / ubuntu-24.04-arm.Debug.shared.g++ (push) Waiting to run
build-and-test / ubuntu-24.04-arm.Debug.static.g++ (push) Waiting to run
build-and-test / macos-latest.Release.shared.clang++ (push) Waiting to run
build-and-test / ubuntu-22.04.Release.shared.clang++ (push) Waiting to run
build-and-test / ubuntu-24.04.Release.shared.clang++ (push) Waiting to run
build-and-test / ubuntu-24.04-arm.Release.shared.clang++ (push) Waiting to run
build-and-test / macos-latest.Release.static.clang++ (push) Waiting to run
build-and-test / ubuntu-22.04.Release.static.clang++ (push) Waiting to run
build-and-test / ubuntu-24.04.Release.static.clang++ (push) Waiting to run
build-and-test / ubuntu-24.04-arm.Release.static.clang++ (push) Waiting to run
build-and-test / macos-latest.Release.shared.g++ (push) Waiting to run
build-and-test / ubuntu-22.04.Release.shared.g++ (push) Waiting to run
build-and-test / ubuntu-24.04.Release.shared.g++ (push) Waiting to run
build-and-test / ubuntu-24.04-arm.Release.shared.g++ (push) Waiting to run
build-and-test / macos-latest.Release.static.g++ (push) Waiting to run
build-and-test / ubuntu-22.04.Release.static.g++ (push) Waiting to run
build-and-test / ubuntu-24.04.Release.static.g++ (push) Waiting to run
build-and-test / ubuntu-24.04-arm.Release.static.g++ (push) Waiting to run
build-and-test / windows-2022.Debug.shared.VS-17-2022 (push) Waiting to run
build-and-test / windows-2022.Debug.static.VS-17-2022 (push) Waiting to run
build-and-test / windows-2025.Debug.shared.VS-18-2025 (push) Waiting to run
build-and-test / windows-2025.Debug.static.VS-18-2025 (push) Waiting to run
build-and-test / windows-2022.Release.shared.VS-17-2022 (push) Waiting to run
build-and-test / windows-2022.Release.static.VS-17-2022 (push) Waiting to run
build-and-test / windows-2025.Release.shared.VS-18-2025 (push) Waiting to run
build-and-test / windows-2025.Release.static.VS-18-2025 (push) Waiting to run
build-and-test / windows-latest.Debug.shared.CLANG64 (push) Waiting to run
build-and-test / windows-latest.Debug.shared.MINGW64 (push) Waiting to run
build-and-test / windows-latest.Debug.shared.UCRT64 (push) Waiting to run
build-and-test / windows-latest.Debug.static.CLANG64 (push) Waiting to run
build-and-test / windows-latest.Debug.static.MINGW64 (push) Waiting to run
build-and-test / windows-latest.Debug.static.UCRT64 (push) Waiting to run
build-and-test / windows-latest.Release.shared.CLANG64 (push) Waiting to run
build-and-test / windows-latest.Release.shared.MINGW64 (push) Waiting to run
build-and-test / windows-latest.Release.shared.UCRT64 (push) Waiting to run
build-and-test / windows-latest.Release.static.CLANG64 (push) Waiting to run
build-and-test / windows-latest.Release.static.MINGW64 (push) Waiting to run
build-and-test / windows-latest.Release.static.UCRT64 (push) Waiting to run
test-bindings / Test Python 3.10 bindings on macos-latest (push) Waiting to run
test-bindings / Test Python 3.11 bindings on macos-latest (push) Waiting to run
test-bindings / Test Python 3.12 bindings on macos-latest (push) Waiting to run
test-bindings / Test Python 3.13 bindings on macos-latest (push) Waiting to run
test-bindings / Test Python 3.10 bindings on windows-latest (push) Waiting to run
test-bindings / Test Python 3.11 bindings on windows-latest (push) Waiting to run
test-bindings / Test Python 3.12 bindings on windows-latest (push) Waiting to run
test-bindings / Test Python 3.13 bindings on windows-latest (push) Waiting to run
test-bindings / Test Rust bindings on macos-latest (push) Waiting to run
test-bindings / Test Rust bindings on windows-latest (push) Waiting to run
bazel / bazel.ubuntu-latest (push) Failing after 1s
build-and-test-perfcounters / ubuntu-latest.Debug (push) Failing after 0s
build-and-test-perfcounters / ubuntu-latest.Release (push) Failing after 1s
doxygen / Build HTML documentation (push) Failing after 1s
python + Bazel pre-commit checks / pre-commit (push) Failing after 1s
sanitizer / tsan.Debug (push) Failing after 1s
sanitizer / ubsan.Debug (push) Failing after 0s
sanitizer / ubsan.RelWithDebInfo (push) Failing after 1s
build-and-test-min-cmake / ubuntu-latest.min-cmake (push) Failing after 1s
clang-format-lint / check-clang-format (push) Failing after 1s
sanitizer / asan.Debug (push) Failing after 1s
sanitizer / msan.RelWithDebInfo (push) Failing after 1s
clang-tidy / run-clang-tidy (push) Failing after 1s
sanitizer / msan.Debug (push) Failing after 1s
sanitizer / asan.RelWithDebInfo (push) Failing after 0s
sanitizer / tsan.RelWithDebInfo (push) Failing after 0s
test-bindings / Test Rust bindings on ubuntu-latest (push) Failing after 0s
test-bindings / Test Python 3.10 bindings on ubuntu-latest (push) Failing after 0s
test-bindings / Test Python 3.11 bindings on ubuntu-latest (push) Failing after 0s
test-bindings / Test Python 3.12 bindings on ubuntu-latest (push) Failing after 1s
test-bindings / Test Python 3.13 bindings on ubuntu-latest (push) Failing after 0s
137 lines
4.1 KiB
Python
137 lines
4.1 KiB
Python
# Copyright 2020 Google Inc. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
"""Python benchmarking utilities.
|
|
|
|
Example usage:
|
|
import google_benchmark as benchmark
|
|
|
|
@benchmark.register
|
|
def my_benchmark(state):
|
|
... # Code executed outside `while` loop is not timed.
|
|
|
|
while state:
|
|
... # Code executed within `while` loop is timed.
|
|
|
|
if __name__ == '__main__':
|
|
benchmark.main()
|
|
"""
|
|
|
|
import atexit
|
|
|
|
from google_benchmark import _benchmark
|
|
from google_benchmark._benchmark import (
|
|
Counter as Counter,
|
|
State as State,
|
|
kMicrosecond as kMicrosecond,
|
|
kMillisecond as kMillisecond,
|
|
kNanosecond as kNanosecond,
|
|
kSecond as kSecond,
|
|
o1 as o1,
|
|
oAuto as oAuto,
|
|
oLambda as oLambda,
|
|
oLogN as oLogN,
|
|
oN as oN,
|
|
oNCubed as oNCubed,
|
|
oNLogN as oNLogN,
|
|
oNone as oNone,
|
|
oNSquared as oNSquared,
|
|
)
|
|
|
|
__version__ = "1.9.5"
|
|
|
|
|
|
class __OptionMaker:
|
|
"""A stateless class to collect benchmark options.
|
|
|
|
Collect all decorator calls like @option.range(start=0, limit=1<<5).
|
|
"""
|
|
|
|
class Options:
|
|
"""Pure data class to store options calls, along with the benchmarked
|
|
function."""
|
|
|
|
def __init__(self, func):
|
|
self.func = func
|
|
self.builder_calls = []
|
|
|
|
@classmethod
|
|
def make(cls, func_or_options):
|
|
"""Make Options from Options or the benchmarked function."""
|
|
if isinstance(func_or_options, cls.Options):
|
|
return func_or_options
|
|
return cls.Options(func_or_options)
|
|
|
|
def __getattr__(self, builder_name):
|
|
"""Append option call in the Options."""
|
|
|
|
# The function that get returned on @option.range(start=0, limit=1<<5).
|
|
def __builder_method(*args, **kwargs):
|
|
# The decorator that get called, either with the benchmared function
|
|
# or the previous Options
|
|
def __decorator(func_or_options):
|
|
options = self.make(func_or_options)
|
|
options.builder_calls.append((builder_name, args, kwargs))
|
|
# The decorator returns Options so it is not technically a
|
|
# decorator and needs a final call to @register
|
|
return options
|
|
|
|
return __decorator
|
|
|
|
return __builder_method
|
|
|
|
|
|
# Alias for nicer API.
|
|
# We have to instantiate an object, even if stateless, to be able to use
|
|
# __getattr__ on option.range
|
|
option = __OptionMaker()
|
|
|
|
|
|
def register(undefined=None, *, name=None):
|
|
"""Register function for benchmarking."""
|
|
if undefined is None:
|
|
# Decorator is called without parenthesis so we return a decorator
|
|
return lambda f: register(f, name=name)
|
|
|
|
# We have either the function to benchmark (simple case) or an instance of
|
|
# Options (@option._ case).
|
|
options = __OptionMaker.make(undefined)
|
|
|
|
if name is None:
|
|
name = options.func.__name__
|
|
|
|
# We register the benchmark and reproduce all the @option._ calls onto the
|
|
# benchmark builder pattern
|
|
benchmark = _benchmark.RegisterBenchmark(name, options.func)
|
|
for name, args, kwargs in options.builder_calls[::-1]:
|
|
getattr(benchmark, name)(*args, **kwargs)
|
|
|
|
# return the benchmarked function because the decorator does not modify it
|
|
return options.func
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> None:
|
|
import sys
|
|
|
|
_benchmark.Initialize(argv or sys.argv)
|
|
return _benchmark.RunSpecifiedBenchmarks()
|
|
|
|
|
|
# FIXME: can we rerun with disabled ASLR?
|
|
|
|
# Methods for use with custom main function.
|
|
initialize = _benchmark.Initialize
|
|
run_benchmarks = _benchmark.RunSpecifiedBenchmarks
|
|
add_custom_context = _benchmark.AddCustomContext
|
|
atexit.register(_benchmark.ClearRegisteredBenchmarks)
|