001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.test;
032:
033: import java.sql.Connection;
034: import java.sql.DriverManager;
035: import java.sql.PreparedStatement;
036: import java.sql.Statement;
037: import java.sql.DatabaseMetaData;
038: import java.sql.ResultSet;
039: import java.util.Random;
040:
041: /**
042: * Test with small cache and very large row inserts
043: */
044: public class TestStressInsert {
045:
046: private Connection con;
047: private PreparedStatement insertStmt;
048: private static final int MAX_SIZE = 800000;
049: private final Random random = new Random(0);
050: byte[] data = getRandomBytes(MAX_SIZE);
051:
052: public void init() throws Exception {
053:
054: String driver = "org.hsqldb.jdbcDriver";
055: String url = "jdbc:hsqldb:file:testing/test";
056:
057: Class.forName(driver);
058:
059: con = DriverManager.getConnection(url, "sa", "");
060:
061: con.setAutoCommit(true);
062:
063: // set cache sizes
064: Statement stmt = con.createStatement();
065:
066: try {
067: // stmt.execute("set property \"hsqldb.nio_data_file\" false");
068: stmt.execute("set property \"hsqldb.cache_scale\" 8");
069: stmt.execute("set property \"hsqldb.cache_size_scale\" 10");
070: stmt.execute("set write_delay 0");
071: stmt.execute("set logsize " + 100);
072:
073: DatabaseMetaData metaData = con.getMetaData();
074: ResultSet rs = metaData.getTables(null, null, "A", null);
075: boolean schemaExists;
076:
077: try {
078: schemaExists = rs.next();
079: } finally {
080: rs.close();
081: }
082:
083: if (!schemaExists) {
084: stmt
085: .execute("create cached table A (ID binary(16) PRIMARY KEY, DATA varbinary not null)");
086: }
087:
088: stmt.execute("checkpoint");
089: } finally {
090: stmt.close();
091: }
092:
093: // prepare statements
094: insertStmt = con
095: .prepareStatement("insert into A (DATA, ID) values (?, ?)");
096: }
097:
098: public void shutdown() throws Exception {
099: insertStmt.close();
100: con.close();
101: }
102:
103: public void insert(byte[] id) throws Exception {
104:
105: try {
106: insertStmt.setBytes(1, data);
107: insertStmt.setBytes(2, id);
108: insertStmt.execute();
109: } finally {
110: insertStmt.clearParameters();
111: insertStmt.clearWarnings();
112: }
113: }
114:
115: public static void main(String[] args) {
116:
117: try {
118: TestStressInsert test = new TestStressInsert();
119: long t1 = System.currentTimeMillis();
120:
121: System.out.print("Initializing...");
122: test.init();
123:
124: long t2 = System.currentTimeMillis();
125:
126: System.out.println("done " + (t2 - t1));
127:
128: for (int i = 0; i < MAX_SIZE; i++) {
129: test.insert(test.getRandomBytes(16));
130:
131: if (i % 100 == 0) {
132: long t3 = System.currentTimeMillis();
133: System.out.println("inserted " + i + " in "
134: + (t3 - t2));
135: t2 = t3;
136: }
137: }
138:
139: test.shutdown();
140: } catch (Exception e) {
141: e.printStackTrace();
142: }
143: }
144:
145: private byte[] getRandomBytes(int length) {
146:
147: byte[] ret = new byte[length];
148:
149: random.nextBytes(ret);
150:
151: return ret;
152: }
153: }
|