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.
|
|
|
|
"""Python benchmarking utilities.
|
|
|
|
|
|
|
|
Example usage:
|
2020-07-09 11:54:41 +00:00
|
|
|
import google_benchmark as benchmark
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
@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()
|
|
|
|
"""
|
|
|
|
|
|
|
|
from absl import app
|
2020-07-09 11:54:41 +00:00
|
|
|
from google_benchmark import _benchmark
|
2020-09-10 08:57:30 +00:00
|
|
|
from google_benchmark._benchmark import Counter
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"register",
|
|
|
|
"main",
|
2020-09-10 08:57:30 +00:00
|
|
|
"Counter",
|
2020-05-06 16:28:29 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
__version__ = "0.1.0"
|
|
|
|
|
|
|
|
|
|
|
|
def register(f=None, *, name=None):
|
2020-09-09 08:43:26 +00:00
|
|
|
if f is None:
|
|
|
|
return lambda f: register(f, name=name)
|
|
|
|
if name is None:
|
|
|
|
name = f.__name__
|
|
|
|
_benchmark.RegisterBenchmark(name, f)
|
|
|
|
return f
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _flags_parser(argv):
|
2020-09-09 08:43:26 +00:00
|
|
|
argv = _benchmark.Initialize(argv)
|
|
|
|
return app.parse_flags_with_usage(argv)
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _run_benchmarks(argv):
|
2020-09-09 08:43:26 +00:00
|
|
|
if len(argv) > 1:
|
2020-09-10 08:57:30 +00:00
|
|
|
raise app.UsageError("Too many command-line arguments.")
|
2020-09-09 08:43:26 +00:00
|
|
|
return _benchmark.RunSpecifiedBenchmarks()
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main(argv=None):
|
2020-09-09 08:43:26 +00:00
|
|
|
return app.run(_run_benchmarks, argv=argv, flags_parser=_flags_parser)
|
2020-06-30 08:51:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Methods for use with custom main function.
|
|
|
|
initialize = _benchmark.Initialize
|
|
|
|
run_benchmarks = _benchmark.RunSpecifiedBenchmarks
|