-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
PerfTestCases.pas
1251 lines (1142 loc) · 33.7 KB
/
PerfTestCases.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit PerfTestCases;
interface
{$I mormot.defines.inc}
// enable/disable third-party libraries
{.$define USENEXUSDB}
{.$define USEBDE}
{.$define USEUNIDAC}
{.$define USEZEOS}
{.$define USEIBX}
{.$define USEFIREDAC}
// enable/disable database engines
{.$define USEJET}
{.$define USEFIREBIRDEMB}
{.$define ODBCSQLITEFIREBIRD}
{.$define USELOCALMSSQLEXPRESS} // SQL Server 2008 R2 Express locally
{.$define USELOCALDBMSSQLEXPRESS} // SQL Server 2012 LocalDB edition
{.$define USELOCALDB2}
{.$define USELOCALPOSTGRESQL}
{.$define USELOCALMYSQL}
{.$define USEMONGODB} // those tests are not converted from mORMot 1.18 yet
{$ifdef CPU64}
{$undef USENEXUSDB} // official NexusDB is not yet 64 bit ready :(
{$undef USEJET} // MS Access / JET is not available under Win64
{$endif CPU64}
{$ifndef OSWINDOWS}
{$undef USENEXUSDB}
{$undef USEJET}
{$endif OSWINDOWS}
// if defined, will create two "stored false" properties, to test UNIQUE columns
{.$define UNIK}
uses
{$I mormot.uses.inc}
SysUtils,
mormot.core.base,
mormot.core.data,
mormot.core.text,
mormot.core.buffers,
mormot.core.datetime,
mormot.core.os,
mormot.core.perf,
mormot.core.unicode,
mormot.core.test,
mormot.orm.core,
mormot.orm.base,
mormot.orm.sql,
mormot.orm.storage,
mormot.orm.rest,
mormot.rest.core,
mormot.rest.sqlite3,
mormot.rest.memserver,
mormot.rest.server,
mormot.db.sql,
mormot.db.sql.sqlite3,
mormot.db.raw.sqlite3,
mormot.db.raw.sqlite3.static,
mormot.db.proxy,
mormot.core.log,
{$ifdef OSWINDOWS}
mormot.db.sql.oledb,
mormot.db.rad,
{$endif OSWINDOWS}
mormot.db.sql.odbc,
{$ifdef USENEXUSDB}
mormot.db.rad.nexusdb,
{$endif USENEXUSDB}
{$ifdef USEBDE}
mormot.db.rad.bde,
{$endif USEBDE}
{$ifdef USEUNIDAC}
mormot.db.rad.unidac,
SQLiteUniProvider,
InterbaseUniProvider,
OracleUniProvider,
DB2UniProvider,
SQLServerUniProvider,
PostgreSQLUniProvider,
MySqlUniProvider,
{$endif USEUNIDAC}
{$ifdef USEZEOS}
mormot.db.sql.zeos,
{$endif USEZEOS}
{$ifdef USEIBX}
mormot.db.sql.ibx,
{$endif USEIBX}
{$ifdef USEFIREDAC}
mormot.db.rad.firedac,
{$ifdef ISDELPHIXE5}
FireDAC.Phys.Oracle,
FireDAC.Phys.MSAcc,
FireDAC.Phys.MSSQL,
FireDAC.Phys.MySQL,
FireDAC.Phys.SQLite,
FireDAC.Phys.IB,
FireDAC.Phys.PG,
FireDAC.Phys.DB2,
{$else}
uADPhysOracle,
uADPhysMSAcc,
uADPhysMSSQL,
uADPhysSQLite,
uADPhysIB,
uADPhysPG,
uADPhysDB2,
uADPhysMySQL,
{$endif ISDELPHIXE5}
{$endif USEFIREDAC}
{$ifdef USELOCALPOSTGRESQL}
mormot.db.sql.postgres,
{$endif USELOCALPOSTGRESQL}
{$ifdef USEMONGODB}
mormot.db.nosql.mongodb,
{$endif USEMONGODB}
mormot.db.core;
type
TStat = class(TSynPersistent)
private
fCreateTable: RawUtf8;
fNumberOfElements: integer;
fInsertTime: RawUtf8;
fEngine: RawUtf8;
fClientCloseTime: RawUtf8;
fInsertRate: integer;
fReadOneByOneTime: RawUtf8;
fReadOneByOneRate: integer;
fInsertBatchTransactionRate: integer;
fInsertTransactionRate: integer;
fInsertBatchRate: integer;
fInsertBatchTransactionTime: RawUtf8;
fInsertTransactionTime: RawUtf8;
fInsertBatchTime: RawUtf8;
fReadAllVirtualRate: integer;
fReadAllDirectRate: integer;
fReadAllDirectTime: RawUtf8;
fReadAllVirtualTime: RawUtf8;
{$ifdef UNIK}
fReadOneByNameRate: integer;
fReadOneByNameTime: RawUtf8;
{$endif UNIK}
published
property Engine: RawUtf8
read fEngine;
property CreateTableTime: RawUtf8
read fCreateTable write fCreateTable;
property NumberOfElements: integer
read fNumberOfElements write fNumberOfElements;
property InsertTime: RawUtf8
read fInsertTime;
property InsertRate: integer
read fInsertRate;
property InsertBatchTime: RawUtf8
read fInsertBatchTime;
property InsertBatchRate: integer
read fInsertBatchRate;
property InsertTransactionTime: RawUtf8
read fInsertTransactionTime;
property InsertTransactionRate: integer
read fInsertTransactionRate;
property InsertBatchTransactionTime: RawUtf8
read fInsertBatchTransactionTime;
property InsertBatchTransactionRate: integer
read fInsertBatchTransactionRate;
property ReadOneByOneTime: RawUtf8
read fReadOneByOneTime;
property ReadOneByOneRate: integer
read fReadOneByOneRate;
{$ifdef UNIK}
property ReadOneByNameTime: RawUtf8
read fReadOneByNameTime;
property ReadOneByNameRate: integer
read fReadOneByNameRate;
{$endif UNIK}
property ReadAllVirtualTime: RawUtf8
read fReadAllVirtualTime;
property ReadAllVirtualRate: integer
read fReadAllVirtualRate;
property ReadAllDirectTime: RawUtf8
read fReadAllDirectTime;
property ReadAllDirectRate: integer
read fReadAllDirectRate;
property ClientCloseTime: RawUtf8
read fClientCloseTime;
end;
TOrmSample = class(TOrm)
private
fFirstName: RawUtf8;
fLastName: RawUtf8;
fAmount: Currency;
fBirthDate: TDateTime;
fLastChange: TModTime;
fCreatedAt: TCreateTime;
published
property FirstName: RawUtf8 index 40
read fFirstName write fFirstName
{$ifdef UNIK} stored AS_UNIQUE{$endif};
property LastName: RawUtf8 index 40
read fLastName write fLastName
{$ifdef UNIK} stored AS_UNIQUE{$endif};
property Amount: Currency
read fAmount write fAmount;
property BirthDate: TDateTime
read fBirthDate write fBirthDate;
property LastChange: TModTime
read fLastChange write fLastChange;
property CreatedAt: TCreateTime
read fCreatedAt write fCreatedAt;
end;
TTestDatabaseBenchmark = class(TSynTestsLogged)
public
Ini: RawUtf8;
Stats: TSynObjectList;
constructor Create(const Ident: string = ''); override;
destructor Destroy; override;
procedure SaveStats;
published
procedure DirectDatabaseAccess;
procedure ExternalDatabaseAccess;
end;
TTestDatabaseFlags = set of (
dbIsFile,
dbInMemory,
dbInMemoryVirtual,
dbPropIsMemory,
dbPropUntouched,
dbDropTable,
dbSlowInsert,
dbRemoteDB);
TTestDatabaseAbstract = class(TSynTestCase)
protected
Main: TTestDatabaseBenchmark;
Value: TOrmSample;
Stat: TStat;
Namee, Num: RawUtf8;
ChangeStart: TTimeLog;
RunTimer: TPrecisionTimer;
SQlite3Mode: TSQLSynchronousMode;
SQlite3Lock: TSQLLockingMode;
Client: TRestClientDB;
Orm: IRestOrmClient;
Server: TRestServerDB;
DBFileName: TFileName;
DBPassword: RawUtf8;
ValueLastName, ValueFirstName: TRawUtf8DynArray;
Res: TIDDynArray;
Flags: TTestDatabaseFlags;
procedure Setup; override;
procedure CleanUp; override;
procedure MethodSetup; override;
procedure MethodCleanUp; override;
procedure RunTests; virtual;
function ModelCreate: TOrmModel; virtual;
procedure ClientCreate; virtual;
procedure ClientFree; virtual;
procedure RunWrites(UseTransactions, UseBatch: boolean); virtual;
procedure ValueCheck(i: PtrInt);
procedure RunModeLock(Mode: TSQLSynchronousMode; Lock: TSQLLockingMode); virtual;
end;
TTestDirectSqliteEngine = class(TTestDatabaseAbstract)
protected
procedure RunModeLock(Mode: TSQLSynchronousMode; Lock: TSQLLockingMode); override;
published
procedure SqliteFileFull;
procedure SqliteFileOff;
procedure SqliteFileOffExc;
procedure SqliteFileOffExcAes;
procedure SqliteInMemory;
end;
TTestInMemoryEngine = class(TTestDatabaseAbstract)
protected
function ModelCreate: TOrmModel; override;
procedure ClientCreate; override;
published
procedure InMemoryStatic;
procedure InMemoryVirtual;
end;
TTestDatabaseExternalAbstract = class(TTestDatabaseAbstract)
protected
Props: TSQLDBConnectionProperties;
function ModelCreate: TOrmModel; override;
procedure ClientCreate; override;
procedure ClientFree; override;
procedure RunExternal(P: TSQLDBConnectionProperties); virtual;
public
end;
TTestSqliteExternal = class(TTestDatabaseExternalAbstract)
protected
fn: TFileName;
sm: TSQLSynchronousMode;
lm: TSQLLockingMode;
procedure ClientCreate; override;
procedure RunExternalSqlite(Mode: TSQLSynchronousMode; Lock: TSQLLockingMode);
published
procedure ExternalSqliteFileFull;
procedure ExternalSqliteFileOff;
procedure ExternalSqliteFileOffExc;
procedure ExternalSqliteInMemory;
end;
TTestSqliteRemote = class(TTestDatabaseExternalAbstract)
protected
RemoteProps: TSQLDBSQLite3ConnectionProperties;
RemoteServer: TSQLDBServerRemote;
RemoteClient: TSQLDBConnectionPropertiesClass;
procedure ClientCreate; override;
procedure ClientFree; override;
published
{$ifdef OSWINDOWS}
procedure RemoteSqliteWinHTTP;
{$endif OSWINDOWS}
procedure RemoteSqliteSocket;
end;
{$ifdef USELOCALPOSTGRESQL}
TTestPostgresql = class(TTestDatabaseExternalAbstract)
published
procedure _SynDBPostgres;
{$ifdef USEZEOS}
procedure ZeosPostgres;
{$endif USEZEOS}
end;
{$endif USELOCALPOSTGRESQL}
{$ifdef USELOCALMYSQL}
TTestmysql = class(TTestDatabaseExternalAbstract)
published
{$ifdef USEZEOS}
procedure Zeosmysql;
{$endif USEZEOS}
end;
{$endif USELOCALMYSQL}
{$ifdef USEFIREBIRDEMB}
{ TTestFirebird }
TTestFirebird = class(TTestDatabaseExternalAbstract)
published
{$ifdef USEIBX}
procedure IbxFirebird;
{$endif USEIBX}
{$ifdef USEZEOS}
procedure ZeosFirebird;
{$endif USEZEOS}
end;
{$endif USEFIREBIRDEMB}
implementation
const
VOIDINDEX = 150; // to identify potential issues with "" values
{$ifdef USEFIREBIRDEMB}
{ TTestFirebird }
const
/// Please configure parameters for firebird connection
{$ifdef CPU64}
// cFIREBIRDEMBEDDEDDLL = '';
cFIREBIRDEMBEDDEDDLL = 'C:\Firebird\Firebird3_64\fbclient.dll';
{$else}
cFIREBIRDEMBEDDEDDLL = 'C:\Firebird\firebird3_32\fbclient.dll';
{$endif CPU64}
{$ifdef USEZEOS}
cFirebirdServerZeos = ''; // 'localhost:3033'
cFirebirdDBFileZeos = 'r:\perftestZeos.fdb';
{$endif USEZEOS}
{$ifdef USEIBX}
cFirebirdServerIbx = ''; // 'localhost/3033'
cFirebirdDBFileIbx = 'r:\perftestIbx.fdb';
{$endif USEIBX}
cUserName = 'SYSDBA';
cPassword = 'masterkey';
{$ifdef USEZEOS}
procedure TTestFirebird.ZeosFirebird;
begin
Flags := [dbPropUntouched, dbDropTable];
// if FileExists(cFirebirdDBFileZeos) then
// DeleteFile(cFirebirdDBFileZeos);
RunExternal(TSqlDBZeosConnectionProperties.Create('zdbc:firebird://' +
cFirebirdServerZeos + '/' + cFirebirdDBFileZeos + '?username=' + cUserName +
';password=' + cPassword + ';LibLocation=' + cFIREBIRDEMBEDDEDDLL, '', '', ''));
end;
{$endif USEZEOS}
{$ifdef USEIBX}
procedure TTestFirebird.IbxFirebird;
var
lProps: TSqlDBIbxConnectionProperties;
begin
Flags := Flags - [dbPropUntouched, dbDropTable];
// if FileExists(cFirebirdDBFileIbx) then
// DeleteFile(cFirebirdDBFileIbx);
lProps := TSqlDBIbxConnectionProperties.Create(cFirebirdServerIbx,
cFirebirdDBFileIbx, cUserName, cPassword);
lProps.FirebirdLibraryPathName := cFIREBIRDEMBEDDEDDLL;
lProps.CreateDescendingOnlyPK := True;
RunExternal(lProps);
end;
{$endif USEIBX}
{$endif USEFIREBIRDEMB}
{ TTestDatabaseAbstract }
procedure TTestDatabaseAbstract.Setup;
begin
EnsureDirectoryExists(Executable.ProgramFilePath + 'db');
Main := Owner as TTestDatabaseBenchmark;
Value := TOrmSample.Create;
Namee := 'Name/ ';
UniqueRawUtf8(Namee); // FPC does not call it
PWord(@Namee[4])^ := $a9c3; // some 'e'acute to test UTF-8 encoding
SQlite3Mode := smOff; // fastest SQlite3 modes by default
SQlite3Lock := lmExclusive;
end;
procedure TTestDatabaseAbstract.Cleanup;
begin
// warning: this method could be called several times after a single Setup
FreeAndNil(Value);
end;
procedure TTestDatabaseAbstract.MethodSetup;
begin
Flags := [];
Stat := TStat.Create;
Stat.fEngine := ToUTF8(Owner.CurrentMethodInfo^.TestName);
end;
procedure TTestDatabaseAbstract.MethodCleanup;
begin
if Stat.Engine <> '' then
Main.Stats.Add(Stat)
else
FreeAndNil(Stat);
ClientFree;
end;
procedure TTestDatabaseAbstract.RunTests;
const
_ID: array['1'..'4'] of RawUtf8 = (
'',
' batch',
' trans',
' batch trans');
var
trans, batch, UseDirect: boolean;
time: RawUtf8;
rate: QWord;
i: PtrInt;
{%H-}log: ISynLog;
begin
// Insertion tests
Num := '1';
for trans := false to true do
for batch := false to true do
begin
log := TSynLog.Enter('% Insert%',
[Owner.CurrentMethodInfo^.IdentTestName, _ID[Num[1]]], self);
RunWrites(trans, batch);
NotifyTestSpeed('insert%',
[_ID[Num[1]]], Stat.NumberOfElements, 0, @RunTimer);
time := RunTimer.LastTime;
rate := RunTimer.PerSec(Stat.NumberOfElements);
log := nil;
case Num[1] of
'1':
begin
Stat.fInsertTime := time;
Stat.fInsertRate := rate;
end;
'2':
begin
Stat.fInsertBatchTime := time;
Stat.fInsertBatchRate := rate;
end;
'3':
begin
Stat.fInsertTransactionTime := time;
Stat.fInsertTransactionRate := rate;
end;
'4':
begin
Stat.fInsertBatchTransactionTime := time;
Stat.fInsertBatchTransactionRate := rate;
break; // still need Client for Read tests
end;
end;
inc(Num[1]);
ClientFree;
end;
// Read tests
log := TSynLog.Enter('% Read One',
[Owner.CurrentMethodInfo^.IdentTestName], self);
Value.ClearProperties;
RunTimer.Start;
for i := 0 to Stat.NumberOfElements - 1 do
begin
// if i = VOIDINDEX then
// Orm.Retrieve(Res[i], Value);
Orm.Retrieve(Res[i], Value);
ValueCheck(i);
end;
NotifyTestSpeed('read one', Stat.NumberOfElements, 0, @RunTimer);
Stat.fReadOneByOneTime := RunTimer.LastTime;
Stat.fReadOneByOneRate := RunTimer.PerSec(Stat.NumberOfElements);
log := nil;
{$ifdef UNIK}
// one by one retrieve values using Name property
log := TSynLog.Enter('% Read Unique',
[Owner.CurrentMethodInfo^.IdentTestName], self);
RunTimer.Start;
for i := 0 to Stat.NumberOfElements - 1 do
begin
Client.Retrieve('LastName=?', [], [ValueLastName[i]], Value);
Check((Value.IDValue = Res[i]) and
(PInt64(@Value.Amount)^ = (i + 1) * 100) and
(Value.LastChange >= ChangeStart));
end;
NotifyTestSpeed('read unique', Stat.NumberOfElements, 0, @RunTimer);
Stat.fReadOneByNameTime := RunTimer.LastTime;
Stat.fReadOneByNameRate := RunTimer.PerSec(Stat.NumberOfElements);
{$endif UNIK}
// retrieve all rows with or without the virtual module
for UseDirect := false to true do
begin
log := nil;
log := TSynLog.Enter('% Read Direct=%',
[Owner.CurrentMethodInfo^.IdentTestName, BOOL_STR[UseDirect]], self);
Server.Server.Cache.Flush; // fair benchmark
Server.DB.CacheFlush; // fair benchmark (16100 rows/s->456000 with cache!)
Server.Server.SetStaticVirtualTableDirect(UseDirect);
RunTimer.Start;
Value.ClearProperties;
Check(Value.FillPrepare(Client.Orm, 'order by RowId'), 'FillPrepare');
//FileFromString((Value.FillTable as TSQLTableJSON).PrivateInternalCopy,Stat.Engine+'.json');
i := 0;
while Value.FillOne do
begin
ValueCheck(i);
inc(i);
end;
CheckEqual(i, Stat.NumberOfElements, 'FillOne');
if UseDirect then
begin
NotifyTestSpeed('read direct', Stat.NumberOfElements, 0, @RunTimer);
Stat.fReadAllDirectTime := RunTimer.LastTime;
Stat.fReadAllDirectRate := RunTimer.PerSec(Stat.NumberOfElements);
end
else
begin
NotifyTestSpeed('read virtual', Stat.NumberOfElements, 0, @RunTimer);
Stat.fReadAllVirtualTime := RunTimer.LastTime;
Stat.fReadAllVirtualRate := RunTimer.PerSec(Stat.NumberOfElements);
end;
end;
end;
function TTestDatabaseAbstract.ModelCreate: TOrmModel;
begin
result := TOrmModel.Create([TOrmSample]);
end;
procedure TTestDatabaseAbstract.ClientCreate;
var
fn: TFileName;
begin
if dbIsFile in Flags then
fn := DBFileName
else
fn := SQLITE_MEMORY_DATABASE_NAME;
Client := TRestClientDB.Create(ModelCreate, nil, fn, TRestServerDB,
{auth=}false, DBPassword);
Client.Model.Owner := Client;
Orm := Client.Client;
Server := (Client as TRestClientDB).Server;
Server.DB.Synchronous := SQlite3Mode;
Server.DB.LockingMode := SQlite3Lock;
end;
procedure TTestDatabaseAbstract.ClientFree;
begin
Orm := nil; // to be freed before Client
FreeAndNil(Client);
end;
procedure TTestDatabaseAbstract.RunWrites(UseTransactions, UseBatch: boolean);
var
i: PtrInt;
forceID: boolean;
begin
DBFileName := FormatString('%db%%.%.db', [Executable.ProgramFilePath,
PathDelim, Owner.CurrentMethodInfo^.MethodName, Num]);
if FileExists(DBFileName) then
DeleteFile(DBFileName);
RunTimer.Start;
ClientCreate;
if CheckFailed(Client <> nil, 'Client?') then
exit; // avoid GPF
Server.Server.CreateMissingTables;
ChangeStart := Server.OrmInstance.GetServerTimestamp; // use by ValueCheck
if Stat.CreateTableTime = '' then
Stat.CreateTableTime := RunTimer.Stop;
if (dbSlowInsert in Flags) and not UseTransactions then // full synch is slow
Stat.NumberOfElements := 200
else if (dbRemoteDB in Flags) and not UseBatch then
if UseTransactions then
Stat.NumberOfElements := 1000
else
Stat.NumberOfElements := 200
else
Stat.NumberOfElements := 10000;
SetLength(ValueLastName, Stat.NumberOfElements);
SetLength(ValueFirstName, Stat.NumberOfElements);
for i := 0 to Stat.NumberOfElements - 1 do
if ValueLastName[i] = '' then
begin
UInt32ToUtf8(i + 1, ValueLastName[i]);
{$ifndef UNIK}
if i <> VOIDINDEX then // test https://synopse.info/fossil/info/e8c211062e
{$endif UNIK}
ValueFirstName[i] := Namee + ValueLastName[i];
end;
RunTimer.Start;
if UseTransactions then
Orm.TransactionBegin(TOrmSample, Client.SessionID);
if UseBatch then
Orm.BatchStart(TOrmSample, {autotrans=}0);
if length(Res) < Stat.NumberOfElements then
SetLength(Res, Stat.NumberOfElements);
for i := 0 to Stat.NumberOfElements - 1 do
begin
Value.Amount := (i + 1) * 0.01;
Value.LastName := ValueLastName[i];
Value.FirstName := ValueFirstName[i];
Value.BirthDate := i + 1;
forceID := false; //forceID := i and 3 = 1;
if forceID then
if {$ifdef UNIK} (dbInMemory in Flags) or {$endif UNIK}
(Res[i - 1] = 0) then
forceID := false // not yet in TRestStorageInMemory.AddOne
else
Value.IDValue := Res[i - 1] + 1;
if UseBatch then
Check(Orm.BatchAdd(Value, {senddata=}true, forceID) >= 0)
else
begin
Res[i] := Orm.Add(Value, true, forceID);
Check(Res[i] > 0, 'Add');
end;
end;
if UseBatch then
Check(Orm.BatchSend(Res) = HTTP_SUCCESS);
if UseTransactions then
Orm.Commit(Client.SessionID);
Value.ClearProperties;
Check(Orm.Retrieve(Res[1], Value), 'One Retrieve after Add');
ValueCheck(1);
end;
procedure TTestDatabaseAbstract.ValueCheck(i: PtrInt);
begin
CheckEqual(Value.IDValue, Res[i], 'ID');
CheckEqual(PInt64(@Value.Amount)^, (i + 1) * 100, 'Amount');
Check(Value.LastChange >= ChangeStart, 'LastChange');
CheckEqual(Value.FirstName, ValueFirstName[i], 'FirstName');
Value.IDValue := 0;
Value.Amount := 0;
Value.FirstName := '';
Value.LastChange := 0;
end;
procedure TTestDatabaseAbstract.RunModeLock(Mode: TSQLSynchronousMode; Lock:
TSQLLockingMode);
begin
SQlite3Mode := Mode;
SQlite3Lock := Lock;
RunTests;
end;
{ TTestDirectSqliteEngine }
procedure TTestDirectSqliteEngine.RunModeLock(
Mode: TSQLSynchronousMode; Lock: TSQLLockingMode);
begin
Flags := [dbIsFile];
if Mode = smFull then
include(Flags, dbSlowInsert);
inherited RunModeLock(Mode, Lock);
end;
procedure TTestDirectSqliteEngine.SqliteFileFull;
begin
RunModeLock(smFull, lmNormal);
end;
procedure TTestDirectSqliteEngine.SqliteFileOff;
begin
RunModeLock(smOff, lmNormal);
end;
procedure TTestDirectSqliteEngine.SqliteFileOffExc;
begin
RunModeLock(smOff, lmExclusive);
end;
procedure TTestDirectSqliteEngine.SqliteFileOffExcAes;
begin
DBPassword := 'password';
RunModeLock(smOff, lmExclusive);
DBPassword := '';
end;
procedure TTestDirectSqliteEngine.SqliteInMemory;
begin
Flags := [];
RunTests;
end;
{ TTestInMemoryEngine }
function TTestInMemoryEngine.ModelCreate: TOrmModel;
begin
result := inherited ModelCreate;
// registration should be done BEFORE Client is initialized
if dbInMemoryVirtual in Flags then
result.VirtualTableRegister(TOrmSample, TOrmVirtualTableBinary)
end;
procedure TTestInMemoryEngine.ClientCreate;
begin
inherited ClientCreate;
if not (dbInMemoryVirtual in Flags) then
OrmMapInMemory(Client.Server.OrmInstance, TOrmSample, DBFileName, true);
end;
procedure TTestInMemoryEngine.InMemoryStatic;
begin
Flags := [dbInMemory];
RunTests;
end;
procedure TTestInMemoryEngine.InMemoryVirtual;
begin
Flags := [dbInMemory, dbInMemoryVirtual];
RunTests;
end;
{ TTestDatabaseExternalAbstract }
function TTestDatabaseExternalAbstract.ModelCreate: TOrmModel;
begin
result := inherited ModelCreate;
// registration should be done BEFORE Client is initialized
OrmMapExternal(result, TOrmSample, Props, 'SampleRecord');
end;
procedure TTestDatabaseExternalAbstract.ClientCreate;
begin
if (Props <> nil) and
(dbDropTable in Flags) then
begin
// clean the connection pool (slower, but validate its logic)
Props.ClearConnectionPool;
Props.ThreadSafeConnection.Disconnect;
// drop this table
try
Props.ExecuteNoResult('drop table SAMPLERECORD', []);
except
// ignore any exception if table does not exist yet (e.g. at first run)
end;
end;
inherited ClientCreate;
end;
procedure TTestDatabaseExternalAbstract.ClientFree;
begin
inherited ClientFree;
if not (dbPropUntouched in Flags) then
FreeAndNil(Props);
end;
procedure TTestDatabaseExternalAbstract.RunExternal(P: TSQLDBConnectionProperties);
begin
Flags := Flags + [dbPropUntouched, dbDropTable, dbPropIsMemory, dbRemoteDB];
Props := P;
try
Props.ThreadSafeConnection.Connect;
Check(Props.ThreadSafeConnection.Connected, 'connected');
RunTests;
finally
FreeAndNil(Props);
end;
end;
{ TTestSqliteExternal }
procedure TTestSqliteExternal.ClientCreate;
var
P: TSqlDBSQLite3ConnectionProperties;
begin
Exclude(Flags, dbDropTable);
if fn <> SQLITE_MEMORY_DATABASE_NAME then
begin
DeleteFile(fn);
P := TSqlDBSQLite3ConnectionProperties.Create(StringToUtf8(fn), '', '', '');
P.MainSQLite3DB.Synchronous := sm;
P.MainSQLite3DB.LockingMode := lm;
Props := P;
end;
inherited ClientCreate;
end;
procedure TTestSqliteExternal.RunExternalSqlite(Mode: TSQLSynchronousMode; Lock:
TSQLLockingMode);
begin
if dbPropIsMemory in Flags then
fn := SQLITE_MEMORY_DATABASE_NAME
else
fn := FormatString('%db%%.db', [Executable.ProgramFilePath, PathDelim,
Owner.CurrentMethodInfo^.MethodName]);
sm := Mode;
lm := Lock;
if Mode = smFull then
include(Flags, dbSlowInsert);
Flags := Flags + [dbPropIsMemory];
RunTests;
end;
procedure TTestSqliteExternal.ExternalSqliteFileFull;
begin
RunExternalSqlite(smFull, lmNormal);
end;
procedure TTestSqliteExternal.ExternalSqliteFileOff;
begin
RunExternalSqlite(smOff, lmNormal);
end;
procedure TTestSqliteExternal.ExternalSqliteFileOffExc;
begin
RunExternalSqlite(smOff, lmExclusive);
end;
procedure TTestSqliteExternal.ExternalSqliteInMemory;
begin
Flags := [dbPropIsMemory];
RunExternalSqlite(smOff, lmExclusive);
end;
{ TTestSqliteRemote }
procedure TTestSqliteRemote.ClientCreate;
begin
// initialize a fast in-memory SQLite3 remote server
RemoteProps := TSQLDBSQLite3ConnectionProperties.Create(
{DBFileName} SQLITE_MEMORY_DATABASE_NAME, '', '', '');
RemoteProps.MainSQLite3DB.Synchronous := SQlite3Mode;
RemoteProps.MainSQLite3DB.LockingMode := SQlite3Lock;
RemoteServer := TSQLDBServerRemote.Create(
RemoteProps, 'root', '8888', 'user', 'password');
// we actually connect to this DB using our remote binary protocol over HTTP
Props := RemoteClient.Create('localhost:8888', 'root', 'user', 'password');
inherited ClientCreate;
end;
procedure TTestSqliteRemote.ClientFree;
begin
inherited ClientFree;
RemoteServer.Free;
RemoteProps.Free;
end;
{$ifdef OSWINDOWS}
procedure TTestSqliteRemote.RemoteSqliteWinHTTP;
begin
RemoteClient := TSQLDBWinHTTPConnectionProperties;
RunTests;
end;
{$endif OSWINDOWS}
procedure TTestSqliteRemote.RemoteSqliteSocket;
begin
RemoteClient := TSQLDBSocketConnectionProperties;
RunTests;
end;
{$ifdef USELOCALPOSTGRESQL}
{ TTestPostgresql }
procedure TTestPostgresql._SynDBPostgres;
begin
RunExternal(TSQLDBPostgresConnectionProperties.Create(
// 'localhost', 'postgres', 'postgres', 'docker'));
'tfb-database:5432', 'hello_world', 'benchmarkdbuser', 'benchmarkdbpass'));
end;
{$ifdef USEZEOS}
procedure TTestPostgresql.ZeosPostgres;
begin
RunExternal(TSQLDBZEOSConnectionProperties.Create(
TSQLDBZEOSConnectionProperties.URI(dPostgreSQL, 'localhost', 'libpq.so.5', false),
'postgres', 'postgres', 'docker'));
end;
{$endif USEZEOS}
{$endif USELOCALPOSTGRESQL}
{$ifdef USELOCALMYSQL}
{ TTestmysql }
{$ifdef USEZEOS}
procedure TTestmysql.Zeosmysql;
begin
RunExternal(TSQLDBZEOSConnectionProperties.Create(
TSQLDBZEOSConnectionProperties.URI(dMySQL, 'localhost', 'libmariadb.dll', false),
'mormot', 'anon', 'nona'));
end;
{$endif USEZEOS}
{$endif USELOCALMYSQL}
{ TTestDatabaseBenchmark }
constructor TTestDatabaseBenchmark.Create(const Ident: string);
var
fn: TFileName;
begin
Stats := TSynObjectList.Create;
fn := ChangeFileExt(Executable.ProgramFileName, '.ini');
if FileExists(fn) then
Ini := StringFromFile(fn)
else
FileFromString('', fn);
inherited Create(Ident);
end;
destructor TTestDatabaseBenchmark.Destroy;
begin
inherited Destroy;
SaveStats;
Stats.Free;
end;
procedure TTestDatabaseBenchmark.SaveStats;
type
TStatArray = array[0..1000] of TStat;
var
Stat: ^TStatArray;
mode, s, txt: RawUtf8;
m, nCat, col1len: integer;
max, Cat1, Cat2, Eng1, Eng2, Eng: RawUtf8;
Rows: TRawUtf8DynArray;
Doc, Cons: RawUtf8;
procedure SetCategories(const Title: RawUtf8; const Cat: array of RawUtf8);
var
i: integer;
begin
mode := UrlEncode(Title);
s := s + '<h1>' + copy(Title, 1, posEx(' (', Title) - 1) + '</h1>'#13#10;
max := Int32ToUtf8(m);
nCat := length(Cat);
Cat1 := '';
Cat2 := '';
SetLength(Rows, Stats.Count + 1);