Debian Codegraph @

test/cpp/qps/server.h

raw
1 /*
2 *
3 * Copyright 2015 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_QPS_SERVER_H
20 #define TEST_QPS_SERVER_H
21
22 #include <vector>
23
24 #include <grpc/support/cpu.h>
25 #include <grpc/support/log.h>
26 #include <grpcpp/channel.h>
27 #include <grpcpp/resource_quota.h>
28 #include <grpcpp/security/server_credentials.h>
29 #include <grpcpp/server_builder.h>
30
31 #include "src/proto/grpc/testing/control.pb.h"
32 #include "src/proto/grpc/testing/messages.pb.h"
33 #include "test/core/end2end/data/ssl_test_data.h"
34 #include "test/core/util/port.h"
35 #include "test/cpp/qps/usage_timer.h"
36 #include "test/cpp/util/test_credentials_provider.h"
37
38 namespace grpc {
39 namespace testing {
40
41 class Server {
42 public:
43 explicit Server(const ServerConfig& config)
44 : timer_(new UsageTimer), last_reset_poll_count_(0) {
45 cores_ = gpr_cpu_num_cores();
46 if (config.port()) { // positive for a fixed port, negative for inproc
47 port_ = config.port();
48 } else { // zero for dynamic port
49 port_ = grpc_pick_unused_port_or_die();
50 }
51 }
52 virtual ~Server() {}
53
54 ServerStats Mark(bool reset) {
55 UsageTimer::Result timer_result;
56 int cur_poll_count = GetPollCount();
57 int poll_count = cur_poll_count - last_reset_poll_count_;
58 if (reset) {
59 std::unique_ptr<UsageTimer> timer(new UsageTimer);
60 timer.swap(timer_);
61 timer_result = timer->Mark();
62 last_reset_poll_count_ = cur_poll_count;
63 } else {
64 timer_result = timer_->Mark();
65 }
66
67 ServerStats stats;
68 stats.set_time_elapsed(timer_result.wall);
69 stats.set_time_system(timer_result.system);
70 stats.set_time_user(timer_result.user);
71 stats.set_total_cpu_time(timer_result.total_cpu_time);
72 stats.set_idle_cpu_time(timer_result.idle_cpu_time);
73 stats.set_cq_poll_count(poll_count);
74 return stats;
75 }
76
77 static bool SetPayload(PayloadType type, int size, Payload* payload) {
78 // TODO(yangg): Support UNCOMPRESSABLE payload.
79 if (type != PayloadType::COMPRESSABLE) {
80 return false;
81 }
82 payload->set_type(type);
83 // Don't waste time creating a new payload of identical size.
84 if (payload->body().length() != static_cast<size_t>(size)) {
85 std::unique_ptr<char[]> body(new char[size]());
86 payload->set_body(body.get(), size);
87 }
88 return true;
89 }
90
91 int port() const { return port_; }
92 int cores() const { return cores_; }
93 static std::shared_ptr<ServerCredentials> CreateServerCredentials(
94 const ServerConfig& config) {
95 if (config.has_security_params()) {
96 std::string type;
97 if (config.security_params().cred_type().empty()) {
98 type = kTlsCredentialsType;
99 } else {
100 type = config.security_params().cred_type();
101 }
102
103 return GetCredentialsProvider()->GetServerCredentials(type);
104 } else {
105 return InsecureServerCredentials();
106 }
107 }
108
109 virtual int GetPollCount() {
110 // For sync server.
111 return 0;
112 }
113
114 virtual std::shared_ptr<Channel> InProcessChannel(
115 const ChannelArguments& args) = 0;
116
117 protected:
118 static void ApplyConfigToBuilder(const ServerConfig& config,
119 ServerBuilder* builder) {
120 if (config.resource_quota_size() > 0) {
121 builder->SetResourceQuota(ResourceQuota("AsyncQpsServerTest")
122 .Resize(config.resource_quota_size()));
123 }
124 for (const auto& channel_arg : config.channel_args()) {
125 switch (channel_arg.value_case()) {
126 case ChannelArg::kStrValue:
127 builder->AddChannelArgument(channel_arg.name(),
128 channel_arg.str_value());
129 break;
130 case ChannelArg::kIntValue:
131 builder->AddChannelArgument(channel_arg.name(),
132 channel_arg.int_value());
133 break;
134 case ChannelArg::VALUE_NOT_SET:
135 gpr_log(GPR_ERROR, "Channel arg '%s' does not have a value",
136 channel_arg.name().c_str());
137 break;
138 }
139 }
140 }
141
142 private:
143 int port_;
144 int cores_;
145 std::unique_ptr<UsageTimer> timer_;
146 int last_reset_poll_count_;
147 };
148
149 std::unique_ptr<Server> CreateSynchronousServer(const ServerConfig& config);
150 std::unique_ptr<Server> CreateAsyncServer(const ServerConfig& config);
151 std::unique_ptr<Server> CreateAsyncGenericServer(const ServerConfig& config);
152 std::unique_ptr<Server> CreateCallbackServer(const ServerConfig& config);
153
154 } // namespace testing
155 } // namespace grpc
156
157 #endif