summaryrefslogtreecommitdiffstats
path: root/connectivity/qa/complex/connectivity/hsqldb/TestCacheSize.java
blob: d3c802e15a403d5203d40b150d8710299c2c423c (plain)
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
/*
 * This file is part of the LibreOffice project.
 *
 * Copyright (c) 2001-2004, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


package complex.connectivity.hsqldb;



import org.hsqldb.lib.StopWatch;

import java.util.Random;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.sdbc.*;

/**
 * Test large cached tables by setting up a cached table of 100000 records
 * or more and a much smaller memory table with about 1/100th rows used.
 * Populate both tables so that an indexed column of the cached table has a
 * foreign key reference to the main table.
 *
 * This database can be used to demonstrate efficient queries to retrieve
 * the data from the cached table.
 *
 * 1.7.1 insert timings for 100000 rows, cache scale 12:
 * simple table, no extra index: 52 s
 * with index on lastname only: 56 s
 * with index on zip only: 211 s
 * foreign key, referential_integrity true: 216 s
 *
 * The above have improved a lot in 1.7.2
 *
 * This test now incorporates the defunct TestTextTables
 *
 * @version 1.7.2
 * @since 1.7.0
 */
public class TestCacheSize {

    // program can edit the *.properties file to set cache_size
    private boolean filedb = true;

    // shutdown performed mid operation - not for mem: or hsql: URL's
    private boolean shutdown = true;

    // fixed
    private String url = "sdbc:embedded:hsqldb";

    // frequent reporting of progress
    private boolean reportProgress = false;

    // type of the big table {MEMORY | CACHED | TEXT}
    private String tableType      = "CACHED";
    private int    cacheScale     = 17;

    // script format {TEXT, BINARY, COMPRESSED}
    private String logType       = "TEXT";
    private int     writeDelay    = 60;
    private boolean indexZip      = true;
    private boolean indexLastName = false;
    private boolean addForeignKey = false;
    private boolean refIntegrity  = true;

    // speeds up inserts when tableType=="CACHED"
    private boolean createTempTable = false;

    // introduces fragmentation to the .data file during insert
    private boolean deleteWhileInsert         = false;
    private int     deleteWhileInsertInterval = 10000;

    // size of the tables used in test
    private int bigrows   = 10000;
    private int smallrows = 0xfff;

    // if the extra table needs to be created and filled up
    private boolean multikeytable = false;


    private XStatement  sStatement;
    private XConnection cConnection;
    private XDriver drv;
    private com.sun.star.beans.PropertyValue[] info;

    public TestCacheSize(com.sun.star.beans.PropertyValue[] _info,XDriver _drv){
        drv = _drv;
        info = _info;
    }

    public void setURL(String _url){
        url = _url;
    }

