我尝试使用websockets将数据从高速公路/加密服务器传输到浏览器中的javascript客户端。 connection.onmessage处理程序似乎有一些约3mps的吞吐量限制,无论消息传输特性如何,这似乎都是强制执行的。服务器执行此操作:
import time
from twisted.internet import reactor
from twisted.web.server import *
from twisted.web.resource import *
from twisted.web.static import Data
from autobahn.twisted.websocket import *
from autobahn.twisted.resource import *
pageHtml = '''<html><head><script>
function wsReceive(e) {
received += e.data.byteLength;
duration = Date.now() - startTime;
console.log("Rx "+received+" bytes in "+duration+"ms: "+received/(duration*1000.0));
}
function init() {
startTime = Date.now();
received = 0;
connection = new WebSocket('ws://localhost:8080/speedtest');
connection.onmessage = wsReceive;
connection.binaryType = 'arraybuffer';
}
</script></head>
<body onload="javascript:init()"></body></html>'''
dataSize = 1 * 2 ** 20
msgSize = 10 * 2 ** 10
class SpeedTest(WebSocketServerProtocol):
def onOpen(self):
start = time.clock()
for i in range(dataSize/msgSize):
payload = bytearray(msgSize)
self.sendMessage(bytes(payload), True)
print "Transmit to autobahn queue: %.1fms" % ((time.clock()-start)*1000.0)
factory = WebSocketServerFactory()
factory.protocol = SpeedTest
tree = Resource()
frontPage = Data(pageHtml,"text/html")
tree.putChild('', frontPage)
tree.putChild('index.html', frontPage)
tree.putChild('speedtest', WebSocketResource(factory))
reactor.listenTCP(8080, Site(tree))
reactor.run()
没有什么复杂的 - 只是在任何连接的客户端上发布一系列消息。我正在针对Safari和Chrome中运行的javascript(OSX 10.11,pypy-4.0.1)进行测试。为了找到最大性能(一切都在内部环回连接上运行),我用它连接到它:
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientFactory, \
WebSocketClientProtocol, \
connectWS
import time
class SpeedTest(WebSocketClientProtocol):
def onOpen(self):
self.startTime = time.clock()
self.received = 0
print "Connected"
self.sendMessage("Hello")
def onMessage(self, payload, isBinary):
duration = time.clock() - self.startTime
self.received += len(payload)
print "Rx %d bytes in %.1fms: %.1fmps" % (self.received, duration*1000.0, (self.received / duration) // 1000000.0)
factory = WebSocketClientFactory(u"ws://127.0.0.1:8080/speedtest")
factory.protocol = SpeedTest
connectWS(factory)
reactor.run()
无论我如何设置dataSize和msgSize,我都看到大约240mps。当我连接Safari或Chrome时,最大输出为3mps。例如,这显示1MB消息中的10MB数据。
这显示了相同的10MB拆分为64K消息,吞吐量基本相同。对onmessages的调用之间的时间间隔更长,以保持比率。
我在Chrome中看到了相同的内容:Chrome中的时间轴显示浏览器闲置率约为95% - 在将数据移交给onmessage事件处理程序之前,所有等待时间都在javascript之外发生。所以基本上我的问题是两部分:
编辑:意识到我没有在Firefox上测试这个。它的峰值为85mbps,但在某些条件下(更多,更小的消息)似乎也将连接限制在30mbps。对第1部分的部分答案是:使用firefox。