001: /*
002: * $Id: BlobInserts.java,v 1.4 2005/01/27 23:32:02 rwald Exp $
003: * =======================================================================
004: * Copyright (c) 2005 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: package org.axiondb.profile;
041:
042: import java.sql.Connection;
043: import java.sql.DriverManager;
044: import java.sql.PreparedStatement;
045: import java.sql.SQLException;
046: import java.sql.Statement;
047: import java.util.Random;
048:
049: import org.axiondb.types.ByteArrayBlob;
050:
051: /**
052: * Derived from Chas Emerick's 25-Jan-2005 post to the user's list.
053: * @author Chas Emerick
054: * @author Rodney Waldhoff
055: */
056: public class BlobInserts {
057: private static Random _random = new Random();
058:
059: static {
060: try {
061: Class.forName("org.axiondb.jdbc.AxionDriver");
062: } catch (Exception e) {
063: e.printStackTrace();
064: }
065: }
066:
067: public static void main(String[] args) throws Exception {
068: String connectString = "jdbc:axiondb:test:/tmp/testdb"
069: + _random.nextLong();
070: int numLoops = 10000;
071: int reportEvery = 1000;
072: boolean commitEveryBatch = true;
073: boolean useConstraint = false;
074:
075: if (args.length > 0 && "--help".equals(args[0])) {
076: System.out
077: .println("args: <total # of inserts> <batch size> <commit after every batch> <use constraint> <connect string>");
078: System.out.println("defaults: " + numLoops + " "
079: + reportEvery + " " + commitEveryBatch + " "
080: + useConstraint + " " + connectString);
081: return;
082: }
083:
084: switch (args.length) {
085: case 5:
086: connectString = args[4];
087: case 4:
088: useConstraint = Boolean.valueOf(args[3]).booleanValue();
089: case 3:
090: commitEveryBatch = Boolean.valueOf(args[2]).booleanValue();
091: case 2:
092: reportEvery = Integer.parseInt(args[1]);
093: case 1:
094: numLoops = Integer.parseInt(args[0]);
095: break;
096: }
097:
098: System.out.println("Total number of inserts: " + numLoops);
099: System.out.println("Reporting every: " + reportEvery);
100: System.out.println("Commit after each batch? "
101: + commitEveryBatch);
102: System.out.println("Apply constraint? " + useConstraint);
103: System.out.println("Connect String: " + connectString);
104:
105: System.out.println("Running...");
106: System.out.println("");
107:
108: Connection conn = DriverManager.getConnection(connectString);
109: conn.setAutoCommit(false);
110: run(conn, numLoops, reportEvery, commitEveryBatch,
111: useConstraint);
112: conn.close();
113: }
114:
115: private static void run(Connection conn, int loops,
116: int reportEvery, boolean commitEveryBatch,
117: boolean useConstraint) throws SQLException {
118: byte[] data = new byte[25 * 1024];
119: _random.nextBytes(data);
120: {
121: Statement stmt = conn.createStatement();
122: stmt.executeUpdate("create table data_test (id int "
123: + (useConstraint ? "PRIMARY KEY " : "")
124: + ", type_cd varchar(5), filedata blob);");
125: stmt.close();
126: conn.commit();
127: }
128: PreparedStatement pstmt = conn
129: .prepareStatement("insert into data_test (id, type_cd, filedata) values (?, ?, ?)");
130: long start = System.currentTimeMillis();
131: long time = start;
132: for (int i = 1; i <= loops; i++) {
133: pstmt.setInt(1, i);
134: pstmt.setString(2, "t_ind");
135: pstmt.setBlob(3, new ByteArrayBlob(data));
136: pstmt.executeUpdate();
137: if (i % reportEvery == 0) {
138: if (commitEveryBatch) {
139: conn.commit();
140: }
141: long stop = System.currentTimeMillis();
142: System.out.println("Batch of " + reportEvery
143: + " inserts took " + (stop - time) + " ms ["
144: + (((double) stop - time) / reportEvery)
145: + " ms/insert]");
146: time = System.currentTimeMillis();
147: }
148: }
149: conn.commit();
150: long stop = System.currentTimeMillis();
151: System.out.println("Total of " + loops + " inserts took "
152: + (stop - start) + " ms ["
153: + (((double) stop - start) / loops) + " ms/insert]");
154: }
155:
156: }
|