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