TTCP(test TCP): classic TCP performance testing tool
1. What performance do we care?
1. Bandwith MB/s
2. Throughput, messages/s, queries/s(QPS), transactions/s (TPS)
3. Latency, millisecond, percentiles
4. Utilization, percent, payload vs. carrier, goodput vs. theory BW
5. Overhead, eg. CPU usage, for compression and /or encryption
2. Why do we re-implement TTCP?
It uses all basic Sockets APIs: socket, listen, bind, accept, connect, read/recv, write/send, shutdown, close, etc.
The protocol is binary, not just byte stream, so it's better than the classic echo example
Typical behaviors, meaningful result, instead of packet/s
Service as benchmark for programming language as well, by comparing CPU usage
Not concurrent, at least in the very basic form
3. The Protocol
4. The Code
5. 这个例子用到了muduo库的Timestamp.h来计算时间,需要编译一下muduo库。具体方法:
6. 实例代码:
//main.cc
#include "commandLineParser.h"
#include "clientAndServer.h"
#include <assert.h>
int main(int argc, char *argv[]) {
Options options;
parseCommandLine(argc, argv, &options);
if(options.transmit){
transmit(options);
}
else if(options.receive){
receive(options);
}
else{
assert(0);
}
return 0;
}
//commandLineParser.h
#pragma once
#include <string>
#include <stdint.h>
//需要从命令行获取的信息
struct Options{
uint16_t port;
int length;
int number;
bool transmit, receive, nodelay;
std::string host;
Options()
:port(0), length(0), number(0),
transmit(false), receive(false), nodelay(false){}
};
//获取命令行信息
bool parseCommandLine(int argc, char *argv[], Options *opt);
//从命令中解析出协议族地址
struct sockaddr_in resolveAddr(const char * host, uint16_t port);
//commandLineParser.cc
#include "commandLineParser.h"
#include <boost/program_options.hpp>
#include <iostream>
#include <netdb.h>
#include <stdio.h>
using namespace boost::program_options;
bool parseCommandLine(int argc, char *argv[], Options *opt){
options_description desc("Allowed options");
desc.add_options()
("help,h", "Help")
("port,p", value<uint16_t>(&opt->port)->default_value(5001), "TCP port")
("length,l", value<int>(&opt->length)->default_value(8192), <