node js,golang,socket服务返回hello world性能对比

EmmaSteward 发布于12天前 阅读205次
0 条评论

初次接触node js 时,看到node js 异步io 性能很高,并发很强,当时就认为很强,用了一段时间,,后来某天又看到以性能著称的golang,于是又学习了一波,,并对两者进行了性能对比,,果不其然,node js败下阵来。。后来对并发产生了浓厚兴趣,于是自己使用unix系统的api写了个tcp服务,来测试并发,然而水平有限,分别使用了 单线程阻塞,多线程,io多路复用 ,,性能一个比一个差,,因为写的简单只返回hello world,也许性能差的原因和此有关,不同的情况适合不同的io模型,,以后会尝试io密集型,计算密集型测试,现在先将使用ab 工具测试的node js 服务,golang服务,单线程阻塞服务,recv多线程服务,mac下kqueue 服务的ab测试结果及测试的代码贴出来,供大家吐槽指点。

node js,1000请求100并发,平均每秒处理请求数量 12000左右

代码

let http = require("http");

http.createServer(function(req,res){
          res.writeHead(200);
          res.end("hello world");
}).listen(8003);

golang,1000请求100并发,平均每秒处理请求数量 14000左右

代码

package main
import(
        "net/http"
)
func main(){
        http.HandleFunc("/",func(w http.ResponseWriter,r*http.Request){
                w.Write([]byte("hello world"))
        })
        http.ListenAndServe(":8020",nil)
}

单线程阻塞,1000请求100并发,平均每秒请求数量 8000左右

代码

#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>

char resdata[205] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nhello world";

int main(int argc, const char * argv[]) {
    struct sockaddr_in bindaddr;
    bindaddr.sin_family = AF_INET;
    bindaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    bindaddr.sin_port = htons(3001);
    
    int listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if(listenfd>0){
        printf("创建socket成功");
        if (bind(listenfd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) != -1){
            printf("绑定端口号成功");
            if (listen(listenfd, SOMAXCONN) != -1){
                printf("初始化成功");
                while (1) {
                     int clientfd = accept(listenfd, NULL, NULL);
                     char buf[200];
                        //5. 从客户端接受数据
                     int ret = recv(clientfd, buf, 200, 0);
                    if(ret >0){
                        int ret2 = send(clientfd, resdata, 200, 0);
                        if(ret2 > 0){
                            printf("发送数据成功");
                            close(clientfd);
                        }
                        else{
                            printf(ret2);
                        }
                    }
                    else{
                        printf("123");
                    }
                }
            }
        }
    };
    
}

多线程阻塞,1000请求100并发,平均每秒处理请求数量3000左右

代码(打印了许多文字来追踪流程勿喷)

#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <pthread.h>

pthread_t ntid;

char resdata[205] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nhello world";

void deal(int clientfd){
    char buf[999];
    //5. 从客户端接受数据
    int ret = recv(clientfd, buf, 999, 0);
    if(ret >0){
        
        
        int ret2 = send(clientfd, resdata, strlen(resdata), 0);
        if(ret2 > 0){
            printf("发送数据成功");
            close(clientfd);
        }
        else{
            printf(ret2);
        }
        pthread_exit((void *)2);
    }
}


int main(int argc, const char * argv[]) {
    struct sockaddr_in bindaddr;
    bindaddr.sin_family = AF_INET;
    bindaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    bindaddr.sin_port = htons(3000);
    
    int listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if(listenfd>0){
        printf("创建socket成功");
        if (bind(listenfd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) != -1){
            printf("绑定端口号成功");
            if (listen(listenfd, SOMAXCONN) != -1){
                printf("初始化成功");
                while (1) {
                    printf("第二轮循环开始");
                    int clientfd = accept(listenfd, NULL, NULL);
                    int err;
                    err = pthread_create(&ntid,NULL,deal,clientfd);
                    if(err == 0){
                        printf("一轮循环结束\n");
                    }
                }
            }
        }
    };
    
}

kqueue 多路复用(因为我是mac电脑)1000请求100并发大量socket 54 错误,可能代码有问题,,改为1000请求10并发,每秒处理3000左右请求

代码

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/event.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#define PORT 3000
#define MAX_EVENT_COUNT 100000

void dealwrite(int sock,int kq){
    printf("写h数据");
    char buffer[]="HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nhello world";
    ssize_t sendlen = send(sock, buffer, sizeof(buffer), 0);
    printf("%d",sendlen);
    printf("&");
    printf("%d",sock);
    if (sendlen <= 0)
    {
        //链接断开
        close(sock);
        struct kevent event = {sock,EVFILT_READ,EV_DELETE,0,0,NULL};
        kevent(kq, &event, 1, NULL, 0, NULL);
        printf("write broken!\n");
        return;
    }
    //struct kevent event = {sock,EVFILT_READ,EV_DELETE,100,0,NULL};
    //kevent(kq, &event, 1, NULL, 0, NULL);
    //struct kevent event2 = {sock,EVFILT_WRITE,EV_DELETE,200,0,NULL};
    //kevent(kq, &event2, 1, NULL, 0, NULL);
    close(sock);
}

