• 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 * context)55   NoOpMutator(ContextType* context) {}
56 };
57 
58 template <int length>
59 class RandomBinaryMetadata {
60  public:
Key()61   static const grpc::string& Key() { return kKey; }
62 
Value()63   static const grpc::string& Value() {
64     return kValues[rand() % kValues.size()];
65   }
66 
67  private:
68   static const grpc::string kKey;
69   static const std::vector<grpc::string> kValues;
70 
GenerateOneString()71   static grpc::string GenerateOneString() {
72     grpc::string s;
73     s.reserve(length + 1);
74     for (int i = 0; i < length; i++) {
75       s += static_cast<char>(rand());
76     }
77     return s;
78   }
79 };
80 
81 template <int length>
82 class RandomAsciiMetadata {
83  public:
Key()84   static const grpc::string& Key() { return kKey; }
85 
Value()86   static const grpc::string& Value() {
87     return kValues[rand() % kValues.size()];
88   }
89 
90  private:
91   static const grpc::string kKey;
92   static const std::vector<grpc::string> kValues;
93 
GenerateOneString()94   static grpc::string GenerateOneString() {
95     grpc::string s;
96     s.reserve(length + 1);
97     for (int i = 0; i < length; i++) {
98       s += static_cast<char>(rand() % 26 + 'a');
99     }
100     return s;
101   }
102 };
103 
104 template <class Generator, int kNumKeys>
105 class Client_AddMetadata : public NoOpMutator {
106  public:
Client_AddMetadata(ClientContext * context)107   Client_AddMetadata(ClientContext* context) : NoOpMutator(context) {
108     for (int i = 0; i < kNumKeys; i++) {
109       context->AddMetadata(Generator::Key(), Generator::Value());
110     }
111   }
112 };
113 
114 template <class Generator, int kNumKeys>
115 class Server_AddInitialMetadata : public NoOpMutator {
116  public:
Server_AddInitialMetadata(ServerContext * context)117   Server_AddInitialMetadata(ServerContext* context) : NoOpMutator(context) {
118     for (int i = 0; i < kNumKeys; i++) {
119       context->AddInitialMetadata(Generator::Key(), Generator::Value());
120     }
121   }
122 };
123 
124 // static initialization
125 
126 template <int length>
127 const grpc::string RandomBinaryMetadata<length>::kKey = "foo-bin";
128 
129 template <int length>
130 const std::vector<grpc::string> RandomBinaryMetadata<length>::kValues =
131     MakeVector(kPregenerateKeyCount, GenerateOneString);
132 
133 template <int length>
134 const grpc::string RandomAsciiMetadata<length>::kKey = "foo";
135 
136 template <int length>
137 const std::vector<grpc::string> RandomAsciiMetadata<length>::kValues =
138     MakeVector(kPregenerateKeyCount, GenerateOneString);
139 
140 }  // namespace testing
141 }  // namespace grpc
142 
143 #endif
144