172 lines
5.0 KiB
Plaintext
172 lines
5.0 KiB
Plaintext
program Main;
|
|
|
|
{$APPTYPE CONSOLE}
|
|
|
|
{$R *.res}
|
|
|
|
uses
|
|
System.SysUtils,
|
|
System.Math,
|
|
ZConnection,
|
|
ZDataset,
|
|
DatabaseTestExecutor in 'DatabaseTestExecutor.pas',
|
|
DataGenerator in 'DataGenerator.pas';
|
|
|
|
function GetStringArg(const ASwitch, ADefault: string): string;
|
|
begin
|
|
if not FindCmdLineSwitch(ASwitch, Result, True) then
|
|
Result := ADefault;
|
|
end;
|
|
|
|
function GetIntArg(const ASwitch: string; ADefault: Integer): Integer;
|
|
begin
|
|
var LStrValue: string;
|
|
|
|
if FindCmdLineSwitch(ASwitch, LStrValue, True) then
|
|
Result := StrToIntDef(LStrValue, ADefault)
|
|
else
|
|
Result := ADefault;
|
|
end;
|
|
|
|
var
|
|
LConnection: TZConnection;
|
|
|
|
begin
|
|
var LHost: string;
|
|
LHost := GetStringArg('host', 'localhost');
|
|
|
|
var LPort: Integer;
|
|
LPort := GetIntArg('port', 5433);
|
|
|
|
var LDatabase: string;
|
|
LDatabase := GetStringArg('db', 'test_db');
|
|
|
|
var LUser: string;
|
|
LUser := GetStringArg('user', 'postgres');
|
|
|
|
var LPassword: string;
|
|
LPassword := GetStringArg('pass', 'postgres');
|
|
|
|
var LSeed: Integer;
|
|
LSeed := GetIntArg('seed', 1234);
|
|
|
|
var LNumActions: Integer;
|
|
LNumActions := GetIntArg('ops', 10000);
|
|
|
|
LConnection := TZConnection.Create(nil);
|
|
LConnection.LibraryLocation := ExtractFilePath(ParamStr(0)) + 'libpq.dll';
|
|
|
|
try
|
|
LConnection.Protocol := 'postgresql';
|
|
LConnection.HostName := LHost;
|
|
LConnection.Port := LPort;
|
|
LConnection.Database := LDatabase;
|
|
LConnection.User := LUser;
|
|
LConnection.Password := LPassword;
|
|
|
|
Writeln('Connecting to PostgreSQL...');
|
|
LConnection.Connect;
|
|
Writeln('Connected successfully!');
|
|
Writeln('-------------------------');
|
|
|
|
var DatabaseManager: TDatabaseManager;
|
|
DatabaseManager := TDatabaseManager.Create(LConnection, LSeed);
|
|
|
|
try
|
|
var SetupSuccess: Boolean;
|
|
SetupSuccess := DatabaseManager.SetupDatabase();
|
|
|
|
if SetupSuccess then
|
|
begin
|
|
Writeln('Done setting up database!');
|
|
end
|
|
else
|
|
begin
|
|
Writeln('Failed to setup database connection!');
|
|
end;
|
|
Writeln('-------------------------');
|
|
|
|
WriteLn('Testing Database Inserts');
|
|
var LInsertTimes: TArray<Double>;
|
|
LInsertTimes := DatabaseManager.TestInserts(LNumActions);
|
|
|
|
var LMeanInsertTime: Double;
|
|
LMeanInsertTime := Mean(LInsertTimes);
|
|
|
|
if Length(LInsertTimes) <> LNumActions then
|
|
begin
|
|
WriteLn('All inserts did not complete, got=' + Length(LInsertTimes).ToString + ' expected=' + LNumActions.ToString);
|
|
end
|
|
else
|
|
begin
|
|
WriteLn(LNumActions.ToString + ' inserts completed, average time ' + LMeanInsertTime.ToString + 'ms');
|
|
end;
|
|
|
|
Writeln('-------------------------');
|
|
WriteLn('Testing reading back each record by primary key');
|
|
|
|
var LReadTimes: TArray<Double>;
|
|
LReadTimes := TArray<Double>.Create();
|
|
LReadTimes := DatabaseManager.TestReads(LNumActions);
|
|
|
|
var LMeanReadTime: Double;
|
|
LMeanReadTime := Mean(LReadTimes);
|
|
|
|
if Length(LReadTimes) <> LNumActions then
|
|
begin
|
|
WriteLn('All reads did not complete, got=' + Length(LReadTimes).ToString + ' expected=' + LNumActions.ToString);
|
|
end
|
|
else
|
|
begin
|
|
WriteLn(LNumActions.ToString + ' reads completed, average time ' + LMeanReadTime.ToString + 'ms');
|
|
end;
|
|
|
|
Writeln('-------------------------');
|
|
WriteLn('Testing reading back each record by email');
|
|
|
|
var LReadByEmailTimes: TArray<Double>;
|
|
LReadByEmailTimes := DatabaseManager.TestReadsByEmail();
|
|
|
|
var LMeanReadByEmailTime: Double;
|
|
LMeanReadByEmailTime := Mean(LReadByEmailTimes);
|
|
|
|
if Length(LReadByEmailTimes) <> LNumActions then
|
|
begin
|
|
WriteLn('All reads by email did not complete, got=' + Length(LReadByEmailTimes).ToString + ' expected=' + LNumActions.ToString);
|
|
end
|
|
else
|
|
begin
|
|
WriteLn(LNumActions.ToString + ' reads by email completed, average time ' + LMeanReadByEmailTime.ToString + 'ms');
|
|
end;
|
|
|
|
Writeln('-------------------------');
|
|
WriteLn('Testing reading back each record by social security number');
|
|
|
|
var LReadBySocialTimes: TArray<Double>;
|
|
LReadBySocialTimes := DatabaseManager.TestReadsBySocial();
|
|
|
|
var LMeanReadBySocialTime: Double;
|
|
LMeanReadBySocialTime := Mean(LReadBySocialTimes);
|
|
|
|
if Length(LReadBySocialTimes) <> LNumActions then
|
|
begin
|
|
WriteLn('All reads by social did not complete, got=' + Length(LReadBySocialTimes).ToString + ' expected=' + LNumActions.ToString);
|
|
end
|
|
else
|
|
begin
|
|
WriteLn(LNumActions.ToString + ' reads by social completed, average time ' + LMeanReadBySocialTime.ToString + 'ms');
|
|
end;
|
|
|
|
finally
|
|
DatabaseManager.Free;
|
|
end;
|
|
|
|
finally
|
|
FreeAndNil(LConnection);
|
|
end;
|
|
|
|
Writeln('-------------------------');
|
|
Writeln('Press Enter to exit...');
|
|
Readln;
|
|
end.
|