node js,golang,socket服務返回hello world效能對比
初次接觸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;
}
所有程式碼都在本機上執行通過,socket服務是用c語言寫的,需要編譯執行,kqueue需要在mac機上執行,golang需要安裝對應環境及設定對應變數
附一張壓測截圖
相關文章
- Node.js vs. Spring Boot:Hello World 效能對決,誰更快一點?Node.jsSpring Boot
- Node.js 系列 - 搭建 "Hello World" HTTP 伺服器Node.jsHTTP伺服器
- Node.js 系列 – 搭建 “Hello World” HTTP 伺服器Node.jsHTTP伺服器
- 服務端 I/O 效能:Node、PHP、Java、Go 的對比服務端PHPJavaGo
- JMicro微服務Hello World微服務
- 客觀對比Node 與 GolangGolang
- Next.js入門:Hello WorldJS
- Web Socket 效能對比——Spring Boot vs TomWebSpring Boot
- Hello, World
- Hello,World
- Hello World
- Hello World!
- Hello World !
- 使用Golang語言編寫Hello World Web應用GolangWeb
- hello world .net core 微服務框架 Viper微服務框架
- Go - Hello WorldGo
- Deep "Hello world!"
- Hello Python worldPython
- Hello World探究
- Docker Hello WorldDocker
- dotnet hello world
- Go:Hello WorldGo
- ant Hello World
- Node.js 建立MySql服務Node.jsMySql
- Node.js搭建Https服務Node.jsHTTP
- Threes.js入門篇之2 - Hello WorldJS
- 讓你的Laravel在 15 毫秒內返回一個"hello world!"Laravel
- Web Socket 效能對比——Spring Boot vs Tomcat vs NettyWebSpring BootTomcatNetty
- I'm Hello World
- 輸出hello world
- RabbitMQ tutorial - "Hello world!"MQ
- WebGL 的 Hello WorldWeb
- react的”Hello World !“React
- Node.js 服務連線 MySQLNode.jsMySql
- Node.js + Nginx 部署 HTTPS 服務Node.jsNginxHTTP
- node.js啟動http服務Node.jsHTTP
- node.js 用socket實現聊天Node.js
- Flutter Web 之 Hello WorldFlutterWeb