void dealread(int sock,int kq){
    char buffer[100];
    printf("读数据");
    printf("%d",sock);
    printf("&");
    //memset(buffer, '\0', data);
    ssize_t recvlen = recv(sock, buffer, 100, 0);
    printf("%d",recvlen);
    if(recvlen == 100){
        return;
    }
    if (recvlen <= 0)
    {
        //链接断开
        close(sock);
        struct kevent event = {sock,EVFILT_READ,EV_DELETE,0,0,NULL};
        kevent(kq, &event, 1, NULL, 0, NULL);
        printf("read broken!\n");
        return;
    }
    struct kevent changelist2[1];
    EV_SET(&changelist2[0], sock, EVFILT_WRITE, EV_ADD, 0, 200, NULL);
    kevent(kq, changelist2, 1, NULL, 0, NULL);
}


int createSocket()
{
    int sock = socket(PF_INET, SOCK_STREAM, 0);
    if (sock == -1)
    {
        printf("socket() failed:%d\n",errno);
        return -1;
    }
    
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    
    int optval = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
    optval = 1;
    setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval));
    
    if (bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == -1)
    {
        printf("bind() failed:%d\n",errno);
        return -1;
    }
    
    if (listen(sock, 5) == -1)
    {
        printf("listen() failed:%d\n",errno);
        return -1;
    }
    
    return sock;
}

int main(int argc, const char * argv[])
{
    int listenfd = createSocket();
    if (listenfd == -1)
        return -1;
    
    int kq = kqueue();
    if (kq == -1)
    {
        printf("kqueue failed:%d",errno);
        return -1;
    }
    
    struct kevent event = {listenfd,EVFILT_READ,EV_ADD,0,0,NULL};
    int ret = kevent(kq, &event, 1, NULL, 0, NULL);
    if (ret == -1)
    {
        printf("kevent failed:%d",errno);
        return -1;
    }
    printf("%c",EVFILT_WRITE);
    while (1)
    {
        struct kevent eventlist[MAX_EVENT_COUNT];
        struct timespec timeout = {5,0};
        int ret = kevent(kq, NULL, 0, eventlist, MAX_EVENT_COUNT, &timeout);
        if (ret <= 0)
            continue;
        
        for (int i=0; i<ret; i++)
        {
            struct kevent event = eventlist[i];
            int sock = (int)event.ident;
            int16_t filter = event.filter;
            uint32_t flags = event.flags;
            intptr_t data = event.data;
            printf("重新开始");
            printf("%c",filter);
            
            //有新的客户端链接
            if (sock == listenfd)
            {
                printf("新链接");
                printf("%d",sock);
                socklen_t client_addrlen = 4;
                struct sockaddr client_addrlist[client_addrlen];
                int clientfd = accept(listenfd, client_addrlist, &client_addrlen);
                if (clientfd > 0)
                {
                    struct kevent changelist[1];
                    
                    EV_SET(&changelist[0], clientfd, EVFILT_READ, EV_ADD, 0, 100, NULL);
                                        kevent(kq, changelist, 1, NULL, 0, NULL);
                    kevent(kq, changelist, 1, NULL, 0, NULL);

                }
                continue;
            }
            
            //异常事件
            if (flags & EV_ERROR)
            {
                printf("异常");
                close(sock);
                struct kevent event = {sock,EVFILT_READ,EV_DELETE,0,0,NULL};
                kevent(kq, &event, 1, NULL, 0, NULL);
                printf("socket broken,error:%ld\n",data);
                continue;
            }
            
            //数据可读
            if (filter == EVFILT_READ)
            {
                dealread(sock,kq);
            }
            
            //数据可写s
            if (filter == EVFILT_WRITE)
            {
                dealwrite(sock,kq);
            }
            
        }
    }
    
    return 0;
}

如有kqueue错误使用之处请指出。

所有代码都在本机上运行通过,socket服务是用c语言写的,需要编译运行,kqueue需要在mac机上运行,golang需要安装对应环境及设置对应变量

如果要安装golang环境并对golang感兴趣可以给我留言,

附一张压测截图

node js,golang,socket服务返回hello world性能对比

压测截图

查看原文: node js,golang,socket服务返回hello world性能对比

  • beautifulbutterfly
  • browncat
  • greencat
  • lazyostrich
  • blackduck
  • greenmouse
  • redrabbit
需要 登录 后回复方可回复, 如果你还没有账号你可以 注册 一个帐号。