2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* networktest.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-11-18 06:45:48 +08:00
|
|
|
#include "contrib/fmt-8.0.1/include/fmt/format.h"
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbserver/NetworkTest.h"
|
2019-08-22 09:37:50 +08:00
|
|
|
#include "flow/Knobs.h"
|
2021-03-11 02:06:03 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
2020-06-22 18:37:16 +08:00
|
|
|
#include "flow/ActorCollection.h"
|
|
|
|
#include "flow/UnitTest.h"
|
|
|
|
#include <inttypes.h>
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-09-21 00:46:34 +08:00
|
|
|
constexpr int WLTOKEN_NETWORKTEST = WLTOKEN_FIRST_AVAILABLE;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-02-20 02:55:05 +08:00
|
|
|
struct LatencyStats {
|
2020-02-11 07:03:35 +08:00
|
|
|
using sample = double;
|
|
|
|
double x = 0;
|
|
|
|
double x2 = 0;
|
|
|
|
double n = 0;
|
|
|
|
|
|
|
|
sample tick() {
|
|
|
|
// now() returns the timestamp when we were scheduled; count
|
|
|
|
// all that time against this sample.
|
|
|
|
return now();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tock(sample tick) {
|
|
|
|
// time_monotonic returns the timestamp when it was called;
|
|
|
|
// count the time it took us to be dispatched and invoke
|
|
|
|
// timer_monotonic
|
|
|
|
double delta = timer_monotonic() - tick;
|
|
|
|
x += delta;
|
|
|
|
x2 += (delta * delta);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2020-02-20 02:55:05 +08:00
|
|
|
void reset() { *this = LatencyStats(); }
|
|
|
|
double count() { return n; }
|
|
|
|
double mean() { return x / n; }
|
|
|
|
double stddev() { return sqrt(x2 / n - (x / n) * (x / n)); }
|
2020-02-11 07:03:35 +08:00
|
|
|
};
|
|
|
|
|
2021-09-21 00:46:34 +08:00
|
|
|
NetworkTestInterface::NetworkTestInterface(NetworkAddress remote)
|
|
|
|
: test(Endpoint::wellKnown({ remote }, WLTOKEN_NETWORKTEST)) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
NetworkTestInterface::NetworkTestInterface(INetwork* local) {
|
|
|
|
test.makeWellKnownEndpoint(WLTOKEN_NETWORKTEST, TaskPriority::DefaultEndpoint);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Void> networkTestServer() {
|
2021-03-11 02:06:03 +08:00
|
|
|
state NetworkTestInterface interf(g_network);
|
|
|
|
state Future<Void> logging = delay(1.0);
|
2017-05-26 04:48:44 +08:00
|
|
|
state double lastTime = now();
|
|
|
|
state int sent = 0;
|
2020-02-20 02:55:05 +08:00
|
|
|
state LatencyStats latency;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
loop {
|
|
|
|
choose {
|
2021-03-11 02:06:03 +08:00
|
|
|
when(NetworkTestRequest req = waitNext(interf.test.getFuture())) {
|
2020-02-20 02:55:05 +08:00
|
|
|
LatencyStats::sample sample = latency.tick();
|
2021-03-11 02:06:03 +08:00
|
|
|
req.reply.send(NetworkTestReply(Value(std::string(req.replySize, '.'))));
|
2020-02-11 07:03:35 +08:00
|
|
|
latency.tock(sample);
|
2017-05-26 04:48:44 +08:00
|
|
|
sent++;
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
when(wait(logging)) {
|
2017-05-26 04:48:44 +08:00
|
|
|
auto spd = sent / (now() - lastTime);
|
2020-02-11 07:03:35 +08:00
|
|
|
if (FLOW_KNOBS->NETWORK_TEST_SCRIPT_MODE) {
|
2020-02-20 02:55:05 +08:00
|
|
|
fprintf(stderr, "%f\t%.3f\t%.3f\n", spd, latency.mean() * 1e6, latency.stddev() * 1e6);
|
2020-02-11 07:03:35 +08:00
|
|
|
} else {
|
2020-02-20 02:55:05 +08:00
|
|
|
fprintf(stderr, "responses per second: %f (%f us)\n", spd, latency.mean() * 1e6);
|
2020-02-11 07:03:35 +08:00
|
|
|
}
|
|
|
|
latency.reset();
|
2017-05-26 04:48:44 +08:00
|
|
|
lastTime = now();
|
|
|
|
sent = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
logging = delay(1.0);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 00:47:13 +08:00
|
|
|
ACTOR Future<Void> networkTestStreamingServer() {
|
2021-08-28 08:07:47 +08:00
|
|
|
state NetworkTestInterface interf(g_network);
|
|
|
|
state Future<Void> logging = delay(1.0);
|
2021-06-20 00:47:13 +08:00
|
|
|
state double lastTime = now();
|
|
|
|
state int sent = 0;
|
|
|
|
state LatencyStats latency;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
choose {
|
|
|
|
when(state NetworkTestStreamingRequest req = waitNext(interf.testStream.getFuture())) {
|
|
|
|
state LatencyStats::sample sample = latency.tick();
|
|
|
|
state int i = 0;
|
|
|
|
for (; i < 100; ++i) {
|
|
|
|
wait(req.reply.onReady());
|
|
|
|
req.reply.send(NetworkTestStreamingReply{ i });
|
|
|
|
}
|
|
|
|
req.reply.sendError(end_of_stream());
|
|
|
|
latency.tock(sample);
|
|
|
|
sent++;
|
|
|
|
}
|
2021-08-28 08:07:47 +08:00
|
|
|
when(wait(logging)) {
|
2021-06-20 00:47:13 +08:00
|
|
|
auto spd = sent / (now() - lastTime);
|
|
|
|
if (FLOW_KNOBS->NETWORK_TEST_SCRIPT_MODE) {
|
|
|
|
fprintf(stderr, "%f\t%.3f\t%.3f\n", spd, latency.mean() * 1e6, latency.stddev() * 1e6);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "responses per second: %f (%f us)\n", spd, latency.mean() * 1e6);
|
|
|
|
}
|
|
|
|
latency.reset();
|
|
|
|
lastTime = now();
|
|
|
|
sent = 0;
|
2021-08-28 08:07:47 +08:00
|
|
|
logging = delay(1.0);
|
2021-06-20 00:47:13 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-28 08:07:47 +08:00
|
|
|
} catch (Error& e) {
|
|
|
|
if (e.code() != error_code_operation_obsolete) {
|
2021-06-20 00:47:13 +08:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 02:55:05 +08:00
|
|
|
static bool moreRequestsPending(int count) {
|
2020-02-11 07:03:35 +08:00
|
|
|
if (count == -1) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
int request_count = FLOW_KNOBS->NETWORK_TEST_REQUEST_COUNT;
|
|
|
|
return (!request_count) || count < request_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 02:55:05 +08:00
|
|
|
static bool moreLoggingNeeded(int count, int iteration) {
|
2020-02-11 07:03:35 +08:00
|
|
|
if (FLOW_KNOBS->NETWORK_TEST_SCRIPT_MODE) {
|
|
|
|
return iteration <= 2;
|
|
|
|
} else {
|
2020-02-20 02:55:05 +08:00
|
|
|
return moreRequestsPending(count);
|
2020-02-11 07:03:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> testClient(std::vector<NetworkTestInterface> interfs,
|
|
|
|
int* sent,
|
|
|
|
int* completed,
|
2020-02-20 02:55:05 +08:00
|
|
|
LatencyStats* latency) {
|
|
|
|
state std::string request_payload(FLOW_KNOBS->NETWORK_TEST_REQUEST_SIZE, '.');
|
|
|
|
state LatencyStats::sample sample;
|
2020-02-11 07:03:35 +08:00
|
|
|
|
2020-02-20 02:55:05 +08:00
|
|
|
while (moreRequestsPending(*sent)) {
|
2017-05-26 04:48:44 +08:00
|
|
|
(*sent)++;
|
2020-02-11 07:03:35 +08:00
|
|
|
sample = latency->tick();
|
2020-02-20 02:55:05 +08:00
|
|
|
NetworkTestReply rep = wait(
|
|
|
|
retryBrokenPromise(interfs[deterministicRandom()->randomInt(0, interfs.size())].test,
|
|
|
|
NetworkTestRequest(StringRef(request_payload), FLOW_KNOBS->NETWORK_TEST_REPLY_SIZE)));
|
2020-02-11 07:03:35 +08:00
|
|
|
latency->tock(sample);
|
|
|
|
(*completed)++;
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2020-02-11 07:03:35 +08:00
|
|
|
return Void();
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-08-28 08:07:47 +08:00
|
|
|
ACTOR Future<Void> testClientStream(std::vector<NetworkTestInterface> interfs,
|
|
|
|
int* sent,
|
|
|
|
int* completed,
|
|
|
|
LatencyStats* latency) {
|
2021-06-20 00:47:13 +08:00
|
|
|
state std::string request_payload(FLOW_KNOBS->NETWORK_TEST_REQUEST_SIZE, '.');
|
|
|
|
state LatencyStats::sample sample;
|
|
|
|
|
|
|
|
while (moreRequestsPending(*sent)) {
|
|
|
|
(*sent)++;
|
|
|
|
sample = latency->tick();
|
|
|
|
state ReplyPromiseStream<NetworkTestStreamingReply> stream =
|
|
|
|
interfs[deterministicRandom()->randomInt(0, interfs.size())].testStream.getReplyStream(
|
|
|
|
NetworkTestStreamingRequest{});
|
|
|
|
state int j = 0;
|
|
|
|
try {
|
|
|
|
loop {
|
|
|
|
NetworkTestStreamingReply rep = waitNext(stream.getFuture());
|
|
|
|
ASSERT(rep.index == j++);
|
|
|
|
}
|
|
|
|
} catch (Error& e) {
|
|
|
|
ASSERT(e.code() == error_code_end_of_stream || e.code() == error_code_connection_failed);
|
|
|
|
}
|
|
|
|
latency->tock(sample);
|
|
|
|
(*completed)++;
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-02-20 02:55:05 +08:00
|
|
|
ACTOR Future<Void> logger(int* sent, int* completed, LatencyStats* latency) {
|
2017-05-26 04:48:44 +08:00
|
|
|
state double lastTime = now();
|
2020-02-11 07:03:35 +08:00
|
|
|
state int logged = 0;
|
|
|
|
state int iteration = 0;
|
2020-02-20 02:55:05 +08:00
|
|
|
while (moreLoggingNeeded(logged, ++iteration)) {
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(delay(1.0));
|
2020-02-20 02:55:05 +08:00
|
|
|
auto spd = (*completed - logged) / (now() - lastTime);
|
2020-02-11 07:03:35 +08:00
|
|
|
if (FLOW_KNOBS->NETWORK_TEST_SCRIPT_MODE) {
|
|
|
|
if (iteration == 2) {
|
|
|
|
// We don't report the first iteration because of warm-up effects.
|
2020-02-20 02:55:05 +08:00
|
|
|
printf("%f\t%.3f\t%.3f\n", spd, latency->mean() * 1e6, latency->stddev() * 1e6);
|
2020-02-11 07:03:35 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-20 02:55:05 +08:00
|
|
|
fprintf(stderr, "messages per second: %f (%6.3f us)\n", spd, latency->mean() * 1e6);
|
2020-02-11 07:03:35 +08:00
|
|
|
}
|
|
|
|
latency->reset();
|
2017-05-26 04:48:44 +08:00
|
|
|
lastTime = now();
|
2020-02-11 07:03:35 +08:00
|
|
|
logged = *completed;
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2020-02-11 07:03:35 +08:00
|
|
|
// tell the clients to shut down
|
|
|
|
*sent = -1;
|
|
|
|
return Void();
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static void networkTestnanosleep() {
|
2017-05-26 04:48:44 +08:00
|
|
|
printf("nanosleep speed test\n");
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
printf("\nnanosleep(10) latencies:");
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
|
|
|
double before = timer_monotonic();
|
|
|
|
timespec tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_nsec = 10;
|
2020-08-19 05:18:50 +08:00
|
|
|
nanosleep(&tv, nullptr);
|
2017-05-26 04:48:44 +08:00
|
|
|
double after = timer_monotonic();
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
printf(" %0.3lf", (after - before) * 1e6);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nnanosleep(10) latency after 5ms spin:");
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
double a = timer_monotonic() + 5e-3;
|
2021-03-11 02:06:03 +08:00
|
|
|
while (timer_monotonic() < a) {
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
double before = timer_monotonic();
|
|
|
|
timespec tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_nsec = 10;
|
2020-08-19 05:18:50 +08:00
|
|
|
nanosleep(&tv, nullptr);
|
2017-05-26 04:48:44 +08:00
|
|
|
double after = timer_monotonic();
|
2021-03-11 02:06:03 +08:00
|
|
|
printf(" %0.3lf", (after - before) * 1e6);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nnanosleep(20000) latency:");
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
double before = timer_monotonic();
|
|
|
|
timespec tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_nsec = 20000;
|
2020-08-19 05:18:50 +08:00
|
|
|
nanosleep(&tv, nullptr);
|
2017-05-26 04:48:44 +08:00
|
|
|
double after = timer_monotonic();
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
printf(" %0.3lf", (after - before) * 1e6);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("nanosleep(20000) loop\n");
|
|
|
|
while (true) {
|
|
|
|
timespec tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_nsec = 20000;
|
2020-08-19 05:18:50 +08:00
|
|
|
nanosleep(&tv, nullptr);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> networkTestClient(std::string testServers) {
|
2017-05-26 04:48:44 +08:00
|
|
|
if (testServers == "nanosleep") {
|
|
|
|
networkTestnanosleep();
|
2021-03-11 02:06:03 +08:00
|
|
|
// return Void();
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
state std::vector<NetworkTestInterface> interfs;
|
|
|
|
state std::vector<NetworkAddress> servers = NetworkAddress::parseList(testServers);
|
|
|
|
state int sent = 0;
|
2020-02-11 07:03:35 +08:00
|
|
|
state int completed = 0;
|
2020-02-20 02:55:05 +08:00
|
|
|
state LatencyStats latency;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-04 11:36:21 +08:00
|
|
|
interfs.reserve(servers.size());
|
|
|
|
for (int i = 0; i < servers.size(); i++) {
|
2021-03-11 02:06:03 +08:00
|
|
|
interfs.push_back(NetworkTestInterface(servers[i]));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
state std::vector<Future<Void>> clients;
|
2021-03-04 11:36:21 +08:00
|
|
|
clients.reserve(FLOW_KNOBS->NETWORK_TEST_CLIENT_COUNT);
|
2020-02-20 02:55:05 +08:00
|
|
|
for (int i = 0; i < FLOW_KNOBS->NETWORK_TEST_CLIENT_COUNT; i++) {
|
|
|
|
clients.push_back(testClient(interfs, &sent, &completed, &latency));
|
2020-02-11 07:03:35 +08:00
|
|
|
}
|
2020-02-20 02:55:05 +08:00
|
|
|
clients.push_back(logger(&sent, &completed, &latency));
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(waitForAll(clients));
|
2017-05-26 04:48:44 +08:00
|
|
|
return Void();
|
2018-04-07 07:50:58 +08:00
|
|
|
}
|
2020-06-22 18:37:16 +08:00
|
|
|
|
|
|
|
struct RandomIntRange {
|
|
|
|
int min;
|
|
|
|
int max;
|
2020-06-26 11:44:43 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
RandomIntRange(int low = 0, int high = 0) : min(low), max(high) {}
|
2020-06-26 11:44:43 +08:00
|
|
|
|
|
|
|
// Accepts strings of the form "min:max" or "N"
|
|
|
|
// where N will be used for both min and max
|
|
|
|
RandomIntRange(std::string str) {
|
|
|
|
StringRef high = str;
|
|
|
|
StringRef low = high.eat(":");
|
2021-03-11 02:06:03 +08:00
|
|
|
if (high.size() == 0) {
|
2020-06-26 11:44:43 +08:00
|
|
|
high = low;
|
|
|
|
}
|
|
|
|
min = low.size() == 0 ? 0 : atol(low.toString().c_str());
|
|
|
|
max = high.size() == 0 ? 0 : atol(high.toString().c_str());
|
2021-03-11 02:06:03 +08:00
|
|
|
if (min > max) {
|
2020-06-26 11:44:43 +08:00
|
|
|
std::swap(min, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
int get() const { return (max == 0) ? 0 : nondeterministicRandom()->randomInt(min, max + 1); }
|
2020-06-26 11:44:43 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
std::string toString() const { return format("%d:%d", min, max); }
|
2020-06-22 18:37:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct P2PNetworkTest {
|
2020-06-26 11:44:43 +08:00
|
|
|
// Addresses to listen on
|
2020-06-22 18:37:16 +08:00
|
|
|
std::vector<Reference<IListener>> listeners;
|
2020-06-26 11:44:43 +08:00
|
|
|
// Addresses to randomly connect to
|
2020-06-22 18:37:16 +08:00
|
|
|
std::vector<NetworkAddress> remotes;
|
2020-06-26 11:44:43 +08:00
|
|
|
// Number of outgoing connections to maintain
|
2020-06-22 18:37:16 +08:00
|
|
|
int connectionsOut;
|
2020-06-26 11:44:43 +08:00
|
|
|
// Message size range to send on outgoing established connections
|
2020-07-07 06:14:53 +08:00
|
|
|
RandomIntRange requestBytes;
|
|
|
|
// Message size to reply with on incoming established connections
|
|
|
|
RandomIntRange replyBytes;
|
|
|
|
// Number of requests/replies per session
|
|
|
|
RandomIntRange requests;
|
2020-06-26 11:44:43 +08:00
|
|
|
// Delay after message send and receive are complete before closing connection
|
2020-06-22 18:37:16 +08:00
|
|
|
RandomIntRange idleMilliseconds;
|
2020-06-26 11:44:43 +08:00
|
|
|
// Random delay before socket reads
|
|
|
|
RandomIntRange waitReadMilliseconds;
|
|
|
|
// Random delay before socket writes
|
|
|
|
RandomIntRange waitWriteMilliseconds;
|
2020-06-22 18:37:16 +08:00
|
|
|
|
|
|
|
double startTime;
|
|
|
|
int64_t bytesSent;
|
|
|
|
int64_t bytesReceived;
|
|
|
|
int sessionsIn;
|
|
|
|
int sessionsOut;
|
2020-06-26 11:44:43 +08:00
|
|
|
int connectErrors;
|
|
|
|
int acceptErrors;
|
|
|
|
int sessionErrors;
|
|
|
|
|
2020-06-22 18:37:16 +08:00
|
|
|
Standalone<StringRef> msgBuffer;
|
|
|
|
|
2020-06-23 04:37:13 +08:00
|
|
|
std::string statsString() {
|
2020-06-22 18:37:16 +08:00
|
|
|
double elapsed = now() - startTime;
|
2021-03-11 02:06:03 +08:00
|
|
|
std::string s = format(
|
|
|
|
"%.2f MB/s bytes in %.2f MB/s bytes out %.2f/s completed sessions in %.2f/s completed sessions out ",
|
|
|
|
bytesReceived / elapsed / 1e6,
|
|
|
|
bytesSent / elapsed / 1e6,
|
|
|
|
sessionsIn / elapsed,
|
|
|
|
sessionsOut / elapsed);
|
2020-06-26 11:44:43 +08:00
|
|
|
s += format("Total Errors %d connect=%d accept=%d session=%d",
|
2021-03-11 02:06:03 +08:00
|
|
|
connectErrors + acceptErrors + sessionErrors,
|
|
|
|
connectErrors,
|
|
|
|
acceptErrors,
|
|
|
|
sessionErrors);
|
2020-06-23 04:37:13 +08:00
|
|
|
bytesSent = 0;
|
|
|
|
bytesReceived = 0;
|
|
|
|
sessionsIn = 0;
|
|
|
|
sessionsOut = 0;
|
|
|
|
startTime = now();
|
|
|
|
return s;
|
2020-06-22 18:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
P2PNetworkTest() {}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
P2PNetworkTest(std::string listenerAddresses,
|
|
|
|
std::string remoteAddresses,
|
|
|
|
int connectionsOut,
|
|
|
|
RandomIntRange sendMsgBytes,
|
|
|
|
RandomIntRange recvMsgBytes,
|
|
|
|
RandomIntRange requests,
|
|
|
|
RandomIntRange idleMilliseconds,
|
|
|
|
RandomIntRange waitReadMilliseconds,
|
|
|
|
RandomIntRange waitWriteMilliseconds)
|
|
|
|
: connectionsOut(connectionsOut), requestBytes(sendMsgBytes), replyBytes(recvMsgBytes), requests(requests),
|
|
|
|
idleMilliseconds(idleMilliseconds), waitReadMilliseconds(waitReadMilliseconds),
|
|
|
|
waitWriteMilliseconds(waitWriteMilliseconds) {
|
2020-06-22 18:37:16 +08:00
|
|
|
bytesSent = 0;
|
|
|
|
bytesReceived = 0;
|
|
|
|
sessionsIn = 0;
|
|
|
|
sessionsOut = 0;
|
2020-06-26 11:44:43 +08:00
|
|
|
connectErrors = 0;
|
|
|
|
acceptErrors = 0;
|
|
|
|
sessionErrors = 0;
|
|
|
|
msgBuffer = makeString(std::max(sendMsgBytes.max, recvMsgBytes.max));
|
2020-06-22 18:37:16 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!remoteAddresses.empty()) {
|
2020-06-22 18:37:16 +08:00
|
|
|
remotes = NetworkAddress::parseList(remoteAddresses);
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!listenerAddresses.empty()) {
|
|
|
|
for (auto a : NetworkAddress::parseList(listenerAddresses)) {
|
2020-06-22 18:37:16 +08:00
|
|
|
listeners.push_back(INetworkConnections::net()->listen(a));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
NetworkAddress randomRemote() { return remotes[nondeterministicRandom()->randomInt(0, remotes.size())]; }
|
2020-06-22 18:37:16 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR static Future<Standalone<StringRef>> readMsg(P2PNetworkTest* self, Reference<IConnection> conn) {
|
2020-07-07 06:14:53 +08:00
|
|
|
state Standalone<StringRef> buffer = makeString(sizeof(int));
|
2020-06-22 18:37:16 +08:00
|
|
|
state int writeOffset = 0;
|
|
|
|
state bool gotHeader = false;
|
|
|
|
|
2020-06-22 20:15:24 +08:00
|
|
|
// Fill buffer sequentially until the initial bytesToRead is read (or more), then read
|
|
|
|
// intended message size and add it to bytesToRead, continue if needed until bytesToRead is 0.
|
2020-06-22 18:37:16 +08:00
|
|
|
loop {
|
2020-06-26 11:44:43 +08:00
|
|
|
int stutter = self->waitReadMilliseconds.get();
|
2021-03-11 02:06:03 +08:00
|
|
|
if (stutter > 0) {
|
2020-06-26 11:44:43 +08:00
|
|
|
wait(delay(stutter / 1e3));
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
int len = conn->read((uint8_t*)buffer.begin() + writeOffset, (uint8_t*)buffer.end());
|
2020-06-22 20:15:24 +08:00
|
|
|
writeOffset += len;
|
2020-07-07 06:14:53 +08:00
|
|
|
self->bytesReceived += len;
|
2020-06-22 20:15:24 +08:00
|
|
|
|
2020-07-07 06:14:53 +08:00
|
|
|
// If buffer is complete, either process it as a header or return it
|
2021-03-11 02:06:03 +08:00
|
|
|
if (writeOffset == buffer.size()) {
|
|
|
|
if (gotHeader) {
|
2020-07-07 06:14:53 +08:00
|
|
|
return buffer;
|
|
|
|
} else {
|
|
|
|
gotHeader = true;
|
2021-03-11 02:06:03 +08:00
|
|
|
int msgSize = *(int*)buffer.begin();
|
|
|
|
if (msgSize == 0) {
|
2020-07-07 06:14:53 +08:00
|
|
|
return Standalone<StringRef>();
|
|
|
|
}
|
|
|
|
buffer = makeString(msgSize);
|
|
|
|
writeOffset = 0;
|
2020-06-22 18:37:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (len == 0) {
|
2020-06-22 18:37:16 +08:00
|
|
|
wait(conn->onReadable());
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(delay(0, TaskPriority::ReadSocket));
|
2020-06-22 18:37:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR static Future<Void> writeMsg(P2PNetworkTest* self, Reference<IConnection> conn, StringRef msg) {
|
2020-06-22 18:37:16 +08:00
|
|
|
state UnsentPacketQueue packets;
|
2020-07-07 06:14:53 +08:00
|
|
|
PacketWriter writer(packets.getWriteBuffer(msg.size()), nullptr, Unversioned());
|
|
|
|
writer.serializeBinaryItem((int)msg.size());
|
|
|
|
writer.serializeBytes(msg);
|
2020-06-22 18:37:16 +08:00
|
|
|
|
|
|
|
loop {
|
2020-06-26 11:44:43 +08:00
|
|
|
int stutter = self->waitWriteMilliseconds.get();
|
2021-03-11 02:06:03 +08:00
|
|
|
if (stutter > 0) {
|
2020-06-26 11:44:43 +08:00
|
|
|
wait(delay(stutter / 1e3));
|
|
|
|
}
|
2020-07-16 17:31:17 +08:00
|
|
|
int sent = conn->write(packets.getUnsent(), FLOW_KNOBS->MAX_PACKET_SEND_BYTES);
|
2020-06-22 18:37:16 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (sent != 0) {
|
2020-06-26 11:44:43 +08:00
|
|
|
self->bytesSent += sent;
|
|
|
|
packets.sent(sent);
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (packets.empty()) {
|
2020-06-22 18:37:16 +08:00
|
|
|
break;
|
2020-06-26 11:44:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
wait(conn->onWritable());
|
2020-07-17 12:59:20 +08:00
|
|
|
wait(yield(TaskPriority::WriteSocket));
|
2020-06-22 18:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR static Future<Void> doSession(P2PNetworkTest* self, Reference<IConnection> conn, bool incoming) {
|
2020-07-07 06:14:53 +08:00
|
|
|
state int numRequests;
|
|
|
|
|
2020-06-22 18:37:16 +08:00
|
|
|
try {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (incoming) {
|
2020-06-23 04:37:13 +08:00
|
|
|
wait(conn->acceptHandshake());
|
2020-07-07 06:14:53 +08:00
|
|
|
|
|
|
|
// Read the number of requests for the session
|
|
|
|
Standalone<StringRef> buf = wait(readMsg(self, conn));
|
|
|
|
ASSERT(buf.size() == sizeof(int));
|
2021-03-11 02:06:03 +08:00
|
|
|
numRequests = *(int*)buf.begin();
|
2020-06-23 04:37:13 +08:00
|
|
|
} else {
|
|
|
|
wait(conn->connectHandshake());
|
2020-07-07 06:14:53 +08:00
|
|
|
|
|
|
|
// Pick the number of requests for the session and send it to remote
|
|
|
|
numRequests = self->requests.get();
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(writeMsg(self, conn, StringRef((const uint8_t*)&numRequests, sizeof(int))));
|
2020-06-23 04:37:13 +08:00
|
|
|
}
|
2020-07-07 06:14:53 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
while (numRequests > 0) {
|
|
|
|
if (incoming) {
|
2020-07-07 06:14:53 +08:00
|
|
|
// Wait for a request
|
|
|
|
wait(success(readMsg(self, conn)));
|
|
|
|
// Send a reply
|
|
|
|
wait(writeMsg(self, conn, self->msgBuffer.substr(0, self->replyBytes.get())));
|
2021-03-11 02:06:03 +08:00
|
|
|
} else {
|
2020-07-07 06:14:53 +08:00
|
|
|
// Send a request
|
|
|
|
wait(writeMsg(self, conn, self->msgBuffer.substr(0, self->requestBytes.get())));
|
|
|
|
// Wait for a reply
|
|
|
|
wait(success(readMsg(self, conn)));
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (--numRequests == 0) {
|
2020-07-07 06:14:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 18:37:16 +08:00
|
|
|
wait(delay(self->idleMilliseconds.get() / 1e3));
|
|
|
|
conn->close();
|
2020-07-07 06:14:53 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (incoming) {
|
2020-06-23 06:40:04 +08:00
|
|
|
++self->sessionsIn;
|
|
|
|
} else {
|
|
|
|
++self->sessionsOut;
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
} catch (Error& e) {
|
2020-06-26 11:44:43 +08:00
|
|
|
++self->sessionErrors;
|
|
|
|
TraceEvent(SevError, incoming ? "P2PIncomingSessionError" : "P2POutgoingSessionError")
|
2021-03-11 02:06:03 +08:00
|
|
|
.detail("Remote", conn->getPeerAddress())
|
|
|
|
.error(e);
|
2020-06-22 18:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR static Future<Void> outgoing(P2PNetworkTest* self) {
|
2020-06-22 18:37:16 +08:00
|
|
|
loop {
|
2020-06-22 20:15:24 +08:00
|
|
|
wait(delay(0, TaskPriority::WriteSocket));
|
2020-06-22 18:37:16 +08:00
|
|
|
state NetworkAddress remote = self->randomRemote();
|
2020-06-22 20:15:24 +08:00
|
|
|
|
2020-06-22 18:37:16 +08:00
|
|
|
try {
|
2020-06-22 20:15:24 +08:00
|
|
|
state Reference<IConnection> conn = wait(INetworkConnections::net()->connect(remote));
|
2021-03-11 02:06:03 +08:00
|
|
|
// printf("Connected to %s\n", remote.toString().c_str());
|
2020-06-23 04:37:13 +08:00
|
|
|
wait(doSession(self, conn, false));
|
2021-03-11 02:06:03 +08:00
|
|
|
} catch (Error& e) {
|
2020-06-26 11:44:43 +08:00
|
|
|
++self->connectErrors;
|
2021-03-11 02:06:03 +08:00
|
|
|
TraceEvent(SevError, "P2POutgoingError").detail("Remote", remote).error(e);
|
2020-06-26 11:44:43 +08:00
|
|
|
wait(delay(1));
|
2020-06-22 18:37:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR static Future<Void> incoming(P2PNetworkTest* self, Reference<IListener> listener) {
|
2020-06-22 18:37:16 +08:00
|
|
|
state ActorCollection sessions(false);
|
|
|
|
|
|
|
|
loop {
|
2020-06-22 20:15:24 +08:00
|
|
|
wait(delay(0, TaskPriority::AcceptSocket));
|
|
|
|
|
2020-06-22 18:37:16 +08:00
|
|
|
try {
|
2020-06-22 20:15:24 +08:00
|
|
|
state Reference<IConnection> conn = wait(listener->accept());
|
2021-03-11 02:06:03 +08:00
|
|
|
// printf("Connected from %s\n", conn->getPeerAddress().toString().c_str());
|
2020-06-23 04:37:13 +08:00
|
|
|
sessions.add(doSession(self, conn, true));
|
2021-03-11 02:06:03 +08:00
|
|
|
} catch (Error& e) {
|
2020-06-26 11:44:43 +08:00
|
|
|
++self->acceptErrors;
|
2021-03-11 02:06:03 +08:00
|
|
|
TraceEvent(SevError, "P2PIncomingError").detail("Listener", listener->getListenAddress()).error(e);
|
2020-06-22 18:37:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR static Future<Void> run_impl(P2PNetworkTest* self) {
|
2020-06-22 18:37:16 +08:00
|
|
|
state ActorCollection actors(false);
|
2020-06-22 20:15:24 +08:00
|
|
|
|
2020-06-22 18:37:16 +08:00
|
|
|
self->startTime = now();
|
|
|
|
|
2021-11-18 06:45:48 +08:00
|
|
|
fmt::print("{0} listeners, {1} remotes, {2} outgoing connections\n",
|
|
|
|
self->listeners.size(),
|
|
|
|
self->remotes.size(),
|
|
|
|
self->connectionsOut);
|
2020-06-22 18:37:16 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto n : self->remotes) {
|
2020-06-22 18:37:16 +08:00
|
|
|
printf("Remote: %s\n", n.toString().c_str());
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto el : self->listeners) {
|
2020-06-22 18:37:16 +08:00
|
|
|
printf("Listener: %s\n", el->getListenAddress().toString().c_str());
|
|
|
|
actors.add(incoming(self, el));
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:36:05 +08:00
|
|
|
printf("Request size: %s\n", self->requestBytes.toString().c_str());
|
|
|
|
printf("Response size: %s\n", self->replyBytes.toString().c_str());
|
|
|
|
printf("Requests per outgoing session: %s\n", self->requests.toString().c_str());
|
|
|
|
printf("Delay before socket read: %s\n", self->waitReadMilliseconds.toString().c_str());
|
|
|
|
printf("Delay before socket write: %s\n", self->waitWriteMilliseconds.toString().c_str());
|
|
|
|
printf("Delay before session close: %s\n", self->idleMilliseconds.toString().c_str());
|
|
|
|
printf("Send/Recv size %d bytes\n", FLOW_KNOBS->MAX_PACKET_SEND_BYTES);
|
|
|
|
|
|
|
|
if ((self->remotes.empty() || self->connectionsOut == 0) && self->listeners.empty()) {
|
|
|
|
printf("No listeners and no remotes or connectionsOut, so there is nothing to do!\n");
|
|
|
|
ASSERT((!self->remotes.empty() && (self->connectionsOut > 0)) || !self->listeners.empty());
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!self->remotes.empty()) {
|
|
|
|
for (int i = 0; i < self->connectionsOut; ++i) {
|
2020-06-22 20:15:24 +08:00
|
|
|
actors.add(outgoing(self));
|
|
|
|
}
|
2020-06-22 18:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
2020-06-26 11:44:43 +08:00
|
|
|
wait(delay(1.0, TaskPriority::Max));
|
2020-06-22 18:37:16 +08:00
|
|
|
printf("%s\n", self->statsString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
Future<Void> run() { return run_impl(this); }
|
2020-06-22 18:37:16 +08:00
|
|
|
};
|
|
|
|
|
2021-04-05 12:36:05 +08:00
|
|
|
// Peer-to-Peer network test.
|
|
|
|
// One or more instances can be run and set to talk to each other.
|
|
|
|
// Each instance
|
|
|
|
// - listens on 0 or more listenerAddresses
|
|
|
|
// - maintains 0 or more connectionsOut at a time, each to a random choice from remoteAddresses
|
|
|
|
// Address lists are a string of comma-separated IP:port[:tls] strings.
|
|
|
|
//
|
|
|
|
// The other arguments can be specified as "fixedValue" or "minValue:maxValue".
|
|
|
|
// Each outgoing connection will live for a random requests count.
|
|
|
|
// Each request will
|
|
|
|
// - send a random requestBytes sized message
|
|
|
|
// - wait for a random replyBytes sized response.
|
|
|
|
// The client will close the connection after a random idleMilliseconds.
|
|
|
|
// Reads and writes can optionally preceded by random delays, waitReadMilliseconds and waitWriteMilliseconds.
|
2021-04-05 15:03:15 +08:00
|
|
|
TEST_CASE(":/network/p2ptest") {
|
2021-04-08 09:14:44 +08:00
|
|
|
state P2PNetworkTest p2p(params.get("listenerAddresses").orDefault(""),
|
|
|
|
params.get("remoteAddresses").orDefault(""),
|
|
|
|
params.getInt("connectionsOut").orDefault(1),
|
|
|
|
params.get("requestBytes").orDefault("50:100"),
|
|
|
|
params.get("replyBytes").orDefault("500:1000"),
|
|
|
|
params.get("requests").orDefault("10:10000"),
|
|
|
|
params.get("idleMilliseconds").orDefault("0"),
|
|
|
|
params.get("waitReadMilliseconds").orDefault("0"),
|
|
|
|
params.get("waitWriteMilliseconds").orDefault("0"));
|
2020-06-22 18:37:16 +08:00
|
|
|
|
|
|
|
wait(p2p.run());
|
|
|
|
return Void();
|
|
|
|
}
|