• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
NoOpMutator(ContextType *)55   NoOpMutator(ContextType* /*context*/) {}
56 };
57 
58 template <int length>
59 class RandomBinaryMetadata {
60  public:
Key()61   static const std::string& Key() { return kKey; }
62 
Value()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 
GenerateOneString()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:
Key()82   static const std::string& Key() { return kKey; }
83 
Value()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 
GenerateOneString()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:
Client_AddMetadata(ClientContext * context)103   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:
Server_AddInitialMetadata(ServerContext * context)113   Server_AddInitialMetadata(ServerContext* context) : NoOpMutator(context) {
114     for (int i = 0; i < kNumKeys; i++) {
115       context->AddInitialMetadata(Generator::Key(), Generator::Value());
116     }
117   }
118 };
119 
120 // static initialization
121 
122 template <int length>
123 const std::string RandomBinaryMetadata<length>::kKey = "foo-bin";
124 
125 template <int length>
126 const std::vector<std::string> RandomBinaryMetadata<length>::kValues =
127     MakeVector(kPregenerateKeyCount, GenerateOneString);
128 
129 template <int length>
130 const std::string RandomAsciiMetadata<length>::kKey = "foo";
131 
132 template <int length>
133 const std::vector<std::string> RandomAsciiMetadata<length>::kValues =
134     MakeVector(kPregenerateKeyCount, GenerateOneString);
135 
136 }  // namespace testing
137 }  // namespace grpc
138 
139 #endif
140