我正在开发需要特定信令服务器实现的WebRTC应用程序。最初,我在NodeJS中开发服务器,但后来决定迁移到Python(使用Django Channels AsyncWebsocketConsumer通过Websocket与客户端进行通信)。迁移之后,我使用WebSocket基准测试工具Thor比较了两种实现,这些都是获得的结果(5000个websocket连接,每个发送1000条消息):
Python(Django渠道)实现:
class SignallingConsumer(AsyncWebsocketConsumer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.peer = None
self.signal = None
self.is_peer_registered = False
async def connect(self):
await self.accept()
async def disconnect(self, close_code):
pass
async def receive(self, text_data=None, bytes_data=None):
pass
NodeJS实现:
method.start = function () {
this.webServer = this.createWebServer();
this.webServer.listen(this.config.port, function (){
console.log("Web server is listening");
});
this.socket = this.createWebSocket(this.webServer);
this.socket.on('connection', function(ws) {
var pingsCompleted = 0;
ws.on('message', function(evt) {
}.bind(this));
// Set out ping/pong mechanism
var pingInterval = setInterval(function() {
if(pingsCompleted > 2) {
ws.close();
}
else {
ws.ping();
pingsCompleted++;
}
}.bind(this), config.pingPeriod);
ws.on('pong', function(evt) {
pingsCompleted = 0;
}.bind(this));
ws.on('close', function(evt) {
}.bind(this));
Python(Django渠道)结果:
Online 30792 milliseconds
Time taken 30792 milliseconds
Connected 3714
Disconnected 0
Failed 1286
Total transferred 4.43MB
Total received 525.91kB
Durations (ms):
min mean stddev median max
Handshaking 4795 11410 5517 10824 23923
Latency NaN NaN NaN NaN NaN
NodeJS结果:
Online 41307 milliseconds
Time taken 41307 milliseconds
Connected 4051
Disconnected 0
Failed 949
Total transferred 952.72kB
Total received 693.4kB
Durations (ms):
min mean stddev median max
Handshaking 2 1124 1044 860 5200
Latency NaN NaN NaN NaN NaN
因此,尽管两种实现中失败连接的数量大致相同,但Django Channels中握手协商的持续时间比NodeJS中的慢得多,这使我重新考虑了Python的迁移。
那么这些结果正常吗?在这种情况下使用NodeJS更好吗?
答案 0 :(得分:1)
NodeJS在相当低的级别上执行其非阻塞I / O,并得到V8的支持,后者实现了相当不错的JIT优化。
Django通道是更高级别的构造,性能也将在很大程度上取决于Python的实现。除其他外,CPython直接解释字节码并使用全局执行锁。
我希望NodeJS在CPython上的性能比Django好。但是,如果出于其他原因而喜欢Django,则可能需要比较PyPy,IronPython和Jython的性能。