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

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

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

  1. package main
  2. import (
  3. "net/http"
  4. "io/ioutil"
  5. "log"
  6. "github.com/gorilla/websocket"
  7. )
  8. var file []byte
  9. var upgrader = websocket.Upgrader{
  10. ReadBufferSize: 1024,
  11. WriteBufferSize: 1024,
  12. }
  13. func handler(w http.ResponseWriter, r *http.Request) {
  14. conn, err := upgrader.Upgrade(w, r, nil)
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. for i := 0; i < 100; i++ {
  19. conn.WriteMessage(2, file)
  20. }
  21. }
  22. func main() {
  23. file, _ = ioutil.ReadFile("<path-to-file>")
  24. http.HandleFunc("/", handler)
  25. err := http.ListenAndServeTLS(":443", "<path-to-cert>", "<path-to-key>", nil)
  26. if err != nil {
  27. panic("ListenAndServe: " + err.Error())
  28. }
  29. }

Node

  1. const WebSocket = require('ws');
  2. const https = require('https');
  3. const fs = require('fs');
  4. const options = {
  5. key: fs.readFileSync('<path-to-key>'),
  6. cert: fs.readFileSync('<path-to-cert>')
  7. };
  8. var file = fs.readFileSync('<path-to-file>')
  9. var httpServer = new https.createServer(options).listen(443);
  10. var wss = new WebSocket.Server({
  11. server: httpServer,
  12. perMessageDeflate: false
  13. });
  14. wss.on('connection', function(ws) {
  15. for (let i = 0; i < repetition; i++) {
  16. ws.send(file);
  17. }
  18. });
英文:

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

  1. package main
  2. import (
  3. &quot;net/http&quot;
  4. &quot;io/ioutil&quot;
  5. &quot;log&quot;
  6. &quot;github.com/gorilla/websocket&quot;
  7. )
  8. var file []byte
  9. var upgrader = websocket.Upgrader{
  10. ReadBufferSize: 1024,
  11. WriteBufferSize: 1024,
  12. }
  13. func handler(w http.ResponseWriter, r *http.Request) {
  14. conn, err := upgrader.Upgrade(w, r, nil)
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. for i := 0; i &lt; 100; i++ {
  19. conn.WriteMessage(2, file)
  20. }
  21. }
  22. func main() {
  23. file, _ = ioutil.ReadFile(&quot;&lt;path-to-file&gt;&quot;)
  24. http.HandleFunc(&quot;/&quot;, handler)
  25. err := http.ListenAndServeTLS(&quot;:443&quot;, &quot;&lt;path-to-cert&gt;&quot;, &quot;&lt;path-to-key&gt;&quot;, nil)
  26. if err != nil {
  27. panic(&quot;ListenAndServe: &quot; + err.Error())
  28. }
  29. }

Node

  1. const WebSocket = require(&#39;ws&#39;);
  2. const https = require(&#39;https&#39;);
  3. const fs = require(&#39;fs&#39;);
  4. const options = {
  5. key: fs.readFileSync(&#39;&lt;path-to-key&gt;&#39;),
  6. cert: fs.readFileSync(&#39;&lt;path-to-cert&gt;&#39;)
  7. };
  8. var file = fs.readFileSync(&#39;&lt;path-to-file&gt;&#39;)
  9. var httpServer = new https.createServer(options).listen(443);
  10. var wss = new WebSocket.Server({
  11. server: httpServer,
  12. perMessageDeflate: false
  13. });
  14. wss.on(&#39;connection&#39;, function(ws) {
  15. for (let i = 0; i &lt; repetition; i++) {
  16. ws.send(file);
  17. }
  18. });

答案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:

确定