测试单进程单线程极限处理能力

测试单进程单线程极限处理能力

[toc]

测试过程

章节标题按照:环境、网络(本机、内网)、包大小(5字节、512字节、65k)结果

MBP本机5字节33万每秒

[lawrencechi@centos6 /data/host_share]$ ./udpserver
cnt:1
cnt:335070
cnt:331970
cnt:325655
cnt:336310

MBP本机512字节32万每秒

[lawrencechi@centos6 /data/host_share]$ ./udpserver
cnt:1
cnt:323410
cnt:325464
cnt:322750
cnt:325346

MBP本机60k字节9万每秒

[lawrencechi@centos6 /data/host_share]$ ./udpserver
cnt:1
cnt:100932
cnt:100261
cnt:103060
cnt:99918
cnt:101380
cnt:101387
cnt:96641
cnt:102714
cnt:101106
cnt:99290
cnt:102612
cnt:101937

腾讯云本机5字节21万每秒

[chiyl@VM_231_33_centos tmp]$ ./udpserver
cnt:1
cnt:211167
cnt:209314
cnt:208062
cnt:213580
cnt:212643
cnt:207598

腾讯云本机512字节20万每秒

[chiyl@VM_231_33_centos tmp]$ ./udpserver
cnt:1
cnt:200112
cnt:206787
cnt:205799
cnt:206545
cnt:201219
cnt:203444

腾讯云本机65k字节4万每秒

[chiyl@VM_231_33_centos tmp]$ ./udpserver
cnt:1
cnt:42880
cnt:43289
cnt:43468
cnt:43475
cnt:43383
cnt:43909

腾讯云机房内网5字节12万每秒

[chiyl@VM_231_33_centos tmp]$ ./udpserver
cnt:1
cnt:117529
cnt:122266
cnt:122153

腾讯云机房内网1400字节(MTU 1500)8万每秒

[chiyl@VM_231_33_centos tmp]$ ./udpserver
cnt:1
cnt:83065
cnt:83596
cnt:82676
cnt:82902

测试代码

server

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>

#define BUFLEN 65515
#define NPACK 10
#define PORT 9930

void diep(const char *s)
{
    perror(s);
    exit(1);
}

int main(void)
{
    struct sockaddr_in si_me, si_other;
    int s, i, slen=sizeof(si_other);
    char buf[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
        diep("socket");

    memset((char *) &si_me, 0, sizeof(si_me));
    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(s, (const sockaddr*)&si_me, sizeof(si_me))==-1)
        diep("bind");

    unsigned packet_cnt=0;
    struct timeval tv_begin={0};
    struct timeval tv_end={0};

    gettimeofday(&tv_begin, NULL);
    while(1)
    {
        if (recvfrom(s, buf, BUFLEN, 0, (sockaddr*)&si_other, (socklen_t*)&slen)==-1)
            diep("recvfrom()");
        packet_cnt++;
#if 0
        if(packet_cnt%50000==0)
        {
            struct timeval tv={0};
            gettimeofday(&tv, NULL);

            printf("cnt:%d, curtstamp:%d.%d\n",packet_cnt,tv.tv_sec,tv.tv_usec);
        }
#else
        gettimeofday(&tv_end, NULL);
        long ex=(tv_end.tv_sec-tv_begin.tv_sec)*1000000+(tv_end.tv_usec-tv_begin.tv_usec);
        if(ex>1000000)
        {
            printf("cnt:%d\n",packet_cnt);
            tv_begin=tv_end;
            packet_cnt=0;
        }
#endif
    }

    //for (i=0; i<NPACK; i++) {
    //    if (recvfrom(s, buf, BUFLEN, 0, (sockaddr*)&si_other, (socklen_t*)&slen)==-1)
    //        diep("recvfrom()");
    //    printf("Received packet from %s:%d\nData: %s\n\n", 
    //            inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
    //}

    close(s);
    return 0;
}

client

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#define BUFLEN 65000
#define NPACK 100*10000
#define PORT 9930
//#define SRV_IP "10.104.231.33"
#define SRV_IP "127.0.0.1"

void diep(const char *s)
{
    perror(s);
    exit(1);
}

int main(int argc,char* argv[])
{
    struct sockaddr_in si_other;
    int s, i, slen=sizeof(si_other);
    char buf[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
        diep("socket");

    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
    if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }

    int cnt=NPACK;
    if(argc!=1)
    {
        cnt=atoi(argv[1]);
    }

    for (i=0; i<cnt; i++) {
        if (sendto(s, buf, BUFLEN, 0, (const sockaddr*)&si_other, slen)==-1)
            diep("sendto()");
    }

    close(s);
    return 0;
}

makefile

all:
    g++ -o udpserver server.cpp
    g++ -o udpclient client.cpp

clean:
    rm -f *.o
    rm -f udpclient
    rm -f udpserver

标签: none



添加新评论