Skip to content

Commit

Permalink
tpc/tcpperf support -p and -b options
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuo committed May 18, 2021
1 parent 4255e00 commit e921300
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 24 deletions.
18 changes: 11 additions & 7 deletions python/throughput-bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import socket, sys, time

def report(total_bytes : int, elapsed_seconds : float):
def report(total_bytes : int, elapsed_seconds : float, syscalls : int):
mbps = total_bytes / 1e6 / elapsed_seconds
print('Transferred %.3fMB in %.3fs, throughput %.3fMB/s %.3fMbits/s' %
(total_bytes / 1e6, elapsed_seconds, mbps, mbps * 8))
print('Transferred %.3fMB in %.3fs, throughput %.3fMB/s %.3fMbits/s, %d syscalls %.1f Bytes/syscall' %
(total_bytes / 1e6, elapsed_seconds, mbps, mbps * 8, syscalls, total_bytes / syscalls))


def print_buf(sock):
Expand All @@ -28,32 +28,36 @@ def run_sender(sock):
print('Sending... %s -> %s' % format_address(sock))
print_buf(sock)
start = time.time()
total = 0
count = 0
while True:
sock.sendall(buf)
total += sock.send(buf)
count += 1
if time.time() - start > 10:
break
total = len(buf) * count
print_buf(sock)
sent = time.time()
sock.shutdown(socket.SHUT_WR)
sock.recv(4096)
report(total, time.time() - start)
print('waited %.1fms' % ((time.time() - sent) * 1e3))
report(total, time.time() - start, count)


def run_receiver(sock):
print('Receiving... %s <- %s' % format_address(sock))
print_buf(sock)
start = time.time()
total = 0
count = 0
while True:
data = sock.recv(65536)
if not data:
break
total += len(data)
count += 1
print_buf(sock)
sock.close()
report(total, time.time() - start)
report(total, time.time() - start, count)


if __name__ == '__main__':
Expand Down
75 changes: 58 additions & 17 deletions tpc/bin/tcpperf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ class BandwidthReporter

void reportDelta(double now, int64_t total_bytes)
{
report(now, total_bytes, total_bytes - last_bytes_, now - last_time_);
report(now, total_bytes - last_bytes_, now - last_time_);
last_time_ = now;
last_bytes_ = total_bytes;
}

void reportAll(double now, int64_t total_bytes)
void reportAll(double now, int64_t total_bytes, int64_t syscalls)
{
report(now, total_bytes, total_bytes, now);
printf("Transferred %.3fMB %.3fMiB in %.3fs, %lld syscalls, %.1f Bytes/syscall\n",
total_bytes / 1e6, total_bytes / (1024.0 * 1024), now, (long long)syscalls,
total_bytes * 1.0 / syscalls);
report(now, total_bytes, now);
}

