| 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 |
#include <stdio.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <iterator> |
| 24 |
#include <map> |
| 25 |
#include <string> |
| 26 |
#include <utility> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include "absl/algorithm/container.h" |
| 30 |
#include "absl/flags/flag.h" |
| 31 |
#include "absl/flags/parse.h" |
| 32 |
#include "absl/strings/str_cat.h" |
| 33 |
#include "absl/strings/str_split.h" |
| 34 |
#include "absl/strings/string_view.h" |
| 35 |
|
| 36 |
#include <grpc/support/log.h> |
| 37 |
#include <grpc/support/time.h> |
| 38 |
|
| 39 |
#include "src/core/lib/gprpp/host_port.h" |
| 40 |
#include "test/core/util/port.h" |
| 41 |
#include "test/core/util/subprocess.h" |
| 42 |
#include "test/core/util/test_config.h" |
| 43 |
|
| 44 |
ABSL_FLAG(std::string, benchmark_names, "call,channel", |
| 45 |
"Which benchmark to run"); // Default all benchmarks in order to |
| 46 |
// trigger CI testing for each one |
| 47 |
ABSL_FLAG(int, size, 1000, "Number of channels/calls"); |
| 48 |
ABSL_FLAG(std::string, scenario_config, "insecure", |
| 49 |
"Possible Values: minstack (Use minimal stack), resource_quota, " |
| 50 |
"secure (Use SSL credentials on server)"); |
| 51 |
ABSL_FLAG(bool, memory_profiling, false, |
| 52 |
"Run memory profiling"); // TODO (chennancy) Connect this flag |
| 53 |
|
| 54 |
class Subprocess { |
| 55 |
public: |
| 56 |
explicit Subprocess(std::vector<std::string> args) { |
| 57 |
std::vector<const char*> args_c; |
| 58 |
args_c.reserve(args.size()); |
| 59 |
for (const auto& arg : args) { |
| 60 |
args_c.push_back(arg.c_str()); |
| 61 |
} |
| 62 |
process_ = gpr_subprocess_create(args_c.size(), args_c.data()); |
| 63 |
} |
| 64 |
|
| 65 |
int GetPID() { return gpr_subprocess_get_process_id(process_); } |
| 66 |
int Join() { return gpr_subprocess_join(process_); } |
| 67 |
void Interrupt() { gpr_subprocess_interrupt(process_); } |
| 68 |
|
| 69 |
~Subprocess() { gpr_subprocess_destroy(process_); } |
| 70 |
|
| 71 |
private: |
| 72 |
gpr_subprocess* process_; |
| 73 |
}; |
| 74 |
|
| 75 |
/* per-call memory usage benchmark */ |
| 76 |
int RunCallBenchmark(char* root, std::vector<std::string> server_scenario_flags, |
| 77 |
std::vector<std::string> client_scenario_flags) { |
| 78 |
int status; |
| 79 |
int port = grpc_pick_unused_port_or_die(); |
| 80 |
|
| 81 |
/* start the server */ |
| 82 |
std::vector<std::string> server_flags = { |
| 83 |
absl::StrCat(root, "/memory_usage_server", |
| 84 |
gpr_subprocess_binary_extension()), |
| 85 |
"--bind", grpc_core::JoinHostPort("::", port)}; |
| 86 |
// Add scenario-specific server flags to the end of the server_flags |
| 87 |
absl::c_move(server_scenario_flags, std::back_inserter(server_flags)); |
| 88 |
Subprocess svr(server_flags); |
| 89 |
|
| 90 |
/* start the client */ |
| 91 |
std::vector<std::string> client_flags = { |
| 92 |
absl::StrCat(root, "/memory_usage_client", |
| 93 |
gpr_subprocess_binary_extension()), |
| 94 |
"--target", grpc_core::JoinHostPort("127.0.0.1", port), |
| 95 |
absl::StrCat("--warmup=", 10000), |
| 96 |
absl::StrCat("--benchmark=", absl::GetFlag(FLAGS_size))}; |
| 97 |
// Add scenario-specific client flags to the end of the client_flags |
| 98 |
absl::c_move(client_scenario_flags, std::back_inserter(client_flags)); |
| 99 |
Subprocess cli(client_flags); |
| 100 |
/* wait for completion */ |
| 101 |
if ((status = cli.Join()) != 0) { |
| 102 |
printf("client failed with: %d", status); |
| 103 |
return 1; |
| 104 |
} |
| 105 |
|
| 106 |
svr.Interrupt(); |
| 107 |
return svr.Join() == 0 ? 0 : 2; |
| 108 |
} |
| 109 |
|
| 110 |
/* Per-channel benchmark*/ |
| 111 |
int RunChannelBenchmark(char* root) { |
| 112 |
// TODO(chennancy) Add the scenario specific flags |
| 113 |
int status; |
| 114 |
int port = grpc_pick_unused_port_or_die(); |
| 115 |
|
| 116 |
/* start the server */ |
| 117 |
std::vector<std::string> server_flags = { |
| 118 |
absl::StrCat(root, "/memory_usage_callback_server", |
| 119 |
gpr_subprocess_binary_extension()), |
| 120 |
"--bind", grpc_core::JoinHostPort("::", port)}; |
| 121 |
Subprocess svr(server_flags); |
| 122 |
|
| 123 |
// Wait one second before starting client to avoid possible race condition |
| 124 |
// of client sending an RPC before the server is set up |
| 125 |
gpr_sleep_until(grpc_timeout_seconds_to_deadline(1)); |
| 126 |
|
| 127 |
/* start the client */ |
| 128 |
std::vector<std::string> client_flags = { |
| 129 |
absl::StrCat(root, "/memory_usage_callback_client", |
| 130 |
gpr_subprocess_binary_extension()), |
| 131 |
"--target", |
| 132 |
grpc_core::JoinHostPort("127.0.0.1", port), |
| 133 |
"--nosecure", |
| 134 |
absl::StrCat("--server_pid=", svr.GetPID()), |
| 135 |
absl::StrCat("--size=", absl::GetFlag(FLAGS_size))}; |
| 136 |
Subprocess cli(client_flags); |
| 137 |
/* wait for completion */ |
| 138 |
if ((status = cli.Join()) != 0) { |
| 139 |
printf("client failed with: %d", status); |
| 140 |
return 1; |
| 141 |
} |
| 142 |
svr.Interrupt(); |
| 143 |
return svr.Join() == 0 ? 0 : 2; |
| 144 |
} |
| 145 |
|
| 146 |
int RunBenchmark(char* root, absl::string_view benchmark, |
| 147 |
std::vector<std::string> server_scenario_flags, |
| 148 |
std::vector<std::string> client_scenario_flags) { |
| 149 |
if (benchmark == "call") { |
| 150 |
return RunCallBenchmark(root, server_scenario_flags, client_scenario_flags); |
| 151 |
} else if (benchmark == "channel") { |
| 152 |
return RunChannelBenchmark(root); |
| 153 |
} else { |
| 154 |
gpr_log(GPR_INFO, "Not a valid benchmark name"); |
| 155 |
return 4; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
int main(int argc, char** argv) { |
| 160 |
absl::ParseCommandLine(argc, argv); |
| 161 |
|
| 162 |
char* me = argv[0]; |
| 163 |
char* lslash = strrchr(me, '/'); |
| 164 |
char root[1024]; |
| 165 |
|
| 166 |
std::vector<const char*> args; |
| 167 |
/* figure out where we are */ |
| 168 |
if (lslash) { |
| 169 |
memcpy(root, me, static_cast<size_t>(lslash - me)); |
| 170 |
root[lslash - me] = 0; |
| 171 |
} else { |
| 172 |
strcpy(root, "."); |
| 173 |
} |
| 174 |
|
| 175 |
/* Set configurations based off scenario_config*/ |
| 176 |
struct ScenarioArgs { |
| 177 |
std::vector<std::string> client; |
| 178 |
std::vector<std::string> server; |
| 179 |
}; |
| 180 |
// TODO(chennancy): add in resource quota parameter setting later |
| 181 |
const std::map<std::string /*scenario*/, ScenarioArgs> scenarios = { |
| 182 |
{"secure", {/*client=*/{}, /*server=*/{"--secure"}}}, |
| 183 |
{"resource_quota", {/*client=*/{}, /*server=*/{"--secure"}}}, |
| 184 |
{"minstack", {/*client=*/{"--minstack"}, /*server=*/{"--minstack"}}}, |
| 185 |
{"insecure", {{}, {}}}, |
| 186 |
}; |
| 187 |
auto it_scenario = scenarios.find(absl::GetFlag(FLAGS_scenario_config)); |
| 188 |
if (it_scenario == scenarios.end()) { |
| 189 |
printf("No scenario matching the name could be found\n"); |
| 190 |
return 3; |
| 191 |
} |
| 192 |
|
| 193 |
// Run all benchmarks listed (Multiple benchmarks usually only for default |
| 194 |
// scenario) |
| 195 |
auto benchmarks = absl::StrSplit(absl::GetFlag(FLAGS_benchmark_names), ','); |
| 196 |
for (const auto& benchmark : benchmarks) { |
| 197 |
int r = RunBenchmark(root, benchmark, it_scenario->second.server, |
| 198 |
it_scenario->second.client); |
| 199 |
if (r != 0) return r; |
| 200 |
} |
| 201 |
return 0; |
| 202 |
} |