gnoga_2.1.2_5f127c56/deps/simple_components/test_components/test_sqlite_benchmark.adb

  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
--                                                                    --
--  procedure                       Copyright (c)  Dmitry A. Kazakov  --
--     Test_SQLite_Benchmark                       Luebeck            --
--  Benchmark SQLite vs B-trees                    Autumn, 2014       --
--                                                                    --
--                                Last revision :  13:13 14 Sep 2019  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, or (at your option) any later version. This library  --
--  is distributed in the hope that it will be useful,  but  WITHOUT  --
--  ANY   WARRANTY;   without   even   the   implied   warranty   of  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.Calendar;           use Ada.Calendar;
with Ada.Exceptions;         use Ada.Exceptions;
with Ada.Text_IO;            use Ada.Text_IO;
with Ada.Strings.Fixed;      use Ada.Strings.Fixed;
with Strings_Edit.Floats;    use Strings_Edit.Floats;
with Strings_Edit.Integers;  use Strings_Edit.Integers;
with SQLite;                 use SQLite;

with Ada.Numerics.Discrete_Random;
with Persistent.Blocking_Files.Transactional.Dump;
with Persistent.Memory_Pools.Dump;
with Test_String_B_Trees;

procedure Test_SQLite_Benchmark is
   Count   : constant := 1000;

   use Test_String_B_Trees.External;
   use Persistent.Blocking_Files;
   use Persistent.Blocking_Files.Transactional;
   use Persistent.Blocking_Files.Transactional.Dump;
   use Persistent.Memory_Pools;
   use Persistent.Memory_Pools.Dump;

   subtype Token_Length is Integer range 1..100;
   subtype Token_Letter is Character range 'A'..'Z';
   package Random_Letter is
      new Ada.Numerics.Discrete_Random (Token_Letter);
   use Random_Letter;
   package Random_Length is
      new Ada.Numerics.Discrete_Random (Token_Length);
   use Random_Length;

   type Benchmark_Type is mod 2**2;
   Benchmark_SQLite : constant Benchmark_Type := 2**0;
   Benchmark_B_Tree : constant Benchmark_Type := 2**1;
   Benchmark_All    : constant Benchmark_Type := Benchmark_Type'Last;

--   Benchmark : constant Benchmark_Type := Benchmark_B_Tree;
   Benchmark : constant Benchmark_Type := Benchmark_All;

   Letter_Dice  : Random_Letter.Generator;
   Letter_State : Random_Letter.State;
   Length_Dice  : Random_Length.Generator;
   Length_State : Random_Length.State;

   function Create_Key (Index : Natural) return String is
      Result : String (1..Random (Length_Dice));
   begin
      for Index in Result'Range loop
         Result (Index) := Random (Letter_Dice);
      end loop;
      return Result;
   end Create_Key;

   function Create_Value (Index : Natural) return String
      renames Create_Key;

   Start        : Time;
   Tree_Address : Byte_Index;
   Seconds      : Duration;
begin
   Reset (Letter_Dice, 1);
   Save  (Letter_Dice, Letter_State);
   Reset (Length_Dice, 1);
   Save  (Length_Dice, Length_State);
