| 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 <limits.h> |
| 20 |
#include <stdio.h> |
| 21 |
|
| 22 |
#include <chrono> |
| 23 |
#include <memory> |
| 24 |
#include <string> |
| 25 |
#include <utility> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
#include "absl/flags/flag.h" |
| 29 |
#include "absl/flags/parse.h" |
| 30 |
#include "absl/strings/string_view.h" |
| 31 |
|
| 32 |
#include <grpc/support/log.h> |
| 33 |
#include <grpcpp/grpcpp.h> |
| 34 |
#include <grpcpp/security/credentials.h> |
| 35 |
#include <grpcpp/support/channel_arguments.h> |
| 36 |
#include <grpcpp/support/status.h> |
| 37 |
|
| 38 |
#include "src/core/lib/gprpp/notification.h" |
| 39 |
#include "src/proto/grpc/testing/benchmark_service.grpc.pb.h" |
| 40 |
#include "src/proto/grpc/testing/messages.pb.h" |
| 41 |
#include "test/core/memory_usage/memstats.h" |
| 42 |
#include "test/core/util/test_config.h" |
| 43 |
|
| 44 |
ABSL_FLAG(std::string, target, "", "Target host:port"); |
| 45 |
ABSL_FLAG(bool, secure, false, "Use SSL Credentials"); |
| 46 |
ABSL_FLAG(int, server_pid, 99999, "Server's pid"); |
| 47 |
ABSL_FLAG(int, size, 50, "Number of channels"); |
| 48 |
|
| 49 |
std::shared_ptr<grpc::Channel> CreateChannelForTest(int index) { |
| 50 |
// Set the authentication mechanism. |
| 51 |
std::shared_ptr<grpc::ChannelCredentials> creds = |
| 52 |
grpc::InsecureChannelCredentials(); |
| 53 |
if (absl::GetFlag(FLAGS_secure)) { |
| 54 |
// TODO (chennancy) Add in secure credentials |
| 55 |
gpr_log(GPR_INFO, "Supposed to be secure, is not yet"); |
| 56 |
} |
| 57 |
|
| 58 |
// Channel args to prevent connection from closing after RPC is done |
| 59 |
grpc::ChannelArguments channel_args; |
| 60 |
channel_args.SetInt(GRPC_ARG_MAX_CONNECTION_IDLE_MS, INT_MAX); |
| 61 |
channel_args.SetInt(GRPC_ARG_MAX_CONNECTION_AGE_MS, INT_MAX); |
| 62 |
// Arg to bypass mechanism that combines channels on the serverside if they |
| 63 |
// have the same channel args. Allows for one channel per connection |
| 64 |
channel_args.SetInt("grpc.memory_usage_counter", index); |
| 65 |
|
| 66 |
// Create a channel to the server and a stub |
| 67 |
std::shared_ptr<grpc::Channel> channel = |
| 68 |
CreateCustomChannel(absl::GetFlag(FLAGS_target), creds, channel_args); |
| 69 |
return channel; |
| 70 |
} |
| 71 |
|
| 72 |
struct CallParams { |
| 73 |
grpc::ClientContext context; |
| 74 |
grpc::testing::SimpleRequest request; |
| 75 |
grpc::testing::SimpleResponse response; |
| 76 |
grpc::testing::MemorySize snapshot_response; |
| 77 |
grpc_core::Notification done; |
| 78 |
}; |
| 79 |
|
| 80 |
// Simple Unary RPC to send to confirm connection is open |
| 81 |
std::shared_ptr<CallParams> UnaryCall(std::shared_ptr<grpc::Channel> channel) { |
| 82 |
std::unique_ptr<grpc::testing::BenchmarkService::Stub> stub = |
| 83 |
grpc::testing::BenchmarkService::NewStub(channel); |
| 84 |
|
| 85 |
// Start a call. |
| 86 |
auto params = std::make_shared<CallParams>(); |
| 87 |
stub->async()->UnaryCall(¶ms->context, ¶ms->request, |
| 88 |
¶ms->response, |
| 89 |
[params](const grpc::Status& status) { |
| 90 |
if (!status.ok()) { |
| 91 |
gpr_log(GPR_ERROR, "UnaryCall RPC failed."); |
| 92 |
} |
| 93 |
params->done.Notify(); |
| 94 |
}); |
| 95 |
return params; |
| 96 |
} |
| 97 |
|
| 98 |
// Get memory usage of server's process before the server is made |
| 99 |
std::shared_ptr<CallParams> GetBeforeSnapshot( |
| 100 |
std::shared_ptr<grpc::Channel> channel, long& before_server_memory) { |
| 101 |
std::unique_ptr<grpc::testing::BenchmarkService::Stub> stub = |
| 102 |
grpc::testing::BenchmarkService::NewStub(channel); |
| 103 |
|
| 104 |
// Start a call. |
| 105 |
auto params = std::make_shared<CallParams>(); |
| 106 |
stub->async()->GetBeforeSnapshot( |
| 107 |
¶ms->context, ¶ms->request, ¶ms->snapshot_response, |
| 108 |
[params, &before_server_memory](const grpc::Status& status) { |
| 109 |
if (status.ok()) { |
| 110 |
before_server_memory = params->snapshot_response.rss(); |
| 111 |
gpr_log(GPR_INFO, "Server Before RPC: %ld", |
| 112 |
params->snapshot_response.rss()); |
| 113 |
gpr_log(GPR_INFO, "GetBeforeSnapshot succeeded."); |
| 114 |
} else { |
| 115 |
gpr_log(GPR_ERROR, "GetBeforeSnapshot failed."); |
| 116 |
} |
| 117 |
params->done.Notify(); |
| 118 |
}); |
| 119 |
return params; |
| 120 |
} |
| 121 |
|
| 122 |
int main(int argc, char** argv) { |
| 123 |
absl::ParseCommandLine(argc, argv); |
| 124 |
char* fake_argv[1]; |
| 125 |
GPR_ASSERT(argc >= 1); |
| 126 |
fake_argv[0] = argv[0]; |
| 127 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 128 |
if (absl::GetFlag(FLAGS_target).empty()) { |
| 129 |
gpr_log(GPR_ERROR, "Client: No target port entered"); |
| 130 |
return 1; |
| 131 |
} |
| 132 |
gpr_log(GPR_INFO, "Client Target: %s", absl::GetFlag(FLAGS_target).c_str()); |
| 133 |
gpr_log(GPR_INFO, "Client Size: %d", absl::GetFlag(FLAGS_size)); |
| 134 |
|
| 135 |
// Getting initial memory usage |
| 136 |
std::shared_ptr<grpc::Channel> get_memory_channel = CreateChannelForTest(0); |
| 137 |
long before_server_memory; |
| 138 |
GetBeforeSnapshot(get_memory_channel, before_server_memory) |
| 139 |
->done.WaitForNotification(); |
| 140 |
long before_client_memory = GetMemUsage(); |
| 141 |
|
| 142 |
// Create the channels and send an RPC to confirm they're open |
| 143 |
int size = absl::GetFlag(FLAGS_size); |
| 144 |
std::vector<std::shared_ptr<grpc::Channel>> channels_list(size); |
| 145 |
for (int i = 0; i < size; ++i) { |
| 146 |
std::shared_ptr<grpc::Channel> channel = CreateChannelForTest(i); |
| 147 |
channels_list[i] = channel; |
| 148 |
UnaryCall(channel)->done.WaitForNotification(); |
| 149 |
} |
| 150 |
|
| 151 |
// Getting peak memory usage |
| 152 |
long peak_server_memory = GetMemUsage(absl::GetFlag(FLAGS_server_pid)); |
| 153 |
long peak_client_memory = GetMemUsage(); |
| 154 |
|
| 155 |
// Checking that all channels are still open |
| 156 |
for (int i = 0; i < size; ++i) { |
| 157 |
GPR_ASSERT(!std::exchange(channels_list[i], nullptr) |
| 158 |
->WaitForStateChange(GRPC_CHANNEL_READY, |
| 159 |
std::chrono::system_clock::now() + |
| 160 |
std::chrono::milliseconds(1))); |
| 161 |
} |
| 162 |
|
| 163 |
printf("---------Client channel stats--------\n"); |
| 164 |
printf("client channel memory usage: %f bytes per channel\n", |
| 165 |
static_cast<double>(peak_client_memory - before_client_memory) / size * |
| 166 |
1024); |
| 167 |
printf("---------Server channel stats--------\n"); |
| 168 |
printf("server channel memory usage: %f bytes per channel\n", |
| 169 |
static_cast<double>(peak_server_memory - before_server_memory) / size * |
| 170 |
1024); |
| 171 |
gpr_log(GPR_INFO, "Client Done"); |
| 172 |
return 0; |
| 173 |
} |