为什么 WebSocket 实现在传输多个文件时比 HTTP/2 Push 慢?(Node.js / Go)

huangapple go评论90阅读模式
英文:

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 WebSocketnode-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 (
  &quot;net/http&quot;
  &quot;io/ioutil&quot;
  &quot;log&quot;
  &quot;github.com/gorilla/websocket&quot;
)

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 &lt; 100; i++ {
      conn.WriteMessage(2, file)
    }
}

func main() {
  file, _ = ioutil.ReadFile(&quot;&lt;path-to-file&gt;&quot;)

  http.HandleFunc(&quot;/&quot;, handler)
  err := http.ListenAndServeTLS(&quot;:443&quot;, &quot;&lt;path-to-cert&gt;&quot;, &quot;&lt;path-to-key&gt;&quot;, nil)
  if err != nil {
    panic(&quot;ListenAndServe: &quot; + err.Error())
  }
}

Node

const WebSocket = require(&#39;ws&#39;);
const https = require(&#39;https&#39;);
const fs = require(&#39;fs&#39;);

const options = {
  key: fs.readFileSync(&#39;&lt;path-to-key&gt;&#39;),
  cert: fs.readFileSync(&#39;&lt;path-to-cert&gt;&#39;)
};

var file = fs.readFileSync(&#39;&lt;path-to-file&gt;&#39;)

var httpServer = new https.createServer(options).listen(443);

var wss = new WebSocket.Server({
  server: httpServer,
  perMessageDeflate: false
});

wss.on(&#39;connection&#39;, function(ws) {
  for (let i = 0; i &lt; 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.

huangapple
  • 本文由 发表于 2017年3月14日 00:08:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/42768309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定