Skip to content

Instantly share code, notes, and snippets.

@tihorygit
Last active May 7, 2022 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tihorygit/d40ceccce344506495319716db736922 to your computer and use it in GitHub Desktop.
Save tihorygit/d40ceccce344506495319716db736922 to your computer and use it in GitHub Desktop.
PostgreSQL + mORMot Server/Client
program PostgreSQLHttpRest;
{$I mormot.defines.inc}
uses
{$I mormot.uses.inc}
sysutils,
classes,
MTProcs,
mormot.core.os,
mormot.core.perf,
mormot.core.base,
mormot.orm.core,
mormot.orm.sql,
mormot.orm.sqlite3,
mormot.rest.core,
mormot.rest.http.server,
mormot.rest.http.client,
mormot.rest.sqlite3,
mormot.db.sql,
mormot.db.sql.postgres,
mormot.db.raw.postgres,
mormot.db.raw.sqlite3,
mormot.db.raw.sqlite3.static,
OrmModels;
const
InsertDirectCount = 10 * 1000;
InsertOrmCount = InsertDirectCount;
MultiConnection = 10;
PerConnection = InsertDirectCount div MultiConnection;
var
Texts: array of TOrmOITbl;
procedure Client(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
var
Model: TOrmModel;
Http: TRestHttpClient;
I: PtrInt;
begin
writeln('Start Client: ', Index);
Model := TOrmModel.Create([TOrmOITbl]);
Http := TRestHttpClient.Create('192.168.1.107', '9000', Model);
for I := Index * PerConnection to (Index + 1) * PerConnection - 1 do
begin
Http.Add(Texts[I], True);
end;
Http.Free;
Model.Free;
writeln('End Client: ', Index);
end;
var
I: Integer;
timer: TLocalPrecisionTimer;
begin
SetLength(Texts, InsertOrmCount);
for I := 1 to InsertOrmCount do
begin
Texts[I] := TOrmOITbl.Create();
with Texts[I] do
begin
Value := '{"X": ' + IntToStr(I) + '}';
end;
end;
timer := TLocalPrecisionTimer.CreateAndStart;
ProcThreadPool.DoParallel(Client, 0, MultiConnection - 1, nil, MultiConnection);
WriteLn('PostgreSQL ORM :', #9'Records Per Second : ', timer.PerSec(InsertDirectCount));
Writeln('---------------------------------------------------------');
for I := 0 to High(Texts) do
Texts[I].Free;
timer.Free;
end.
program PostgreSQLHttpRestServer;
{$I mormot.defines.inc}
uses
{$I mormot.uses.inc}
sysutils,
classes,
mormot.core.os,
mormot.core.perf,
mormot.core.base,
mormot.orm.core,
mormot.orm.sql,
mormot.orm.sqlite3,
mormot.rest.core,
mormot.rest.http.server,
mormot.rest.http.client,
mormot.rest.sqlite3,
mormot.db.sql,
mormot.db.sql.postgres,
mormot.db.raw.postgres,
mormot.db.raw.sqlite3,
mormot.db.raw.sqlite3.static,
OrmModels;
const
PQHost: PChar = '127.0.0.1';
PQPort: PChar = '5432';
PQDBName: PChar = 'mormot_postgres_test';
PQUserName: PChar = 'root';
PQPassword: PChar = '1';
var
PQProps: TSqlDBPostgresConnectionProperties;
Model: TOrmModel;
Server: TRestServerDB;
Http: TRestHttpServer;
begin
PQProps := TSqlDBPostgresConnectionProperties.Create(PQHost, PQDBName, PQUserName, PQPassword);
PQProps.ThreadingMode := tmThreadPool;
Model := TOrmModel.Create([TOrmOITbl]);
VirtualTableExternalRegisterAll(Model, PQProps);
Server := TRestServerDB.Create(Model, ':memory:');
Http := TRestHttpServer.Create('9000', [Server], '+', HTTP_DEFAULT_MODE, 10);
Server.DB.Synchronous := smOff;
Server.DB.LockingMode := lmExclusive;
Server.CreateMissingTables();
Http.AccessControlAllowOrigin := '*';
ReadLn;
Http.Free;
Server.Free;
Model.Free;
end.
@tihorygit
Copy link
Author

tihorygit commented May 7, 2022

Output

Start Client: 1
Start Client: 2
Start Client: 3
Start Client: 4
Start Client: 5
Start Client: 6
Start Client: 7
Start Client: 8
Start Client: 0
Start Client: 9
End Client: 5
End Client: 2
End Client: 9
End Client: 3
End Client: 4
End Client: 7
End Client: 1
End Client: 0
End Client: 8
End Client: 6
PostgreSQL ORM : Records Per Second : 745

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment