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