otel_cc_benchmark.bzl 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. def otel_cc_benchmark(name, srcs, deps, tags = [""]):
  4. """
  5. Creates targets for the benchmark and related targets.
  6. Example:
  7. otel_cc_benchmark(
  8. name = "foo_benchmark",
  9. srcs = ["foo_benchmark.cc"],
  10. deps = ["//bar"],
  11. )
  12. Creates:
  13. :foo_benchmark (the benchmark binary)
  14. :foo_benchmark_result (results from running the benchmark)
  15. :foo_benchmark_smoketest (a fast test that runs a single iteration)
  16. """
  17. # This is the benchmark as a binary, it can be run manually, and is used
  18. # to generate the _result below.
  19. native.cc_binary(
  20. name = name,
  21. srcs = srcs,
  22. deps = deps + ["@com_github_google_benchmark//:benchmark"],
  23. tags = tags + ["manual"],
  24. defines = ["BAZEL_BUILD"],
  25. )
  26. # The result of running the benchmark, captured into a text file.
  27. native.genrule(
  28. name = name + "_result",
  29. outs = [name + "_result.json"],
  30. tools = [":" + name],
  31. tags = tags + ["benchmark_result", "manual"],
  32. testonly = True,
  33. cmd = "$(location :" + name + (") --benchmark_format=json --benchmark_color=false --benchmark_min_time=.1s &> $@"),
  34. )
  35. # This is run as part of "bazel test ..." to smoke-test benchmarks. It's
  36. # meant to complete quickly rather than get accurate results.
  37. native.cc_test(
  38. name = name + "_smoketest",
  39. srcs = srcs,
  40. deps = deps + ["@com_github_google_benchmark//:benchmark"],
  41. args = ["--benchmark_min_time=1x"],
  42. tags = tags + ["benchmark"],
  43. defines = ["BAZEL_BUILD"],
  44. )