~ubuntu-branches/ubuntu/maverick/hsqldb/maverick

« back to all changes in this revision

Viewing changes to src/org/hsqldb/test/TestStressInsert.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-09-26 11:47:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060926114749-3jd0utm7w21x1iqt
Tags: 1.8.0.5-2ubuntu1
* Synchronise with Debian unstable; remaining changes:
  - build using java-gcj-compat.
* libhsqldb-java: Add gij as alternative dependency.
* Build a libhsqldb-java-gcj package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2001-2005, The HSQL Development Group
 
2
 * All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 * Redistributions of source code must retain the above copyright notice, this
 
8
 * list of conditions and the following disclaimer.
 
9
 *
 
10
 * Redistributions in binary form must reproduce the above copyright notice,
 
11
 * this list of conditions and the following disclaimer in the documentation
 
12
 * and/or other materials provided with the distribution.
 
13
 *
 
14
 * Neither the name of the HSQL Development Group nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from this
 
16
 * software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
21
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 
22
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
 
 
32
package org.hsqldb.test;
 
33
 
 
34
import java.sql.Connection;
 
35
import java.sql.DriverManager;
 
36
import java.sql.PreparedStatement;
 
37
import java.sql.Statement;
 
38
import java.sql.DatabaseMetaData;
 
39
import java.sql.ResultSet;
 
40
import java.util.Random;
 
41
 
 
42
/**
 
43
 * Test with small cache and very large row inserts
 
44
 */
 
45
public class TestStressInsert {
 
46
 
 
47
    private Connection        con;
 
48
    private PreparedStatement insertStmt;
 
49
    private static final int  MAX_SIZE = 800000;
 
50
    private final Random      random   = new Random(0);
 
51
    byte[]                    data     = getRandomBytes(MAX_SIZE);
 
52
 
 
53
    public void init() throws Exception {
 
54
 
 
55
        String driver = "org.hsqldb.jdbcDriver";
 
56
        String url    = "jdbc:hsqldb:file:testing/test";
 
57
 
 
58
        Class.forName(driver);
 
59
 
 
60
        con = DriverManager.getConnection(url, "sa", "");
 
61
 
 
62
        con.setAutoCommit(true);
 
63
 
 
64
        // set cache sizes
 
65
        Statement stmt = con.createStatement();
 
66
 
 
67
        try {
 
68
//            stmt.execute("set property \"hsqldb.nio_data_file\" false");
 
69
            stmt.execute("set property \"hsqldb.cache_scale\" 8");
 
70
            stmt.execute("set property \"hsqldb.cache_size_scale\" 10");
 
71
            stmt.execute("set write_delay 0");
 
72
            stmt.execute("set logsize " + 100);
 
73
 
 
74
            DatabaseMetaData metaData = con.getMetaData();
 
75
            ResultSet        rs = metaData.getTables(null, null, "A", null);
 
76
            boolean          schemaExists;
 
77
 
 
78
            try {
 
79
                schemaExists = rs.next();
 
80
            } finally {
 
81
                rs.close();
 
82
            }
 
83
 
 
84
            if (!schemaExists) {
 
85
                stmt.execute(
 
86
                    "create cached table A (ID binary(16) PRIMARY KEY, DATA varbinary not null)");
 
87
            }
 
88
 
 
89
            stmt.execute("checkpoint");
 
90
        } finally {
 
91
            stmt.close();
 
92
        }
 
93
 
 
94
        // prepare statements
 
95
        insertStmt =
 
96
            con.prepareStatement("insert into A (DATA, ID) values (?, ?)");
 
97
    }
 
98
 
 
99
    public void shutdown() throws Exception {
 
100
        insertStmt.close();
 
101
        con.close();
 
102
    }
 
103
 
 
104
    public void insert(byte[] id) throws Exception {
 
105
 
 
106
        try {
 
107
            insertStmt.setBytes(1, data);
 
108
            insertStmt.setBytes(2, id);
 
109
            insertStmt.execute();
 
110
        } finally {
 
111
            insertStmt.clearParameters();
 
112
            insertStmt.clearWarnings();
 
113
        }
 
114
    }
 
115
 
 
116
    public static void main(String[] args) {
 
117
 
 
118
        try {
 
119
            TestStressInsert test = new TestStressInsert();
 
120
            long     t1   = System.currentTimeMillis();
 
121
 
 
122
            System.out.print("Initializing...");
 
123
            test.init();
 
124
 
 
125
            long t2 = System.currentTimeMillis();
 
126
 
 
127
            System.out.println("done " + (t2 - t1));
 
128
 
 
129
            for (int i = 0; i < MAX_SIZE; i++) {
 
130
                test.insert(test.getRandomBytes(16));
 
131
 
 
132
 
 
133
                if (i %100 == 0 ) {
 
134
                    long t3 = System.currentTimeMillis();
 
135
                    System.out.println("inserted " + i + " in " + (t3 - t2));
 
136
                    t2 = t3;
 
137
                }
 
138
            }
 
139
 
 
140
            test.shutdown();
 
141
        } catch (Exception e) {
 
142
            e.printStackTrace();
 
143
        }
 
144
    }
 
145
 
 
146
    private byte[] getRandomBytes(int length) {
 
147
 
 
148
        byte[] ret = new byte[length];
 
149
 
 
150
        random.nextBytes(ret);
 
151
 
 
152
        return ret;
 
153
    }
 
154
}