Theldus / wsServer

wsServer - a tiny WebSocket server library written in C
https://theldus.github.io/wsServer
GNU General Public License v3.0
422 stars 80 forks source link

Server processing time increase at each new client connection #28

Closed sancelot closed 3 years ago

sancelot commented 3 years ago

Hi, I have an unattended behaviour in the next context :

I have got a websocket wsserver server ready for connections.

The client that connects is always doing the same work : it sends an init string, then reads all the buffer sent by the server (almost 2megabytes data sent by binary chunks/ and some texts messages ) and closes.

I measured the processing time on client side to measure the whole process.

The wsServer is really fast with synchronous . (The best in comparison to websocketpp or Qt websocket)

But , each time a new client is launched and the server has done the job, the processing time to receive all the data increases:

eg here is client processing time sample increasing: 2.033s 2.07s 2.337s 2.649s 3.158s 3.607s 3.739s 4.319s....

I don't really understand, since there is always only one client connected at a time.

Have you got some idea ?

Theldus commented 3 years ago

Hi @sancelot, This behavior is really not expected, maybe it's something in you client-side?

I tried to reproduce something similar here but I don't know if that's exactly what you want:

send_receive.c:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ws.h>

void onopen(int fd){printf("Client #%d connected!\n", fd);}
void onclose(int fd){printf("Client #%d disconnected!\n", fd);}

void onmessage(int fd, const unsigned char *msg,
    uint64_t size, int type)
{
    ((void)size);
    ((void)type);

    #define BUF_SIZE (2 * 1024 * 1024)
    char *buf;

    if (!strcmp((char*)msg, "start")) {
        if (!(buf = malloc(BUF_SIZE))) {
            fprintf(stderr, "Unable to allocate buffer!\n");
            exit(EXIT_FAILURE);
        }
        memset(buf, 'a', BUF_SIZE);
        ws_sendframe_bin(fd, buf, BUF_SIZE, false);

        free(buf);
    }
}

int main(void)
{
    struct ws_events evs;
    evs.onopen    = &onopen;
    evs.onclose   = &onclose;
    evs.onmessage = &onmessage;
    ws_socket(&evs, 8080, 0);
    return (0);
}

send_receive.html:

<html>
    <head>
        <script>
            var ws;
            var sendCount = 0;

            function doWork(addr) {
                ws = new WebSocket("ws://localhost:8080");
                console.time('time');

                ws.onopen = function() {
                    console.log("Connected #" + sendCount + ", sending msg...");
                    ws.send("start");
                };
                ws.onmessage = function (evt) {
                    console.log("Receive message..., length: " + evt.data.size);
                    ws.close(1000);
                };
                ws.onclose = function(event) {
                    console.log("Disconnected...");
                    console.timeEnd('time');
                    console.log("");
                    sendCount++;
                };
            }
        </script>
    </head>
    <body>
        <input type="button" value="Do Work!" onClick="doWork()">
    </body>
</html>

In this example, if I press 'Do Work!' several times (+30x), the time between connecting, receiving the message and disconnecting remains constant, close to ~50ms.

Could you provide me (if possible) with a minimal example where this occurs? this makes it easier to isolate the problem and investigate where it occurs.

Also, what is your environment?

sancelot commented 3 years ago

I tried it. it worked fine. I found it was an issue in my code.

Theldus commented 3 years ago

No problem, I'm glad my library is being used out there =).