英文:
Why is WebSocket implementation slower than HTTP/2 Push at transferring multiple files? (Node.js / Go)
问题
我一直在使用Node和Go中的WebSockets和HTTP/2库进行实验。我的基本设置是创建一个客户端和服务器,重复从服务器发送文件,并测量每个文件在客户端可用的时间。
令我惊讶的是,HTTP/2的推送实现比WebSocket表现得更好(总时间快了5倍以上)。我做错了什么吗?
下面是我的Gorilla WebSocket和node-ws服务器代码:
Go
package main
import (
"net/http"
"io/ioutil"
"log"
"github.com/gorilla/websocket"
)
var file []byte
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func handler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
for i := 0; i < 100; i++ {
conn.WriteMessage(2, file)
}
}
func main() {
file, _ = ioutil.ReadFile("<path-to-file>")
http.HandleFunc("/", handler)
err := http.ListenAndServeTLS(":443", "<path-to-cert>", "<path-to-key>", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
Node
const WebSocket = require('ws');
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('<path-to-key>'),
cert: fs.readFileSync('<path-to-cert>')
};
var file = fs.readFileSync('<path-to-file>')
var httpServer = new https.createServer(options).listen(443);
var wss = new WebSocket.Server({
server: httpServer,
perMessageDeflate: false
});
wss.on('connection', function(ws) {
for (let i = 0; i < repetition; i++) {
ws.send(file);
}
});
英文:
I've been experiment with WebSockets and HTTP/2 libraries in Node and Go. My basic setup is to create a client and server, repeatedly send a file from the server and measure the time until each file is available at the client.
To my surprise, the HTTP/2 Push implementation performs significantly better than WebSocket (more than 5x faster in total time). Am I doing something wrong?
My Gorilla WebSocket and node-ws servers below:
Go
package main
import (
"net/http"
"io/ioutil"
"log"
"github.com/gorilla/websocket"
)
var file []byte
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func handler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
for i := 0; i < 100; i++ {
conn.WriteMessage(2, file)
}
}
func main() {
file, _ = ioutil.ReadFile("<path-to-file>")
http.HandleFunc("/", handler)
err := http.ListenAndServeTLS(":443", "<path-to-cert>", "<path-to-key>", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
Node
const WebSocket = require('ws');
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('<path-to-key>'),
cert: fs.readFileSync('<path-to-cert>')
};
var file = fs.readFileSync('<path-to-file>')
var httpServer = new https.createServer(options).listen(443);
var wss = new WebSocket.Server({
server: httpServer,
perMessageDeflate: false
});
wss.on('connection', function(ws) {
for (let i = 0; i < repetition; i++) {
ws.send(file);
}
});
答案1
得分: 1
请参考 https://github.com/gorilla/websocket/issues/228。原帖中测量的是使用HTTP/2 Push打开流的时间,而不是传输所有数据的时间。原帖作者发现Websockets比HTTP/2 Push更快。
英文:
See https://github.com/gorilla/websocket/issues/228. The OP was measuring the time to open the streams with HTTP/2 Push, not the time to transfer all of the data over the streams. The OP found that Websockets are faster than HTTP/2 push.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论