Skip to content

Instantly share code, notes, and snippets.

@tihorygit
Last active May 4, 2022 11:16
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/3b2592dcd62e384a54746804a63ed71b to your computer and use it in GitHub Desktop.
Save tihorygit/3b2592dcd62e384a54746804a63ed71b to your computer and use it in GitHub Desktop.
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;
{$DEFINE Parallel}
const
PQHost: PChar = '127.0.0.1';
PQPort: PChar = '5432';
PQDBName: PChar = 'mormot_postgres_test';
PQUserName: PChar = 'root';
PQPassword: PChar = '1';
InsertDirectCount = 10 * 1000;
InsertOrmCount = InsertDirectCount;
MultiConnection = 10;
PerConnection = InsertDirectCount div MultiConnection;
type
TOrmOITbl = class(TOrm)
private
FValue: RawJson;
published
property Value: RawJson read FValue write FValue;
end;
//var
// TerminateLock: TSynLocker;
var
Texts: array of TOrmOITbl;
procedure Client(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
var
Model: TOrmModel;
Http: TRestHttpClient;
I: PtrInt;
begin
Model := TOrmModel.Create([TOrmOITbl]);
Http := TRestHttpClient.Create('127.0.0.1', '9000', Model);
for I := Index * PerConnection to (Index + 1) * PerConnection - 1 do
begin
Http.Add(Texts[I], True);
end;
Http.Free;
Model.Free;
end;
var
I: Integer;
timer: TLocalPrecisionTimer;
PQProps: TSqlDBPostgresConnectionProperties;
Model: TOrmModel;
Server: TRestServerDB;
Http: TRestHttpServer;
begin
//TerminateLock.Init;
//TerminateLock.Lock;
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, MultiConnection);
Server.DB.Synchronous := smOff;
Server.DB.LockingMode := lmExclusive;
Server.CreateMissingTables();
Http.AccessControlAllowOrigin := '*';
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;
//TerminateLock.UnLock;
//TerminateLock.Done;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment