fullstack_context_mutators.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef TEST_CPP_MICROBENCHMARKS_FULLSTACK_CONTEXT_MUTATORS_H
  19. #define TEST_CPP_MICROBENCHMARKS_FULLSTACK_CONTEXT_MUTATORS_H
  20. #include <grpc/support/log.h>
  21. #include <grpcpp/channel.h>
  22. #include <grpcpp/create_channel.h>
  23. #include <grpcpp/security/credentials.h>
  24. #include <grpcpp/security/server_credentials.h>
  25. #include <grpcpp/server.h>
  26. #include <grpcpp/server_builder.h>
  27. #include <grpcpp/server_context.h>
  28. #include "test/cpp/microbenchmarks/helpers.h"
  29. namespace grpc {
  30. namespace testing {
  31. /*******************************************************************************
  32. * CONTEXT MUTATORS
  33. */
  34. static const int kPregenerateKeyCount = 100000;
  35. template <class F>
  36. auto MakeVector(size_t length, F f) -> std::vector<decltype(f())> {
  37. std::vector<decltype(f())> out;
  38. out.reserve(length);
  39. for (size_t i = 0; i < length; i++) {
  40. out.push_back(f());
  41. }
  42. return out;
  43. }
  44. class NoOpMutator {
  45. public:
  46. template <class ContextType>
  47. explicit NoOpMutator(ContextType* /*context*/) {}
  48. };
  49. template <int length>
  50. class RandomBinaryMetadata {
  51. public:
  52. static const std::string& Key() { return kKey; }
  53. static const std::string& Value() { return kValues[rand() % kValues.size()]; }
  54. private:
  55. static const std::string kKey;
  56. static const std::vector<std::string> kValues;
  57. static std::string GenerateOneString() {
  58. std::string s;
  59. s.reserve(length + 1);
  60. for (int i = 0; i < length; i++) {
  61. s += static_cast<char>(rand());
  62. }
  63. return s;
  64. }
  65. };
  66. template <int length>
  67. class RandomAsciiMetadata {
  68. public:
  69. static const std::string& Key() { return kKey; }
  70. static const std::string& Value() { return kValues[rand() % kValues.size()]; }
  71. private:
  72. static const std::string kKey;
  73. static const std::vector<std::string> kValues;
  74. static std::string GenerateOneString() {
  75. std::string s;
  76. s.reserve(length + 1);
  77. for (int i = 0; i < length; i++) {
  78. s += static_cast<char>(rand() % 26 + 'a');
  79. }
  80. return s;
  81. }
  82. };
  83. template <class Generator, int kNumKeys>
  84. class Client_AddMetadata : public NoOpMutator {
  85. public:
  86. explicit Client_AddMetadata(ClientContext* context) : NoOpMutator(context) {
  87. for (int i = 0; i < kNumKeys; i++) {
  88. context->AddMetadata(Generator::Key(), Generator::Value());
  89. }
  90. }
  91. };
  92. template <class Generator, int kNumKeys>
  93. class Server_AddInitialMetadata : public NoOpMutator {
  94. public:
  95. explicit Server_AddInitialMetadata(ServerContext* context)
  96. : NoOpMutator(context) {
  97. for (int i = 0; i < kNumKeys; i++) {
  98. context->AddInitialMetadata(Generator::Key(), Generator::Value());
  99. }
  100. }
  101. };
  102. // static initialization
  103. template <int length>
  104. const std::string RandomBinaryMetadata<length>::kKey = "foo-bin";
  105. template <int length>
  106. const std::vector<std::string> RandomBinaryMetadata<length>::kValues =
  107. MakeVector(kPregenerateKeyCount, GenerateOneString);
  108. template <int length>
  109. const std::string RandomAsciiMetadata<length>::kKey = "foo";
  110. template <int length>
  111. const std::vector<std::string> RandomAsciiMetadata<length>::kValues =
  112. MakeVector(kPregenerateKeyCount, GenerateOneString);
  113. } // namespace testing
  114. } // namespace grpc
  115. #endif