1 //
2 //
3 // Copyright 2022 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 #include <signal.h>
20 #include <unistd.h>
21
22 #include <memory>
23 #include <string>
24
25 #include "absl/flags/flag.h"
26 #include "absl/flags/parse.h"
27
28 #include <grpc/support/log.h>
29 #include <grpcpp/grpcpp.h>
30 #include <grpcpp/security/server_credentials.h>
31 #include <grpcpp/support/server_callback.h>
32 #include <grpcpp/support/status.h>
33 #include <grpcpp/xds_server_builder.h>
34
35 #include "src/proto/grpc/testing/benchmark_service.grpc.pb.h"
36 #include "src/proto/grpc/testing/messages.pb.h"
37 #include "test/core/memory_usage/memstats.h"
38 #include "test/core/util/test_config.h"
39
40 ABSL_FLAG(std::string, bind, "", "Bind host:port");
41 ABSL_FLAG(bool, secure, false, "Use SSL Credentials");
42 ABSL_FLAG(bool, use_xds, false, "Use xDS");
43
44 class ServerCallbackImpl final
45 : public grpc::testing::BenchmarkService::CallbackService {
46 public:
ServerCallbackImpl(long before_server_memory)47 explicit ServerCallbackImpl(long before_server_memory)
48 : before_server_create(before_server_memory) {}
49
UnaryCall(grpc::CallbackServerContext * context,const grpc::testing::SimpleRequest *,grpc::testing::SimpleResponse *)50 grpc::ServerUnaryReactor* UnaryCall(
51 grpc::CallbackServerContext* context,
52 const grpc::testing::SimpleRequest* /* request */,
53 grpc::testing::SimpleResponse* /* response */) override {
54 auto* reactor = context->DefaultReactor();
55 reactor->Finish(grpc::Status::OK);
56 return reactor;
57 }
GetBeforeSnapshot(grpc::CallbackServerContext * context,const grpc::testing::SimpleRequest *,grpc::testing::MemorySize * response)58 grpc::ServerUnaryReactor* GetBeforeSnapshot(
59 grpc::CallbackServerContext* context,
60 const grpc::testing::SimpleRequest* /* request */,
61 grpc::testing::MemorySize* response) override {
62 gpr_log(GPR_INFO, "BeforeSnapshot RPC CALL RECEIVED");
63 response->set_rss(before_server_create);
64 auto* reactor = context->DefaultReactor();
65 reactor->Finish(grpc::Status::OK);
66 return reactor;
67 }
68
69 private:
70 long before_server_create;
71 };
72
73 // We have some sort of deadlock, so let's not exit gracefully for now.
74 // TODO(chennancy) Add graceful shutdown
sigint_handler(int)75 static void sigint_handler(int /*x*/) { _exit(0); }
76
main(int argc,char ** argv)77 int main(int argc, char** argv) {
78 absl::ParseCommandLine(argc, argv);
79 char* fake_argv[1];
80 GPR_ASSERT(argc >= 1);
81 fake_argv[0] = argv[0];
82 grpc::testing::TestEnvironment env(&argc, argv);
83 grpc_init();
84 signal(SIGINT, sigint_handler);
85 std::string server_address = absl::GetFlag(FLAGS_bind);
86 if (server_address.empty()) {
87 gpr_log(GPR_ERROR, "Server: No port entered");
88 return 1;
89 }
90 gpr_log(GPR_INFO, "Server port: %s", server_address.c_str());
91
92 // Get initial process memory usage before creating server
93 long before_server_create = GetMemUsage();
94 ServerCallbackImpl callback_server(before_server_create);
95
96 grpc::XdsServerBuilder xds_builder;
97 grpc::ServerBuilder normal_builder;
98 grpc::ServerBuilder* builder =
99 absl::GetFlag(FLAGS_use_xds) ? &xds_builder : &normal_builder;
100
101 // Set the authentication mechanism.
102 std::shared_ptr<grpc::ServerCredentials> creds =
103 grpc::InsecureServerCredentials();
104 if (absl::GetFlag(FLAGS_secure)) {
105 gpr_log(GPR_INFO, "Supposed to be secure, is not yet");
106 // TODO (chennancy) Add in secure credentials
107 }
108 builder->AddListeningPort(server_address, creds);
109 builder->RegisterService(&callback_server);
110
111 // Set up the server to start accepting requests.
112 std::shared_ptr<grpc::Server> server(builder->BuildAndStart());
113 gpr_log(GPR_INFO, "Server listening on %s", server_address.c_str());
114
115 // Keep the program running until the server shuts down.
116 server->Wait();
117 return 0;
118 }
119