bm_channel.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. *
  3. * Copyright 2017 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. /* Benchmark channel */
  19. #include <benchmark/benchmark.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/grpc_security.h>
  22. #include "test/core/util/test_config.h"
  23. #include "test/cpp/microbenchmarks/helpers.h"
  24. #include "test/cpp/util/test_config.h"
  25. class ChannelDestroyerFixture {
  26. public:
  27. ChannelDestroyerFixture() {}
  28. virtual ~ChannelDestroyerFixture() {
  29. if (channel_) {
  30. grpc_channel_destroy(channel_);
  31. }
  32. }
  33. virtual void Init() = 0;
  34. protected:
  35. grpc_channel* channel_ = nullptr;
  36. };
  37. class InsecureChannelFixture : public ChannelDestroyerFixture {
  38. public:
  39. InsecureChannelFixture() {}
  40. void Init() override {
  41. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  42. channel_ = grpc_channel_create("localhost:1234", creds, nullptr);
  43. grpc_channel_credentials_release(creds);
  44. }
  45. };
  46. class LameChannelFixture : public ChannelDestroyerFixture {
  47. public:
  48. LameChannelFixture() {}
  49. void Init() override {
  50. channel_ = grpc_lame_client_channel_create(
  51. "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
  52. }
  53. };
  54. template <class Fixture>
  55. static void BM_InsecureChannelCreateDestroy(benchmark::State& state) {
  56. // In order to test if channel creation time is affected by the number of
  57. // already existing channels, we create some initial channels here.
  58. Fixture initial_channels[512];
  59. for (int i = 0; i < state.range(0); i++) {
  60. initial_channels[i].Init();
  61. }
  62. for (auto _ : state) {
  63. Fixture channel;
  64. channel.Init();
  65. }
  66. }
  67. BENCHMARK_TEMPLATE(BM_InsecureChannelCreateDestroy, InsecureChannelFixture)
  68. ->Range(0, 512);
  69. ;
  70. BENCHMARK_TEMPLATE(BM_InsecureChannelCreateDestroy, LameChannelFixture)
  71. ->Range(0, 512);
  72. ;
  73. // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
  74. // and others do not. This allows us to support both modes.
  75. namespace benchmark {
  76. void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
  77. } // namespace benchmark
  78. int main(int argc, char** argv) {
  79. grpc::testing::TestEnvironment env(argc, argv);
  80. LibraryInitializer libInit;
  81. ::benchmark::Initialize(&argc, argv);
  82. grpc::testing::InitTest(&argc, &argv, false);
  83. benchmark::RunTheBenchmarksNamespaced();
  84. return 0;
  85. }