private:
void report(double now, int64_t total_bytes, int64_t bytes, double elapsed)
void report(double now, int64_t bytes, double elapsed)
{
printf("%6.3f %.3fM %.3fM/s ", now, total_bytes / 1e6,
elapsed > 0 ? bytes / 1e6 / elapsed : 0.0);
double mbps = elapsed > 0 ? bytes / 1e6 / elapsed : 0.0;
printf("%6.3f %6.2fMB/s %6.1fMbits/s ", now, mbps, mbps*8);
if (sender_)
printSender();
else
Expand Down Expand Up @@ -108,21 +111,26 @@ void runClient(const InetAddress& serverAddr, int64_t bytes_limit, double durati
perror("");
return;
}
printf("Connected %s -> %s\n", stream->getLocalAddr().toIpPort().c_str(),
stream->getPeerAddr().toIpPort().c_str());

const Timestamp start = Timestamp::now();
const int block_size = 64 * 1024;
std::string message(block_size, 'S');
int seconds = 1;
int64_t total_bytes = 0;
int64_t syscalls = 0;
double elapsed = 0;
BandwidthReporter rpt(stream->fd(), true);
rpt.reportAll(0, 0);
rpt.reportDelta(0, 0);

while (total_bytes < bytes_limit) {
int nw = stream->sendAll(message.data(), message.size());
int bytes = std::min<int64_t>(message.size(), bytes_limit - total_bytes);
int nw = stream->sendSome(message.data(), bytes);
if (nw <= 0)
break;
total_bytes += nw;
syscalls++;
elapsed = timeDifference(Timestamp::now(), start);

if (elapsed >= duration)
Expand All @@ -146,7 +154,7 @@ void runClient(const InetAddress& serverAddr, int64_t bytes_limit, double durati
printf("nr = %d\n", nr);
Timestamp end = Timestamp::now();
elapsed = timeDifference(end, start);
rpt.reportAll(elapsed, total_bytes);
rpt.reportAll(elapsed, total_bytes, syscalls);
}

void runServer(int port)
Expand All @@ -158,22 +166,25 @@ void runServer(int port)
printf("Accepting on port %d ... Ctrl-C to exit\n", port);
TcpStreamPtr stream = acceptor.accept();
++count;
printf("accepted no. %d client from %s\n", count,
printf("accepted no. %d client %s <- %s\n", count,
stream->getLocalAddr().toIpPort().c_str(),
stream->getPeerAddr().toIpPort().c_str());

const Timestamp start = Timestamp::now();
int seconds = 1;
int64_t bytes = 0;
int64_t syscalls = 0;
double elapsed = 0;
BandwidthReporter rpt(stream->fd(), false);
rpt.reportAll(elapsed, bytes);
rpt.reportDelta(elapsed, bytes);

char buf[65536];
while (true) {
int nr = stream->receiveSome(buf, sizeof buf);
if (nr <= 0)
break;
bytes += nr;
syscalls++;

elapsed = timeDifference(Timestamp::now(), start);
if (elapsed >= seconds) {
Expand All @@ -183,41 +194,71 @@ void runServer(int port)
}
}
elapsed = timeDifference(Timestamp::now(), start);
rpt.reportAll(elapsed, bytes);
rpt.reportAll(elapsed, bytes, syscalls);
printf("Client no. %d done\n", count);
}
}

int64_t parseBytes(const char* arg)
{
char* end = NULL;
int64_t bytes = strtoll(arg, &end, 10);
switch (*end) {
case '\0':
return bytes;
case 'k':
return bytes * 1000;
case 'K':
return bytes * 1024;
case 'm':
return bytes * 1000 * 1000;
case 'M':
return bytes * 1024 * 1024;
case 'g':
return bytes * 1000 * 1000 * 1000;
case 'G':
return bytes * 1024 * 1024 * 1024;
default:
return 0;
}
}

int main(int argc, char* argv[])
{
int opt;
bool client = false, server = false;
InetAddress serverAddr;
const int port = 2009;
std::string serverAddr;
int port = 2009;
const int64_t kGigaBytes = 1024 * 1024 * 1024;
int64_t bytes_limit = 10 * kGigaBytes;
double duration = 10;

while ((opt = getopt(argc, argv, "sc:t:")) != -1) {
while ((opt = getopt(argc, argv, "sc:t:b:p:")) != -1) {
switch (opt) {
case 's':
server = true;
break;
case 'c':
client = true;
serverAddr = InetAddress(optarg, port);
serverAddr = optarg;
break;
case 't':
duration = strtod(optarg, NULL);
break;
case 'b':
bytes_limit = parseBytes(optarg);
break;
case 'p':
port = strtol(optarg, NULL, 10);
break;
default:
fprintf(stderr, "Usage: %s FIXME\n", argv[0]);
break;
}
}

if (client)
runClient(serverAddr, bytes_limit, duration);
runClient(InetAddress(serverAddr, port), bytes_limit, duration);
else if (server)
runServer(port);
}

0 comments on commit e921300

Please sign in to comment.