------------------------------------------------------------------------
   Put_Line ("Insertion test");
   if 0 /= (Benchmark and Benchmark_SQLite) then
      declare
         DB : Data_Base;
      begin
         DB := Open ("sqlite_benchmark.db");
         Exec (DB, "PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL");
         Exec (DB, "DROP TABLE IF EXISTS test_table");
         Exec
         (  DB,
            "CREATE TABLE test_table (key TEXT PRIMARY KEY, value TEXT)"
         );
         declare
            Command : constant Statement :=
                      Prepare
                      (  DB,
                         "INSERT INTO test_table VALUES (?, ?)"
                      );
         begin
            Reset (Letter_Dice, Letter_State);
            Reset (Length_Dice, Length_State);
            Start := Clock;
            for Index in 1..Count loop
               Exec (DB, "BEGIN TRANSACTION;");
               declare
                  Key   : aliased String := Create_Key (Index);
                  Value : aliased String := Create_Value (Index);
               begin
                  Bind (Command, 1, Key'Access);
                  Bind (Command, 2, Value'Access);
                  Step (Command);
                  Reset (Command);
               end;
               Exec (DB, "COMMIT");
            end loop;
            Seconds := Clock - Start;
         end;
         Put_Line
         (  "SQLite "
         &  Image (Count)
         &  " prepared INSERTs, "
         &  Image (1000.0 * (Float (Seconds) / Float (Count)))
         &  "ms per operation"
         );
      end;
   end if;
   if 0 /= (Benchmark and Benchmark_B_Tree) then
      declare
         DB : aliased Persistent_Transactional_Array;
      begin
         Open
         (  Container => DB,
            Name      => "test_benchmark.db",
            Mode      => Create_Mode,
            Map_Size  => Get_Map_Size (Count),
            Hash_Size => 1024
         );
         declare
            Pool : aliased Persistent_Pool (DB'Access);
            Tree : B_Tree (Pool'Access);
         begin
            Reset (Letter_Dice, Letter_State);
            Reset (Length_Dice, Length_State);
            Start := Clock;
            for Index in 1..Count loop
               declare
                  Key   : aliased constant String := Create_Key (Index);
                  Value : aliased constant String :=
                                           Create_Value (Index);
               begin
                  Add (Tree, Key, Value);
                  Commit (Pool);
               end;
            end loop;
            Seconds := Clock - Start;
            Tree_Address := Get_Root_Address (Tree);
            Put_Line
            (  "Blocking file "
            &  Image (Count)
            &  " committed insertions, "
            &  Image (1000.0 * (Float (Seconds) / Float (Count)))
            &  "ms per operation. "
            &  "File size"
            &  Block_Count'Image (Get_Block_Size (DB))
            );
         end;
      end;
   end if;
------------------------------------------------------------------------
   Put_Line ("Search test");
   if 0 /= (Benchmark and Benchmark_SQLite) then
      declare
         DB : Data_Base;
      begin
         DB := Open ("sqlite_benchmark.db", READWRITE or FULLMUTEX);
         Exec (DB, "PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL");
         declare
            Command : constant Statement :=
                      Prepare
                      (  DB,
                         "SELECT value FROM test_table WHERE key=?"
                      );
         begin
            Reset (Letter_Dice, Letter_State);
            Reset (Length_Dice, Length_State);
            Start := Clock;
            for Index in 1..Count loop
               declare
                  Key   : aliased String := Create_Key (Index);
                  Value : aliased String := Create_Value (Index);
               begin
                  Bind (Command, 1, Key'Access);
                  Step (Command);
                  if Value /= Column (Command, 1) then
                     Raise_Exception
                     (  Data_Error'Identity,
                        (  "Error in SELECT, searched for "
                        &  Key
                        &  ", found "
                        &  Column (Command, 1)
                        &  ", expected "
                        &  Value
                     )  );
                  end if;
                  Reset (Command);
               end;
            end loop;
            Seconds := Clock - Start;
         end;
         Put_Line
         (  "SQLite "
         &  Image (Count)
         &  " prepared committed SELECTs, "
         &  Image (1000.0 * (Float (Seconds) / Float (Count)))
         &  "ms per operation"
         );
      end;
   end if;
   if 0 /= (Benchmark and Benchmark_B_Tree) then
      declare
         DB : aliased Persistent_Transactional_Array;
      begin
         Open
         (  Container => DB,
            Name      => "test_benchmark.db",
            Mode      => Read_Mode,
            Map_Size  => Get_Map_Size (Count),
            Hash_Size => 1024
         );
         declare
            Pool : aliased Persistent_Pool (DB'Access);
            Tree : B_Tree (Pool'Access);
         begin
            Set_Root_Address (Tree, Tree_Address);
            Reset (Letter_Dice, Letter_State);
            Reset (Length_Dice, Length_State);
            Start := Clock;
            for Index in 1..Count loop
               declare
                  Key   : aliased String := Create_Key (Index);
                  Value : aliased String := Create_Value (Index);
               begin
                  if Get (Tree, Key) /= Value then
                     Raise_Exception
                     (  Data_Error'Identity,
                        (  "Error in Find, searched for "
                        &  Key
                        &  ", found "
                        &  Get (Tree, Key)
                        &  ", expected "
                        &  Value
                     )  );
                  end if;
               end;
            end loop;
            Seconds := Clock - Start;
            Put_Line
            (  "Blocking file "
            &  Image (Count)
            &  " searches, "
            &  Image (1000.0 * (Float (Seconds) / Float (Count)))
            &  "ms per operation"
            );
         end;
      end;
   end if;
------------------------------------------------------------------------
   Put_Line ("Update test");
   if 0 /= (Benchmark and Benchmark_SQLite) then
      declare
         DB : Data_Base;
      begin
         DB := Open ("sqlite_benchmark.db", READWRITE or FULLMUTEX);
         Exec (DB, "PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL");
         declare
            Command : constant Statement :=
                      Prepare
                      (  DB,
                         "UPDATE test_table SET value=? WHERE key=?"
                      );
         begin
            Reset (Letter_Dice, Letter_State);
            Reset (Length_Dice, Length_State);
            Start := Clock;
            for Index in 1..Count loop
               Exec (DB, "BEGIN TRANSACTION;");
               declare
                  Key   : aliased String := Create_Key (Index);
                  Value : aliased String := 2 * Create_Value (Index);
               begin
                  Bind (Command, 1, Value'Access);
                  Bind (Command, 2, Key'Access);
                  Step (Command);
                  Reset (Command);
               end;
               Exec (DB, "COMMIT");
            end loop;
            Seconds := Clock - Start;
         end;
         Put_Line
         (  "SQLite "
         &  Image (Count)
         &  " prepared committed UPDATESs, "
         &  Image (1000.0 * (Float (Seconds) / Float (Count)))
         &  "ms per operation"
         );
      end;
   end if;
   if 0 /= (Benchmark and Benchmark_B_Tree) then
      declare
         DB : aliased Persistent_Transactional_Array;
      begin
         Open
         (  Container => DB,
            Name      => "test_benchmark.db",
            Mode      => Read_Write_Mode,
            Map_Size  => Get_Map_Size (Count),
            Hash_Size => 1024
         );
         declare
            Pool : aliased Persistent_Pool (DB'Access);
            Tree : B_Tree (Pool'Access);
         begin
            Set_Root_Address (Tree, Tree_Address);
            Reset (Letter_Dice, Letter_State);
            Reset (Length_Dice, Length_State);
            Start := Clock;
            for Index in 1..Count loop
               declare
                  Key   : aliased String := Create_Key (Index);
                  Value : aliased String := 2 * Create_Value (Index);
               begin
                  Replace (Tree, Key, Value);
                  Commit (Pool);
               end;
            end loop;
            Seconds := Clock - Start;
            Put_Line
            (  "Blocking file "
            &  Image (Count)
            &  " committed updates, "
            &  Image (1000.0 * (Float (Seconds) / Float (Count)))
            &  "ms per operation"
            );
         end;
      end;
   end if;
------------------------------------------------------------------------
   Put_Line ("Delete test");
   if 0 /= (Benchmark and Benchmark_SQLite) then
      declare
         DB : Data_Base;
      begin
         DB := Open ("sqlite_benchmark.db", READWRITE or FULLMUTEX);
         Exec (DB, "PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL");
         declare
            Command : constant Statement :=
                      Prepare
                      (  DB,
                         "DELETE FROM test_table WHERE key=?"
                      );
         begin
            Reset (Letter_Dice, Letter_State);
            Reset (Length_Dice, Length_State);
            Start := Clock;
            for Index in 1..Count / 2 loop
--               Exec (DB, "BEGIN TRANSACTION;");
               declare
                  Key   : aliased String := Create_Key (Index);
                  Value : aliased String := Create_Value (Index);
               begin
                  Bind (Command, 1, Key'Access);
                  Step (Command);
                  Reset (Command);
               end;
--               Exec (DB, "COMMIT");
            end loop;
            Seconds := Clock - Start;
         end;
         Put_Line
         (  "SQLite "
         &  Image (Count / 2)
         &  " prepared committed DELETEs, "
         &  Image (1000.0 * (Float (Seconds) / Float (Count / 2)))
         &  "ms per operation"
         );
      end;
   end if;
   if 0 /= (Benchmark and Benchmark_B_Tree) then
      declare
         DB : aliased Persistent_Transactional_Array;
      begin
         Open
         (  Container => DB,
            Name      => "test_benchmark.db",
            Mode      => Read_Write_Mode,
            Map_Size  => Get_Map_Size (Count),
            Hash_Size => 1024
         );
         declare
            Pool : aliased Persistent_Pool (DB'Access);
            Tree : B_Tree (Pool'Access);
         begin
            Set_Root_Address (Tree, Tree_Address);
            Reset (Letter_Dice, Letter_State);
            Reset (Length_Dice, Length_State);
            Start := Clock;
            for Index in 1..Count / 2 loop
               declare
                  Key   : aliased constant String := Create_Key (Index);
                  Value : aliased String := Create_Value (Index);
               begin
                  Remove (Tree, Key);
                  Commit (Pool);
               end;
            end loop;
            Seconds := Clock - Start;
            Put_Line
            (  "Blocking file "
            &  Image (Count / 2)
            &  " committed deletes, "
            &  Image (1000.0 * (Float (Seconds) / Float (Count / 2)))
            &  "ms per operation"
            );
         end;
      end;
   end if;
exception
   when Error : others =>
      Put_Line ("Fault: " & Exception_Information (Error));
end Test_SQLite_Benchmark;