Last active
May 7, 2022 05:41
-
-
Save tihorygit/d40ceccce344506495319716db736922 to your computer and use it in GitHub Desktop.
PostgreSQL + mORMot Server/Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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