001: /*
002: * $Id: TestInsertIntoSeveralLargeIndices.java,v 1.4 2005/05/02 22:32:02 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.functional;
042:
043: import java.io.File;
044: import java.sql.Connection;
045: import java.sql.DriverManager;
046: import java.sql.PreparedStatement;
047: import java.sql.SQLException;
048: import java.sql.Statement;
049: import java.util.Random;
050:
051: import junit.framework.Test;
052: import junit.framework.TestSuite;
053:
054: /**
055: * @version $Revision: 1.4 $ $Date: 2005/05/02 22:32:02 $
056: * @author Rodney Waldhoff
057: */
058: public class TestInsertIntoSeveralLargeIndices extends
059: AbstractFunctionalTest {
060:
061: //------------------------------------------------------------ Conventional
062:
063: public TestInsertIntoSeveralLargeIndices(String testName) {
064: super (testName);
065: }
066:
067: public static Test suite() {
068: return new TestSuite(TestInsertIntoSeveralLargeIndices.class);
069: }
070:
071: public static void main(String[] args) throws Exception {
072: TestInsertIntoSeveralLargeIndices test = new TestInsertIntoSeveralLargeIndices(
073: "via main");
074: int rowsToInsert = DEFAULT_TOTAL_INSERTS;
075: int commitEvery = DEFAULT_COMMIT_EVERY;
076: boolean createStringIndices = true;
077: switch (args.length) {
078: case 0:
079: break;
080: case 3:
081: createStringIndices = "true".equals(args[2]);
082: case 2:
083: commitEvery = Integer.parseInt(args[1]);
084: case 1:
085: try {
086: rowsToInsert = Integer.parseInt(args[0]);
087: } catch (NumberFormatException e) {
088: printHelp();
089: return;
090: }
091: break;
092: default:
093: printHelp();
094: return;
095: }
096: test.createSchema(createStringIndices);
097: test.populate(rowsToInsert, commitEvery);
098: }
099:
100: //--------------------------------------------------------------- Lifecycle
101:
102: public void setUp() throws Exception {
103: createSchema(true);
104: }
105:
106: public void tearDown() throws Exception {
107: super .tearDown();
108: }
109:
110: protected String getConnectString() {
111: return "jdbc:axiondb:testdb:testdb";
112: }
113:
114: protected File getDatabaseDirectory() {
115: return new File("testdb");
116: }
117:
118: //------------------------------------------------------------------- Tests
119:
120: public void testPopulate() throws Exception {
121: populate(DEFAULT_TOTAL_INSERTS, DEFAULT_COMMIT_EVERY);
122: }
123:
124: //--------------------------------------------------------------
125:
126: private void createSchema(boolean createStringIndices)
127: throws Exception {
128: Connection conn = DriverManager
129: .getConnection(getConnectString());
130: Statement stmt = conn.createStatement();
131: stmt.execute("create sequence mysequence");
132: stmt
133: .execute("create table mytable ( id integer, columna varchar(10), columnb varchar(10), columnc varchar(10), columnd varchar(10) )");
134: stmt.execute("create btree index indexid on mytable ( id )");
135: if (createStringIndices) {
136: stmt
137: .execute("create btree index indexa on mytable ( columna )");
138: stmt
139: .execute("create btree index indexb on mytable ( columnb )");
140: stmt
141: .execute("create btree index indexc on mytable ( columnc )");
142: stmt
143: .execute("create btree index indexd on mytable ( columnd )");
144: }
145: stmt.close();
146: conn.close();
147: }
148:
149: private void populate(int totalInserts, int commitEvery)
150: throws SQLException {
151: long init = System.currentTimeMillis();
152: Connection conn = DriverManager
153: .getConnection(getConnectString());
154: conn.setAutoCommit(false);
155: PreparedStatement pstmt = conn
156: .prepareStatement("insert into mytable values ( mysequence.nextval, ?, ?, ?, ? )");
157: long last = System.currentTimeMillis();
158: for (int i = 0; i < totalInserts; i++) {
159: if (i != 0 && i % commitEvery == 0) {
160: long commitstart = System.currentTimeMillis();
161: conn.commit();
162: long now = System.currentTimeMillis();
163: printTimes(i, init, last, commitstart, now);
164: last = now;
165: }
166: pstmt.setString(1, createRandomString());
167: pstmt.setString(2, createRandomString());
168: pstmt.setString(3, createRandomString());
169: pstmt.setString(4, createRandomString());
170: pstmt.executeUpdate();
171: }
172: long commitstart = System.currentTimeMillis();
173: conn.commit();
174: long now = System.currentTimeMillis();
175: printTimes(totalInserts, init, last, commitstart, now);
176: pstmt.close();
177: conn.close();
178: System.out.println("Total time: "
179: + (System.currentTimeMillis() - init));
180: }
181:
182: private static String createRandomString() {
183: char[] chars = new char[RANDOM.nextInt(64)];
184: for (int i = 0; i < chars.length; i++) {
185: chars[i] = CHARS[RANDOM.nextInt(CHARS.length)];
186: }
187: return new String(chars);
188: }
189:
190: private static void printTimes(int totalRowCount, long startTime,
191: long timeOfLastCommit, long timeOfCommitStart, long now) {
192: System.out
193: .println(totalRowCount
194: + " records inserted. [total time: "
195: + (now - startTime)
196: + ", time: "
197: + (now - timeOfLastCommit)
198: + "; free: "
199: + Runtime.getRuntime().freeMemory()
200: + "]. Commit time: "
201: + (100L * (now - timeOfCommitStart) / (now - timeOfLastCommit))
202: + "%");
203: }
204:
205: private static void printHelp() {
206: System.out
207: .println("args: [<number of rows to insert> [<number of rows between commits> [<create string indices>]]]");
208: System.out.println("defaults: " + DEFAULT_TOTAL_INSERTS + " "
209: + DEFAULT_COMMIT_EVERY + " true");
210: }
211:
212: //--------------------------------------------------------------
213: private static final Random RANDOM = new Random();
214:
215: private static final char[] CHARS;
216: static {
217: StringBuffer buf = new StringBuffer();
218: for (char c = 'a'; c <= 'z'; c++) {
219: buf.append(c);
220: }
221: for (char c = 'A'; c <= 'Z'; c++) {
222: buf.append(c);
223: }
224: buf.append(" ~!@#$%^&*()-+=");
225: for (char c = '0'; c <= '9'; c++) {
226: buf.append(c);
227: }
228: CHARS = buf.toString().toCharArray();
229: }
230:
231: private static final int DEFAULT_TOTAL_INSERTS = 200000;
232: private static final int DEFAULT_COMMIT_EVERY = 5000;
233: }
|