JuliaWeb / WebSockets.jl

A WebSockets library for Julia
MIT License
157 stars 58 forks source link

Julia WebSocket slow to read/write #175

Closed nordicice closed 1 year ago

nordicice commented 3 years ago

I was testing Julia WebSockets and I found that they are much slower than the Python/Node equivalents. I wrote a test to send a ping and measure the time taken for a pong response from some crypto exchanges as an example. Julia consistently takes 50ms for the test below, whereas python takes 2-4 ms. Is this expected?

Julia:

using WebSockets function main() WebSockets.open("wss://wsaws.okex.com:8443/ws/v5/public") do ws for i = 1:100 a = time_ns() write(ws, "ping") data, success = readguarded(ws) b = time_ns() println(String(data), " ", (b - a) / 1000000) sleep(0.1) end end end main()

Equivalent Python:

import websockets import asyncio import time

async def main(): uri = "wss://wsaws.okex.com:8443/ws/v5/public" async with websockets.connect(uri) as websocket: msg = "hello" for i in range(101): a = time.time() await websocket.send(msg) x = await websocket.recv() b = time.time() print(b-a) time.sleep(0.1)

asyncio.get_event_loop().run_until_complete(main())

findmyway commented 3 years ago

Could you try the code in https://github.com/JuliaWeb/WebSockets.jl/issues/159#issue-668961968 ?

Based on my test, adding the following two lines will make the performance on par with Python (slightly slower):

using Sockets
using WebSockets

function main()
    WebSockets.open("wss://wsaws.okex.com:8443/ws/v5/public") do ws
        Sockets.nagle(ws.socket.bio, false)
        Sockets.quickack(ws.socket.bio, true)
        for i = 1:100
            a = time_ns()
            write(ws, "ping")
            data, success = readguarded(ws)
            b = time_ns()
            println(String(data), " ", (b - a) / 1000000)
            sleep(0.1)
        end
    end
end

main()