2020-05-06 16:28:29 +00:00
|
|
|
# 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.
|
2020-06-08 16:29:43 +00:00
|
|
|
"""Example of Python using C++ benchmark framework.
|
|
|
|
|
2020-07-09 08:23:06 +00:00
|
|
|
To run this example, you must first install the `google_benchmark` Python package.
|
2020-06-08 16:29:43 +00:00
|
|
|
|
2020-07-09 08:23:06 +00:00
|
|
|
To install using `setup.py`, download and extract the `google_benchmark` source.
|
2020-06-08 16:29:43 +00:00
|
|
|
In the extracted directory, execute:
|
|
|
|
python setup.py install
|
|
|
|
"""
|
2020-05-06 16:28:29 +00:00
|
|
|
|
2020-09-10 08:57:30 +00:00
|
|
|
import random
|
|
|
|
import time
|
|
|
|
|
2020-07-09 08:23:06 +00:00
|
|
|
import google_benchmark as benchmark
|
2020-09-10 08:57:30 +00:00
|
|
|
from google_benchmark import Counter
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@benchmark.register
|
|
|
|
def empty(state):
|
2020-09-09 08:43:26 +00:00
|
|
|
while state:
|
|
|
|
pass
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@benchmark.register
|
|
|
|
def sum_million(state):
|
2020-09-09 08:43:26 +00:00
|
|
|
while state:
|
|
|
|
sum(range(1_000_000))
|
2020-05-06 16:28:29 +00:00
|
|
|
|
Add pre-commit config and GitHub Actions job (#1688)
* Add pre-commit config and GitHub Actions job
Contains the following hooks:
* buildifier - for formatting and linting Bazel files.
* mypy, ruff, isort, black - for Python typechecking, import hygiene,
static analysis, and formatting.
The pylint CI job was changed to be a pre-commit CI job, where pre-commit
is bootstrapped via Python.
Pylint is currently no longer part of the
code checks, but can be re-added if requested. The reason to drop was
that it does not play nicely with pre-commit, and lots of its
functionality and responsibilities are actually covered in ruff.
* Add dev extra to pyproject.toml for development installs
* Clarify that pre-commit contains only Python and Bazel hooks
* Add one-line docstrings to Bazel modules
* Apply buildifier pre-commit fixes to Bazel files
* Apply pre-commit fixes to Python files
* Supply --profile=black to isort to prevent conflicts
* Fix nanobind build file formatting
* Add tooling configs to `pyproject.toml`
In particular, set line length 80 for all Python files.
* Reformat all Python files to line length 80, fix return type annotations
Also ignores the `tools/compare.py` and `tools/gbench/report.py` files
for mypy, since they emit a barrage of errors which we can deal with
later. The errors are mostly related to dynamic classmethod definition.
2023-10-30 15:35:37 +00:00
|
|
|
|
2020-09-10 08:57:30 +00:00
|
|
|
@benchmark.register
|
|
|
|
def pause_timing(state):
|
|
|
|
"""Pause timing every iteration."""
|
|
|
|
while state:
|
|
|
|
# Construct a list of random ints every iteration without timing it
|
|
|
|
state.pause_timing()
|
|
|
|
random_list = [random.randint(0, 100) for _ in range(100)]
|
|
|
|
state.resume_timing()
|
|
|
|
# Time the in place sorting algorithm
|
|
|
|
random_list.sort()
|
|
|
|
|
2020-05-06 16:28:29 +00:00
|
|
|
|
2020-05-28 08:33:06 +00:00
|
|
|
@benchmark.register
|
|
|
|
def skipped(state):
|
2020-09-09 08:43:26 +00:00
|
|
|
if True: # Test some predicate here.
|
2020-09-10 08:57:30 +00:00
|
|
|
state.skip_with_error("some error")
|
2020-09-09 08:43:26 +00:00
|
|
|
return # NOTE: You must explicitly return, or benchmark will continue.
|
2020-05-28 08:33:06 +00:00
|
|
|
|
2020-09-10 08:57:30 +00:00
|
|
|
... # Benchmark code would be here.
|
|
|
|
|
|
|
|
|
|
|
|
@benchmark.register
|
|
|
|
def manual_timing(state):
|
|
|
|
while state:
|
|
|
|
# Manually count Python CPU time
|
|
|
|
start = time.perf_counter() # perf_counter_ns() in Python 3.7+
|
2020-09-11 09:55:18 +00:00
|
|
|
# Something to benchmark
|
2020-09-10 08:57:30 +00:00
|
|
|
time.sleep(0.01)
|
|
|
|
end = time.perf_counter()
|
|
|
|
state.set_iteration_time(end - start)
|
|
|
|
|
|
|
|
|
|
|
|
@benchmark.register
|
|
|
|
def custom_counters(state):
|
2023-01-10 12:25:32 +00:00
|
|
|
"""Collect custom metric using benchmark.Counter."""
|
2020-09-10 08:57:30 +00:00
|
|
|
num_foo = 0.0
|
|
|
|
while state:
|
|
|
|
# Benchmark some code here
|
|
|
|
pass
|
|
|
|
# Collect some custom metric named foo
|
|
|
|
num_foo += 0.13
|
|
|
|
|
|
|
|
# Automatic Counter from numbers.
|
|
|
|
state.counters["foo"] = num_foo
|
|
|
|
# Set a counter as a rate.
|
|
|
|
state.counters["foo_rate"] = Counter(num_foo, Counter.kIsRate)
|
|
|
|
# Set a counter as an inverse of rate.
|
Add pre-commit config and GitHub Actions job (#1688)
* Add pre-commit config and GitHub Actions job
Contains the following hooks:
* buildifier - for formatting and linting Bazel files.
* mypy, ruff, isort, black - for Python typechecking, import hygiene,
static analysis, and formatting.
The pylint CI job was changed to be a pre-commit CI job, where pre-commit
is bootstrapped via Python.
Pylint is currently no longer part of the
code checks, but can be re-added if requested. The reason to drop was
that it does not play nicely with pre-commit, and lots of its
functionality and responsibilities are actually covered in ruff.
* Add dev extra to pyproject.toml for development installs
* Clarify that pre-commit contains only Python and Bazel hooks
* Add one-line docstrings to Bazel modules
* Apply buildifier pre-commit fixes to Bazel files
* Apply pre-commit fixes to Python files
* Supply --profile=black to isort to prevent conflicts
* Fix nanobind build file formatting
* Add tooling configs to `pyproject.toml`
In particular, set line length 80 for all Python files.
* Reformat all Python files to line length 80, fix return type annotations
Also ignores the `tools/compare.py` and `tools/gbench/report.py` files
for mypy, since they emit a barrage of errors which we can deal with
later. The errors are mostly related to dynamic classmethod definition.
2023-10-30 15:35:37 +00:00
|
|
|
state.counters["foo_inv_rate"] = Counter(
|
|
|
|
num_foo, Counter.kIsRate | Counter.kInvert
|
|
|
|
)
|
2020-09-10 08:57:30 +00:00
|
|
|
# Set a counter as a thread-average quantity.
|
|
|
|
state.counters["foo_avg"] = Counter(num_foo, Counter.kAvgThreads)
|
|
|
|
# There's also a combined flag:
|
|
|
|
state.counters["foo_avg_rate"] = Counter(num_foo, Counter.kAvgThreadsRate)
|
2020-05-28 08:33:06 +00:00
|
|
|
|
|
|
|
|
2020-09-11 09:55:18 +00:00
|
|
|
@benchmark.register
|
|
|
|
@benchmark.option.measure_process_cpu_time()
|
|
|
|
@benchmark.option.use_real_time()
|
|
|
|
def with_options(state):
|
|
|
|
while state:
|
|
|
|
sum(range(1_000_000))
|
|
|
|
|
|
|
|
|
|
|
|
@benchmark.register(name="sum_million_microseconds")
|
|
|
|
@benchmark.option.unit(benchmark.kMicrosecond)
|
2021-06-28 09:28:04 +00:00
|
|
|
def with_options2(state):
|
2020-09-11 09:55:18 +00:00
|
|
|
while state:
|
|
|
|
sum(range(1_000_000))
|
|
|
|
|
|
|
|
|
|
|
|
@benchmark.register
|
|
|
|
@benchmark.option.arg(100)
|
|
|
|
@benchmark.option.arg(1000)
|
|
|
|
def passing_argument(state):
|
|
|
|
while state:
|
|
|
|
sum(range(state.range(0)))
|
|
|
|
|
|
|
|
|
|
|
|
@benchmark.register
|
|
|
|
@benchmark.option.range(8, limit=8 << 10)
|
|
|
|
def using_range(state):
|
|
|
|
while state:
|
|
|
|
sum(range(state.range(0)))
|
|
|
|
|
|
|
|
|
|
|
|
@benchmark.register
|
|
|
|
@benchmark.option.range_multiplier(2)
|
|
|
|
@benchmark.option.range(1 << 10, 1 << 18)
|
|
|
|
@benchmark.option.complexity(benchmark.oN)
|
|
|
|
def computing_complexity(state):
|
|
|
|
while state:
|
|
|
|
sum(range(state.range(0)))
|
|
|
|
state.complexity_n = state.range(0)
|
|
|
|
|
|
|
|
|
2020-09-10 08:57:30 +00:00
|
|
|
if __name__ == "__main__":
|
2020-09-09 08:43:26 +00:00
|
|
|
benchmark.main()
|