1
/* Copyright (c) 2001-2005, The HSQL Development Group
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright notice, this
8
* list of conditions and the following disclaimer.
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.
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.
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.
32
package org.hsqldb.test;
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;
43
* Test with small cache and very large row inserts
45
public class TestStressInsert {
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);
53
public void init() throws Exception {
55
String driver = "org.hsqldb.jdbcDriver";
56
String url = "jdbc:hsqldb:file:testing/test";
58
Class.forName(driver);
60
con = DriverManager.getConnection(url, "sa", "");
62
con.setAutoCommit(true);
65
Statement stmt = con.createStatement();
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);
74
DatabaseMetaData metaData = con.getMetaData();
75
ResultSet rs = metaData.getTables(null, null, "A", null);
79
schemaExists = rs.next();
86
"create cached table A (ID binary(16) PRIMARY KEY, DATA varbinary not null)");
89
stmt.execute("checkpoint");
96
con.prepareStatement("insert into A (DATA, ID) values (?, ?)");
99
public void shutdown() throws Exception {
104
public void insert(byte[] id) throws Exception {
107
insertStmt.setBytes(1, data);
108
insertStmt.setBytes(2, id);
109
insertStmt.execute();
111
insertStmt.clearParameters();
112
insertStmt.clearWarnings();
116
public static void main(String[] args) {
119
TestStressInsert test = new TestStressInsert();
120
long t1 = System.currentTimeMillis();
122
System.out.print("Initializing...");
125
long t2 = System.currentTimeMillis();
127
System.out.println("done " + (t2 - t1));
129
for (int i = 0; i < MAX_SIZE; i++) {
130
test.insert(test.getRandomBytes(16));
134
long t3 = System.currentTimeMillis();
135
System.out.println("inserted " + i + " in " + (t3 - t2));
141
} catch (Exception e) {
146
private byte[] getRandomBytes(int length) {
148
byte[] ret = new byte[length];
150
random.nextBytes(ret);