    public void setUp() {

        try {
            sStatement  = null;
            cConnection = null;

            if (filedb) {

                cConnection = drv.connect(url,info);
                sStatement = cConnection.createStatement();

                sStatement.execute("SET SCRIPTFORMAT " + logType);
                sStatement.execute("SET LOGSIZE " + 0);
                sStatement.execute("SHUTDOWN");
                cConnection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("TestSql.setUp() error: " + e.getMessage());
        }
    }

    /**
     * Fill up the cache
     *
     *
     */
    public void testFillUp() {

        StopWatch sw = new StopWatch();
        String ddl1 = "DROP TABLE test IF EXISTS;"
                      + "DROP TABLE zip IF EXISTS;";
        String ddl2 = "CREATE CACHED TABLE zip( zip INT IDENTITY );";
        String ddl3 = "CREATE " + tableType + " TABLE test( id INT IDENTITY,"
                      + " firstname VARCHAR, " + " lastname VARCHAR, "
                      + " zip INTEGER, " + " filler VARCHAR); ";
        String ddl31 = "SET TABLE test SOURCE \"test.csv;cache_scale="
                       + cacheScale + "\";";

        // adding extra index will slow down inserts a bit
        String ddl4 = "CREATE INDEX idx1 ON TEST (lastname);";

        // adding this index will slow down  inserts a lot
        String ddl5 = "CREATE INDEX idx2 ON TEST (zip);";

        // referential integrity checks will slow down inserts a bit
        String ddl6 =
            "ALTER TABLE test add constraint c1 FOREIGN KEY (zip) REFERENCES zip(zip);";
        String ddl7 = "CREATE TEMP TABLE temptest( id INT,"
                      + " firstname VARCHAR, " + " lastname VARCHAR, "
                      + " zip INTEGER, " + " filler VARCHAR); ";
        String filler =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
            + "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String mddl1 = "DROP TABLE test2 IF EXISTS;";
        String mddl2 = "CREATE " + tableType
                       + " TABLE test2( id1 INT, id2 INT,"
                       + " firstname VARCHAR, " + " lastname VARCHAR, "
                       + " zip INTEGER, " + " filler VARCHAR, "
                       + " PRIMARY KEY (id1,id2) ); ";
        String mdd13 = "SET TABLE test2 SOURCE \"test2.csv;cache_scale="
                       + cacheScale + "\";";

        try {
            System.out.println("Connecting");
            sw.zero();

            cConnection = null;
            sStatement  = null;
            cConnection = drv.connect(url,info);

            System.out.println("connected: " + sw.elapsedTime());
            sw.zero();

            sStatement = cConnection.createStatement();

            java.util.Random randomgen = new java.util.Random();

            sStatement.execute("SET WRITE_DELAY " + writeDelay);
            sStatement.execute(ddl1);
            sStatement.execute(ddl2);
            sStatement.execute(ddl3);

            if (tableType.equals("TEXT")) {
                sStatement.execute(ddl31);
            }

            System.out.println("test table with no index");

            if (indexLastName) {
                sStatement.execute(ddl4);
                System.out.println("create index on lastname");
            }

            if (indexZip) {
                sStatement.execute(ddl5);
                System.out.println("create index on zip");
            }

            if (addForeignKey) {
                sStatement.execute(ddl6);
                System.out.println("add foreign key");
            }

            if (createTempTable) {
                sStatement.execute(ddl7);
                System.out.println("temp table");
            }

            if (multikeytable) {
                sStatement.execute(mddl1);
                sStatement.execute(mddl2);

                if (tableType.equals("TEXT")) {
                    sStatement.execute(mdd13);
                }

                System.out.println("multi key table");
            }

            System.out.println("Setup time: " + sw.elapsedTime());
            fillUpBigTable(filler, randomgen);

            if (multikeytable) {
                fillUpMultiTable(filler, randomgen);
            }

            sw.zero();

            if (shutdown) {
                sStatement.execute("SHUTDOWN");
                System.out.println("Shutdown Time: " + sw.elapsedTime());
            }

            cConnection.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

    private void fillUpBigTable(String filler,
                                Random randomgen) throws SQLException {

        StopWatch sw = new StopWatch();
        int       i;

        for (i = 0; i <= smallrows; i++) {
            sStatement.execute("INSERT INTO zip VALUES(null);");
        }

        sStatement.execute("SET REFERENTIAL_INTEGRITY " + this.refIntegrity
                           + ";");

        XPreparedStatement ps = cConnection.prepareStatement(
            "INSERT INTO test (firstname,lastname,zip,filler) VALUES (?,?,?,?)");

        XParameters para = UnoRuntime.queryInterface(XParameters.class,ps);
        para.setString(1, "Julia");
        para.setString(2, "Clancy");

        for (i = 0; i < bigrows; i++) {
            para.setInt(3, randomgen.nextInt(smallrows));

            long nextrandom   = randomgen.nextLong();
            int  randomlength = (int) nextrandom & 0x7f;

            if (randomlength > filler.length()) {
                randomlength = filler.length();
            }

            String varfiller = filler.substring(0, randomlength);

            para.setString(4, nextrandom + varfiller);
            ps.execute();

            if (reportProgress && (i + 1) % 10000 == 0) {
                System.out.println("Insert " + (i + 1) + " : "
                                   + sw.elapsedTime());
            }

            // delete and add 4000 rows to introduce fragmentation
            if (deleteWhileInsert && i != 0
                    && i % deleteWhileInsertInterval == 0) {
                sStatement.execute("CALL IDENTITY();");

                XMultipleResults mrs = UnoRuntime.queryInterface(XMultipleResults.class,sStatement);
                XResultSet rs = mrs.getResultSet();

                rs.next();

                XRow row = UnoRuntime.queryInterface(XRow.class,rs);
                int lastId = row.getInt(1);

                sStatement.execute(
                    "SELECT * INTO TEMP tempt FROM test WHERE id > "
                    + (lastId - 4000) + " ;");
                sStatement.execute("DELETE FROM test WHERE id > "
                                   + (lastId - 4000) + " ;");
                sStatement.execute("INSERT INTO test SELECT * FROM tempt;");
                sStatement.execute("DROP TABLE tempt;");
            }
        }

        System.out.println("Total insert: " + i);
        System.out.println("Insert time: " + sw.elapsedTime() + " rps: "
                           + (1000L * i / (sw.elapsedTime() + 1)));
    }

    private void fillUpMultiTable(String filler,
                                  Random randomgen) throws SQLException {

        StopWatch sw = new StopWatch();
        int       i;
        XPreparedStatement ps = cConnection.prepareStatement(
            "INSERT INTO test2 (id1, id2, firstname,lastname,zip,filler) VALUES (?,?,?,?,?,?)");

        XParameters para = UnoRuntime.queryInterface(XParameters.class,ps);
        para.setString(3, "Julia");
        para.setString(4, "Clancy");

        int id1 = 0;

        for (i = 0; i < bigrows; i++) {
            int id2 = randomgen.nextInt(Integer.MAX_VALUE);

            if (i % 1000 == 0) {
                id1 = randomgen.nextInt(Integer.MAX_VALUE);
            }

            para.setInt(1, id1);
            para.setInt(2, id2);
            para.setInt(5, randomgen.nextInt(smallrows));

            long nextrandom   = randomgen.nextLong();
            int  randomlength = (int) nextrandom & 0x7f;

            if (randomlength > filler.length()) {
                randomlength = filler.length();
            }

            String varfiller = filler.substring(0, randomlength);

            para.setString(6, nextrandom + varfiller);

            try {
                ps.execute();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            if (reportProgress && (i + 1) % 10000 == 0) {
                System.out.println("Insert " + (i + 1) + " : "
                                   + sw.elapsedTime());
            }
        }

        System.out.println("Multi Key Total insert: " + i);
        System.out.println("Insert time: " + sw.elapsedTime() + " rps: "
                           + (1000L * i / (sw.elapsedTime() + 1)));
    }

    public void tearDown() {}

    public void checkResults() {

        try {
            StopWatch sw = new StopWatch();
            XResultSet rs;

            cConnection = drv.connect(url,info);

            System.out.println("Reopened database: " + sw.elapsedTime());
            sw.zero();

            sStatement = cConnection.createStatement();

            sStatement.execute("SET WRITE_DELAY " + writeDelay);

            // the tests use different indexes
            // use primary index
            sStatement.execute("SELECT count(*) from TEST");

            XMultipleResults mrs = UnoRuntime.queryInterface(XMultipleResults.class,sStatement);
            rs = mrs.getResultSet();
            XRow row = UnoRuntime.queryInterface(XRow.class,rs);

            rs.next();
            System.out.println("Row Count: " + row.getInt(1));
            System.out.println("Time to count: " + sw.elapsedTime());

            // use index on zip
            sw.zero();
            sStatement.execute("SELECT count(*) from TEST where zip > -1");

            rs = mrs.getResultSet();

            rs.next();
            System.out.println("Row Count: " + row.getInt(1));
            System.out.println("Time to count: " + sw.elapsedTime());
            checkSelects();
            checkUpdates();
            checkSelects();
            sw.zero();
            sStatement.execute("SELECT count(*) from TEST where zip > -1");

            rs = mrs.getResultSet();

            rs.next();
            System.out.println("Row Count: " + row.getInt(1));
            System.out.println("Time to count: " + sw.elapsedTime());
            sw.zero();

            if (shutdown) {
                sStatement.execute("SHUTDOWN");
                System.out.println("Shutdown Time: " + sw.elapsedTime());
            }

            cConnection.close();
            System.out.println("Closed database: " + sw.elapsedTime());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void checkSelects() {

        StopWatch        sw        = new StopWatch();
        java.util.Random randomgen = new java.util.Random();
        int              i         = 0;
        boolean          slow      = false;

        try {
            XPreparedStatement ps = cConnection.prepareStatement(
                "SELECT TOP 1 firstname,lastname,zip,filler FROM test WHERE zip = ?");
            XParameters para = UnoRuntime.queryInterface(XParameters.class,ps);

            for (; i < bigrows; i++) {
                para.setInt(1, randomgen.nextInt(smallrows));
                ps.execute();

                if ((i + 1) == 100 && sw.elapsedTime() > 5000) {
                    slow = true;
                }

                if (reportProgress && (i + 1) % 10000 == 0
                        || (slow && (i + 1) % 100 == 0)) {
                    System.out.println("Select " + (i + 1) + " : "
                                       + sw.elapsedTime() + " rps: "
                                       + (1000L * i / (sw.elapsedTime() + 1)));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("Select random zip " + i + " rows : "
                           + sw.elapsedTime() + " rps: "
                           + (1000L * i / (sw.elapsedTime() + 1)));
        sw.zero();

        try {
            XPreparedStatement ps = cConnection.prepareStatement(
                "SELECT firstname,lastname,zip,filler FROM test WHERE id = ?");
            XParameters para = UnoRuntime.queryInterface(XParameters.class,ps);

            for (i = 0; i < bigrows; i++) {
                para.setInt(1, randomgen.nextInt(bigrows - 1));
                ps.execute();

                if (reportProgress && (i + 1) % 10000 == 0
                        || (slow && (i + 1) % 100 == 0)) {
                    System.out.println("Select " + (i + 1) + " : "
                                       + (sw.elapsedTime() + 1));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("Select random id " + i + " rows : "
                           + sw.elapsedTime() + " rps: "
                           + (1000L * i / (sw.elapsedTime() + 1)));
        sw.zero();

        try {
            XPreparedStatement ps = cConnection.prepareStatement(
                "SELECT zip FROM zip WHERE zip = ?");
            XParameters para = UnoRuntime.queryInterface(XParameters.class,ps);

            for (i = 0; i < bigrows; i++) {
                para.setInt(1, randomgen.nextInt(smallrows - 1));
                ps.execute();

                if (reportProgress && (i + 1) % 10000 == 0
                        || (slow && (i + 1) % 100 == 0)) {
                    System.out.println("Select " + (i + 1) + " : "
                                       + (sw.elapsedTime() + 1));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("Select random zip from zip table " + i
                           + " rows : " + sw.elapsedTime() + " rps: "
                           + (1000L * i / (sw.elapsedTime() + 1)));
    }

    private void checkUpdates() {

        StopWatch        sw        = new StopWatch();
        java.util.Random randomgen = new java.util.Random();
        int              i         = 0;
        boolean          slow      = false;
        int              count     = 0;

        try {
            XPreparedStatement ps = cConnection.prepareStatement(
                "UPDATE test SET filler = filler || zip WHERE zip = ?");
            XParameters para = UnoRuntime.queryInterface(XParameters.class,ps);

            for (; i < smallrows; i++) {
                int random = randomgen.nextInt(smallrows - 1);

                para.setInt(1, random);

                count += ps.executeUpdate();

                if (reportProgress && count % 10000 < 20) {
                    System.out.println("Update " + count + " : "
                                       + (sw.elapsedTime() + 1));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("Update with random zip " + i
                           + " UPDATE commands, " + count + " rows : "
                           + sw.elapsedTime() + " rps: "
                           + (1000L * count / (sw.elapsedTime() + 1)));
        sw.zero();

        try {
            XPreparedStatement ps = cConnection.prepareStatement(
                "UPDATE test SET zip = zip + 1 WHERE id = ?");
            XParameters para = UnoRuntime.queryInterface(XParameters.class,ps);

            for (i = 0; i < bigrows; i++) {
                int random = randomgen.nextInt(bigrows - 1);

                para.setInt(1, random);
                ps.execute();

                if (reportProgress && (i + 1) % 10000 == 0
                        || (slow && (i + 1) % 100 == 0)) {
                    System.out.println("Update " + (i + 1) + " : "
                                       + sw.elapsedTime() + " rps: "
                                       + (1000L * i / (sw.elapsedTime() + 1)));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("Update with random id " + i + " rows : "
                           + sw.elapsedTime() + " rps: "
                           + (1000L * i / (sw.elapsedTime() + 1)));
    }
}