bm_alarm.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /* This benchmark exists to ensure that immediately-firing alarms are fast */
  19. #include <benchmark/benchmark.h>
  20. #include <grpc/grpc.h>
  21. #include <grpcpp/alarm.h>
  22. #include <grpcpp/completion_queue.h>
  23. #include <grpcpp/impl/grpc_library.h>
  24. #include "test/core/util/test_config.h"
  25. #include "test/cpp/microbenchmarks/helpers.h"
  26. #include "test/cpp/util/test_config.h"
  27. namespace grpc {
  28. namespace testing {
  29. static void BM_Alarm_Tag_Immediate(benchmark::State& state) {
  30. TrackCounters track_counters;
  31. CompletionQueue cq;
  32. Alarm alarm;
  33. void* output_tag;
  34. bool ok;
  35. auto deadline = grpc_timeout_seconds_to_deadline(0);
  36. for (auto _ : state) {
  37. alarm.Set(&cq, deadline, nullptr);
  38. cq.Next(&output_tag, &ok);
  39. }
  40. track_counters.Finish(state);
  41. }
  42. BENCHMARK(BM_Alarm_Tag_Immediate);
  43. } // namespace testing
  44. } // namespace grpc
  45. // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
  46. // and others do not. This allows us to support both modes.
  47. namespace benchmark {
  48. void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
  49. } // namespace benchmark
  50. int main(int argc, char** argv) {
  51. grpc::testing::TestEnvironment env(argc, argv);
  52. LibraryInitializer libInit;
  53. ::benchmark::Initialize(&argc, argv);
  54. grpc::testing::InitTest(&argc, &argv, false);
  55. benchmark::RunTheBenchmarksNamespaced();
  56. return 0;
  57. }