C++后台开发 网络编程实践一

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), <
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值