#1 2018-06-16 13:44:28

mohsenti
Member
Registered: 2015-04-11
Posts: 72

WebSockets memory usage

Hi,

I wanted to try WebSockets chat test with many clients so I made this example that runs many clients at once.
The problem is for 50 clients, Server will use around 50mb of memory and it seems very high.
Using last mORMot, Windows10 and FPC 3.1.1.
Can somebody show me how should I config server and clients?

Server code:

program Server;

{$R *.res}

uses
  ChatUnit,
  SynCommons,
  mORMot,
  SynBidirSock,
  mORMotHttpServer;

var
  HttpServer: TSQLHttpServer;
  Srv: TSQLRestServerFullMemory;
begin
  Srv := TSQLRestServerFullMemory.CreateWithOwnModel([]);
  try
    Srv.ServiceDefine(TChatService, [IChatService], sicShared).SetOptions([], [optExecLockedPerInterface]).ByPassAuthentication := True;
    HttpServer := TSQLHttpServer.Create('8888', [Srv], '+', useBidirSocket);
    try
      HttpServer.WebSocketsEnable(Srv, ChatKey);
      TextColor(ccLightGreen);
      WriteLn('Server is live');
      ReadLn;
    finally
      HttpServer.Free;
    end;
  finally
    Srv.Free;
  end;
end.

Client code:

unit ManyClientsMain;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  ChatUnit, SynCommons, mORMot, mORMotHttpClient;

type
  TClient = record
    HttpClient: TSQLHttpClientWebsockets;
    Service: IChatService;
    Callback: TChatCallback;
  end;

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
  const
    CltsCount = 100;
  var
    Clts: array of TClient;
    procedure NewMsg(Sender: TObject);
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
begin
  Randomize;
  SetLength(Clts, CltsCount);
  for i := 0 to CltsCount - 1 do
    with Clts[i] do
    begin
      HttpClient := TSQLHttpClientWebsockets.Create('127.0.0.1', '8888', TSQLModel.Create([]));
      HttpClient.Model.Owner := HttpClient;
      HttpClient.WebSocketsUpgrade(ChatKey);
      HttpClient.ServerTimeStampSynchronize;
      HttpClient.ServiceDefine([IChatService], sicShared);
      HttpClient.Services.Resolve(IChatService, Service);
      Callback := TChatCallback.Create(HttpClient, IChatCallback);
      Callback.OnNotify := @NewMsg;
      Service.Join('User: ' + IntToStr(Random(1000)), Callback);
    end;
end;

procedure TForm1.NewMsg(Sender: TObject);
begin
end;

end.

Offline

#2 2018-06-16 15:02:37

zed
Member
From: Belarus
Registered: 2015-02-26
Posts: 105

Re: WebSockets memory usage

Each Thread use 1 Mb of RAM to its stack, so if mORMot use one Thread per client there is no surprise.
https://msdn.microsoft.com/en-us/librar … s.85).aspx

Offline

#3 2018-06-16 15:15:32

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: WebSockets memory usage

Nice point.
So what can I do for supporting many concurrent users?

Offline

#4 2018-06-16 15:25:00

zed
Member
From: Belarus
Registered: 2015-02-26
Posts: 105

Re: WebSockets memory usage

Did you test it with 500 or 5000 clients?

I'm not sure exactly, but I think that mORMot can use some Thread poll and the dependence of memory usage on the number of clients will not be linear.

Last edited by zed (2018-06-16 15:44:36)

Offline

#5 2018-06-16 15:46:10

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: WebSockets memory usage

I just did it and it seems you are right. 430mb for 2000 user.
@ab Can you comment on this and what settings I can set?

Offline

Board footer

Powered by FluxBB