[Libwebsockets] Performance Challenges vs socket.io
andy at warmcat.com
Tue Jun 7 04:14:39 CEST 2016
On 06/07/2016 09:35 AM, Ignas wrote:
> Hi Guys,
> I was really happy being able to compile and get libwebsockets working.
> However I am having some challenges with understanding performance of
> libwebsockets on windows.I did a bit of a read on the google group, but
> seems people are taking about unrelated issues.
> In terms of performance. I did a very simple test that goes loads the
> over websocket and then downloads 30k worth of data from a server over
> the websocket. Unfortunately what I am trying to do is latency critical
> and I compared to socket.io <http://socket.io> I can see at least 50+ ms
> delay. Using wireshark I have narrowed it down to basically libwebsocket
> not pushing the frames as fast. Could anyone take a look at my test code
Unfortunately aside from the other considerations lws is a library, but
what gets tested is library + some other code.
I appreciate you sent your code, anyway.
> and see if there is anything I can do to improve it (besides making it
> more readable of course)?
> Considering it is just the "lws_service(context, 0);" line I am not sure
> if there is.
> I am attaching wireshark captures in case you would like to compare those:
> From what I can see after the 5 second mark once the protocol has
> initialised, the diffence is that libwesockets are using RSL and IPA
> protocols compared to socket.io <http://socket.io> TCP.
> Cell +44 7938015569
> "A week of coding can spare an hour of thought."
This is completely wrong -->
* websocket_write_back: write the string data to the destination wsi.
int websocket_write_back(struct lws *wsi_in, char *str, int str_size_in)
unsigned char *out = NULL;
char *write_what = reply_string;
out = (unsigned char
*)malloc(sizeof(char)*(LWS_SEND_BUFFER_PRE_PADDING + len +
//* setup the buffer*/
memcpy(out + LWS_SEND_BUFFER_PRE_PADDING, write_what, len);
//* write out*/
n = lws_write(wsi_in, out + LWS_SEND_BUFFER_PRE_PADDING, len,
//* free the buffer*/
1) len is uninitialized! Your compiler should have complained. You are
malloc()-ing up some random amount of memory.
2) the code is terminally confused (str is not used, global reply_string is)
3) You do not need to malloc up a buffer there at all.
If lws finds your socket can accept what you want to send, it sends it
If it could only send part of it, it malloc()'s up its own buffer and
sends the rest later.
printf(KCYN_L"[Main Service] Server recvived:\n"RESET/*, (char
//* echo back to client*/
websocket_write_back(wsi, (char *)in, -1);
You can't write from the receive callback. You have to ask for
lws_callback_on_writable() and write it in the
Actually if you don't take care about that, in another implementation
say, it will use an unbounded amount of memory holding your async write
data in the case many receives are coming but that connection cannot
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
More information about the Libwebsockets