foundationdb/fdbserver/networktest.actor.cpp

236 lines
6.3 KiB
C++
Raw Normal View History

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
*
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
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
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.
*/
#include "fdbserver/NetworkTest.h"
#include "flow/Knobs.h"
#include "flow/actorcompiler.h" // This must be the last #include.
2017-05-26 04:48:44 +08:00
UID WLTOKEN_NETWORKTEST( -1, 2 );
2020-02-20 02:55:05 +08:00
struct LatencyStats {
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)); }
};
2017-05-26 04:48:44 +08:00
NetworkTestInterface::NetworkTestInterface( NetworkAddress remote )
: test( Endpoint({remote}, WLTOKEN_NETWORKTEST) )
2017-05-26 04:48:44 +08:00
{
}
NetworkTestInterface::NetworkTestInterface( INetwork* local )
{
test.makeWellKnownEndpoint( WLTOKEN_NETWORKTEST, TaskPriority::DefaultEndpoint );
2017-05-26 04:48:44 +08:00
}
ACTOR Future<Void> networkTestServer() {
state NetworkTestInterface interf( g_network );
state Future<Void> logging = delay( 1.0 );
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 {
when( NetworkTestRequest req = waitNext( interf.test.getFuture() ) ) {
2020-02-20 02:55:05 +08:00
LatencyStats::sample sample = latency.tick();
2017-05-26 04:48:44 +08:00
req.reply.send( NetworkTestReply( Value( std::string( req.replySize, '.' ) ) ) );
latency.tock(sample);
2017-05-26 04:48:44 +08:00
sent++;
}
when( wait( logging ) ) {
2017-05-26 04:48:44 +08:00
auto spd = sent / (now() - lastTime);
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);
} else {
2020-02-20 02:55:05 +08:00
fprintf(stderr, "responses per second: %f (%f us)\n", spd, latency.mean() * 1e6);
}
latency.reset();
2017-05-26 04:48:44 +08:00
lastTime = now();
sent = 0;
logging = delay( 1.0 );
}
}
}
}
2020-02-20 02:55:05 +08:00
static bool moreRequestsPending(int count) {
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) {
if (FLOW_KNOBS->NETWORK_TEST_SCRIPT_MODE) {
return iteration <= 2;
} else {
2020-02-20 02:55:05 +08:00
return moreRequestsPending(count);
}
}
2020-02-20 02:55:05 +08:00
ACTOR Future<Void> testClient(std::vector<NetworkTestInterface> interfs, int* sent, int* completed,
LatencyStats* latency) {
state std::string request_payload(FLOW_KNOBS->NETWORK_TEST_REQUEST_SIZE, '.');
state LatencyStats::sample sample;
2020-02-20 02:55:05 +08:00
while (moreRequestsPending(*sent)) {
2017-05-26 04:48:44 +08:00
(*sent)++;
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)));
latency->tock(sample);
(*completed)++;
2017-05-26 04:48:44 +08:00
}
return Void();
2017-05-26 04:48:44 +08:00
}
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();
state int logged = 0;
state int iteration = 0;
2020-02-20 02:55:05 +08:00
while (moreLoggingNeeded(logged, ++iteration)) {
wait( delay(1.0) );
2020-02-20 02:55:05 +08:00
auto spd = (*completed - logged) / (now() - lastTime);
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);
}
} else {
2020-02-20 02:55:05 +08:00
fprintf(stderr, "messages per second: %f (%6.3f us)\n", spd, latency->mean() * 1e6);
}
latency->reset();
2017-05-26 04:48:44 +08:00
lastTime = now();
logged = *completed;
2017-05-26 04:48:44 +08:00
}
// tell the clients to shut down
*sent = -1;
return Void();
2017-05-26 04:48:44 +08:00
}
static void networkTestnanosleep()
{
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;
nanosleep(&tv, NULL);
double after = timer_monotonic();
printf(" %0.3lf", (after - before)*1e6);
}
printf("\nnanosleep(10) latency after 5ms spin:");
for (int i = 0; i < 10; i++) {
double a = timer_monotonic() + 5e-3;
2018-04-07 07:50:58 +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;
nanosleep(&tv, NULL);
double after = timer_monotonic();
printf(" %0.3lf", (after - before)*1e6);
}
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;
nanosleep(&tv, NULL);
double after = timer_monotonic();
printf(" %0.3lf", (after - before)*1e6);
}
printf("\n");
printf("nanosleep(20000) loop\n");
while (true) {
timespec tv;
tv.tv_sec = 0;
tv.tv_nsec = 20000;
nanosleep(&tv, NULL);
}
#endif
return;
}
ACTOR Future<Void> networkTestClient( std:: string testServers ) {
if (testServers == "nanosleep") {
networkTestnanosleep();
//return Void();
}
state std::vector<NetworkTestInterface> interfs;
state std::vector<NetworkAddress> servers = NetworkAddress::parseList(testServers);
state int sent = 0;
state int completed = 0;
2020-02-20 02:55:05 +08:00
state LatencyStats latency;
2017-05-26 04:48:44 +08:00
for( int i = 0; i < servers.size(); i++ ) {
interfs.push_back( NetworkTestInterface( servers[i] ) );
}
state std::vector<Future<Void>> clients;
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-20 02:55:05 +08:00
clients.push_back(logger(&sent, &completed, &latency));
2017-05-26 04:48:44 +08:00
wait( waitForAll( clients ) );
2017-05-26 04:48:44 +08:00
return Void();
2018-04-07 07:50:58 +08:00
}