Skip to content

Commit 0580f35

Browse files
andreas-abelcopybara-github
authored andcommitted
Select a random unused port for the RPC benchmark
Currently, the benchmark uses a hard-coded port. This causes issues when running multiple instances of the benchmark concurrently, as they all use the same port. PiperOrigin-RevId: 752333959 Change-Id: I3f7642a6dcd25fd0e5f1b4fe15f9d056ecf8b198
1 parent e19a0a0 commit 0580f35

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

fleetbench/rpc/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ cc_library(
154154
"@com_github_grpc_grpc//:grpc++",
155155
"@com_google_absl//absl/flags:flag",
156156
"@com_google_absl//absl/log",
157+
"@com_google_absl//absl/log:check",
157158
"@com_google_absl//absl/random",
158159
"@com_google_absl//absl/status",
159160
"@com_google_absl//absl/strings",

fleetbench/rpc/rpcperf.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <vector>
2121

2222
#include "absl/flags/flag.h"
23+
#include "absl/log/check.h"
2324
#include "absl/log/log.h"
2425
#include "absl/random/random.h"
2526
#include "absl/status/status.h"
@@ -94,15 +95,20 @@ std::string HostPortString(absl::string_view host, uint16_t port) {
9495
std::unique_ptr<GRPCServer> CreateAndStartServer(
9596
std::vector<std::string> ports, int32_t workers, bool compress,
9697
bool checksum, std::string logstats_output_path,
97-
std::string resp_delay_us_dist, absl::string_view program) {
98+
std::string resp_delay_us_dist, absl::string_view program,
99+
std::vector<int>* selected_ports) {
100+
CHECK(selected_ports == nullptr || selected_ports->size() == ports.size())
101+
<< "selected_ports must be nullptr, or have the same size as ports";
102+
98103
// First parse the distributions.
99104
fleetbench::rpc::DistributionArgs resp_delay_us_dist_args =
100105
ParseDistributionArgs(resp_delay_us_dist, "<Response delay distribution>",
101106
0);
102107

103108
grpc::ServerBuilder builder;
104109

105-
for (const auto& port_str : ports) {
110+
for (int i = 0; i < ports.size(); ++i) {
111+
std::string& port_str = ports[i];
106112
if (port_str.empty()) {
107113
continue;
108114
}
@@ -112,8 +118,9 @@ std::unique_ptr<GRPCServer> CreateAndStartServer(
112118
continue;
113119
}
114120

121+
int* selected_port = selected_ports ? &(*selected_ports)[i] : nullptr;
115122
builder.AddListeningPort(HostPortString("::", port),
116-
grpc::InsecureServerCredentials());
123+
grpc::InsecureServerCredentials(), selected_port);
117124
}
118125

119126
fleetbench::rpc::GRPCServerOptions opts;

fleetbench/rpc/rpcperf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ namespace fleetbench::rpc {
3030
std::unique_ptr<GRPCServer> CreateAndStartServer(
3131
std::vector<std::string> ports, int32_t workers, bool compress,
3232
bool checksum, std::string logstats_output_path,
33-
std::string resp_delay_us_dist, absl::string_view program);
33+
std::string resp_delay_us_dist, absl::string_view program,
34+
std::vector<int>* selected_ports = nullptr);
3435

3536
std::unique_ptr<GRPCClient> CreateAndStartClient(
3637
int32_t max_outstanding_rpcs, bool compress, bool checksum,

fleetbench/rpc/rpcperf_benchmark.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <memory>
1616
#include <string>
1717
#include <utility>
18+
#include <vector>
1819

1920
#include "absl/log/check.h"
2021
#include "absl/strings/str_cat.h"
@@ -37,18 +38,26 @@ void BM_Rpc(benchmark::State &state, absl::string_view program) {
3738
CHECK(fleetbench::rpc::kPrograms->count(program))
3839
<< "Invalid program name \"" << program << "\" provided to benchmark";
3940

41+
// Requesting port 0 causes the OS to select a random unused port.
42+
std::vector<std::string> ports = {"0"};
43+
std::vector<int> selected_ports(ports.size());
44+
4045
std::unique_ptr<fleetbench::rpc::GRPCServer> server =
4146
fleetbench::rpc::CreateAndStartServer(
42-
/*ports=*/{"10000"}, /*workers=*/1, /*compress=*/false,
47+
/*ports=*/ports, /*workers=*/1, /*compress=*/false,
4348
/*checksum=*/false, /*logstats_output_path=*/"",
44-
/*resp_delay_us_dist=*/"", /*program=*/program);
49+
/*resp_delay_us_dist=*/"", /*program=*/program,
50+
/*selected_ports=*/&selected_ports);
51+
52+
std::vector<std::string> peers = {
53+
absl::StrCat("localhost:", selected_ports[0])};
4554

4655
absl::Mutex keep_running_mtx;
4756
std::unique_ptr<fleetbench::rpc::GRPCClient> client =
4857
fleetbench::rpc::CreateAndStartClient(
4958
/*max_outstanding_rpcs=*/1, /*compress=*/false,
5059
/*checksum=*/false, /*skip_loopback=*/false,
51-
/*peers=*/{"localhost:10000"}, /*max_peers=*/-1,
60+
/*peers=*/peers, /*max_peers=*/-1,
5261
/*connections_per_peer=*/1, /*logstats_output_path=*/"",
5362
/*req_delay_us_dist=*/"", /*program=*/program,
5463
/*keep_running=*/[&state, &keep_running_mtx]() {

0 commit comments

Comments
 (0)