001: /*
002: * $Id: PerformanceTester.java,v 1.1 2005/06/30 01:14:45 ahimanikya Exp $
003: * ======================================================================= Copyright (c)
004: * 2005 Axion Development Team. All rights reserved. Redistribution and use in source and
005: * binary forms, with or without modification, are permitted provided that the following
006: * conditions are met: 1. Redistributions of source code must retain the above copyright
007: * notice, this list of conditions and the following disclaimer. 2. Redistributions in
008: * binary form must reproduce the above copyright notice, this list of conditions and the
009: * following disclaimer in the documentation and/or other materials provided with the
010: * distribution. 3. The names "Tigris", "Axion", nor the names of its contributors may not
011: * be used to endorse or promote products derived from this software without specific
012: * prior written permission. 4. Products derived from this software may not be called
013: * "Axion", nor may "Tigris" or "Axion" appear in their names without specific prior
014: * written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
015: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
016: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
017: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
018: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
019: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
020: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
021: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
022: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
023: * OF SUCH DAMAGE. =======================================================================
024: */
025: package org.axiondb.ext.indexes.ttree;
026:
027: import java.sql.Connection;
028: import java.sql.DriverManager;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Statement;
033:
034: public class PerformanceTester {
035:
036: private class Result {
037: long empty;
038: long popul;
039: long query;
040: long range;
041: }
042:
043: public static void main(String[] args) throws Exception {
044: String url = "jdbc:axiondb:egdb";
045: if (args.length >= 1) {
046: url += ":" + args[0];
047: }
048: //else {
049: // url += ":testdb";
050: //}
051: int n = 40000; //99999;
052: if (args.length >= 2) {
053: n = Integer.parseInt(args[1]);
054: }
055: n += n * Math.random();
056: System.out.println("Iterations: " + n);
057: System.out.println("Connecting: " + url);
058: new PerformanceTester(url, n);
059: }
060:
061: private Result bb = new Result();
062: private Result bu = new Result();
063: private boolean cmp = true;
064: private Connection conn;
065: private final int NITERS;
066: private boolean pre = true;
067: private Result tt = new Result();
068: private Result tu = new Result();
069:
070: public PerformanceTester(String uri, int n) throws Exception {
071: NITERS = n;
072: Class.forName("org.axiondb.jdbc.AxionDriver");
073: conn = DriverManager.getConnection(uri);
074: createTables();
075: populTables();
076: rangeTables();
077: queryTables();
078: emptyTables();
079:
080: double popul1 = ((double) bb.popul / tt.popul - 1.0) * 100;
081: double range1 = ((double) bb.range / tt.range - 1.0) * 100;
082: double query1 = ((double) bb.query / tt.query - 1.0) * 100;
083: double empty1 = ((double) bb.empty / tt.empty - 1.0) * 100;
084: System.out.println("Results TT/BB:");
085: System.out.println("popul: " + popul1);
086: System.out.println("range: " + range1);
087: System.out.println("query: " + query1);
088: System.out.println("empty: " + empty1);
089:
090: double popul2 = ((double) bu.popul / tu.popul - 1.0) * 100;
091: double range2 = ((double) bu.range / tu.range - 1.0) * 100;
092: double query2 = ((double) bu.query / tu.query - 1.0) * 100;
093: double empty2 = ((double) bu.empty / tu.empty - 1.0) * 100;
094: System.out.println("Results TU/BU:");
095: System.out.println("popul: " + popul2);
096: System.out.println("range: " + range2);
097: System.out.println("query: " + query2);
098: System.out.println("empty: " + empty2);
099:
100: System.out.println("Results AVG:");
101: System.out.println("popul: " + (popul1 + popul2) / 2.0);
102: System.out.println("range: " + (range1 + range2) / 2.0);
103: System.out.println("query: " + (query1 + query2) / 2.0);
104: System.out.println("empty: " + (empty1 + empty2) / 2.0);
105: }
106:
107: public void createTables() throws Exception {
108: System.out.println("Creating tables");
109: Statement stmt = null;
110: try {
111: stmt = conn.createStatement();
112: if (pre) {
113: stmt
114: .execute("create table b00 (id int, name varchar(100), "
115: + "tag char, mod timestamp, doit boolean, sum real, "
116: + "val numeric(8));");
117: stmt
118: .execute("create table b00u (id int, name varchar(100), "
119: + "tag char, mod timestamp, doit boolean, sum real, "
120: + "val numeric(8));");
121: stmt
122: .execute("create btree index b00_id on b00 (id)");
123: stmt
124: .execute("create unique btree index b00u_id on b00u(id)");
125:
126: stmt
127: .execute("create table t00 (id int, name varchar(100), "
128: + "tag char, mod timestamp, doit boolean, sum real, "
129: + "val numeric(8));");
130: stmt
131: .execute("create table t00u (id int, name varchar(100), "
132: + "tag char, mod timestamp, doit boolean, sum real, "
133: + "val numeric(8));");
134: stmt
135: .execute("create ttree index t00_id on t00 (id)");
136: stmt
137: .execute("create unique ttree index t00u_id on t00u(id)");
138: }
139: if (cmp) {
140: stmt
141: .execute("create table bbb (id int, name varchar(100), "
142: + "tag char, mod timestamp, doit boolean, sum real, "
143: + "val numeric(8));");
144: stmt
145: .execute("create table bbbu (id int, name varchar(100), "
146: + "tag char, mod timestamp, doit boolean, sum real, "
147: + "val numeric(8));");
148: stmt
149: .execute("create btree index bbb_id on bbb (id)");
150: stmt
151: .execute("create unique btree index bbbu_id on bbbu(id)");
152: }
153: stmt
154: .execute("create table ttt (id int, name varchar(100), "
155: + "tag char, mod timestamp, doit boolean, sum real, "
156: + "val numeric(8));");
157: stmt
158: .execute("create table tttu (id int, name varchar(100), "
159: + "tag char, mod timestamp, doit boolean, sum real, "
160: + "val numeric(8));");
161: stmt
162: .execute("create ttree index ttt_id on ttt (id);");
163: stmt
164: .execute("create unique ttree index tttu_id on tttu(id);");
165: } finally {
166: if (stmt != null)
167: stmt.close();
168: }
169: }
170:
171: public long emptyTable(String table) throws Exception {
172: long time;
173: System.out.println("Emptying " + table);
174: PreparedStatement stmt = null;
175: boolean orgState = conn.getAutoCommit();
176: conn.setAutoCommit(false);
177: try {
178: stmt = conn.prepareStatement("delete from " + table
179: + " where id=?;");
180: time = System.currentTimeMillis();
181: for (int i = 0; i < NITERS; i++) {
182: stmt.setInt(1, i);
183: stmt.executeUpdate();
184: commitIfNeeded(i);
185: }
186: time = System.currentTimeMillis() - time;
187: } finally {
188: if (stmt != null) {
189: stmt.close();
190: }
191: conn.setAutoCommit(orgState);
192: }
193: System.out.println(time);
194: return time;
195: }
196:
197: public void emptyTables() throws Exception {
198: if (pre) {
199: emptyTable("b00");
200: emptyTable("b00u");
201: emptyTable("t00");
202: emptyTable("t00u");
203: }
204: if (cmp) {
205: bb.empty = emptyTable("bbb");
206: bu.empty = emptyTable("bbbu");
207: }
208: tt.empty = emptyTable("ttt");
209: tu.empty = emptyTable("tttu");
210: }
211:
212: public long populTable(String table) throws Exception {
213: long time;
214: System.out.println("Populing " + table);
215: PreparedStatement stmt = null;
216: boolean orgState = conn.getAutoCommit();
217: conn.setAutoCommit(false);
218: try {
219: stmt = conn.prepareStatement("insert into " + table
220: + "(id, name) values(?, ?);");
221: time = System.currentTimeMillis();
222: for (int i = 0; i < NITERS; i++) {
223: stmt.setInt(1, (i + 333) % NITERS);
224: stmt.setString(2, "Jack" + i);
225: stmt.executeUpdate();
226: commitIfNeeded(i);
227: }
228:
229: time = System.currentTimeMillis() - time;
230: } finally {
231: if (stmt != null) {
232: stmt.close();
233: }
234: conn.setAutoCommit(orgState);
235: }
236: System.out.println(time);
237: return time;
238: }
239:
240: private void commitIfNeeded(int i) {
241: if (i % 1000 == 0) {
242: try {
243: conn.commit();
244: } catch (SQLException e) {
245: e.printStackTrace();
246: }
247: }
248: }
249:
250: public void populTables() throws Exception {
251: if (pre) {
252: populTable("b00");
253: populTable("b00u");
254: populTable("t00");
255: populTable("t00u");
256: }
257: if (cmp) {
258: bb.popul = populTable("bbb");
259: bu.popul = populTable("bbbu");
260: }
261: tt.popul = populTable("ttt");
262: tu.popul = populTable("tttu");
263: }
264:
265: public long queryTable(String table) throws Exception {
266: long time;
267: System.out.println("Querying " + table);
268: PreparedStatement stmt = null;
269: try {
270: stmt = conn
271: .prepareStatement("select id,name,tag,mod,doit,sum,val "
272: + "from " + table + " where id=?");
273: time = System.currentTimeMillis();
274: for (int i = 0; i < NITERS; i++) {
275: stmt.setInt(1, (i + 231) % NITERS);
276: ResultSet rset = stmt.executeQuery();
277: if (rset != null)
278: rset.close();
279: }
280: time = System.currentTimeMillis() - time;
281: } finally {
282: if (stmt != null)
283: stmt.close();
284: }
285: System.out.println(time);
286: return time;
287: }
288:
289: public void queryTables() throws Exception {
290: if (pre) {
291: queryTable("b00");
292: queryTable("b00u");
293: queryTable("t00");
294: queryTable("t00u");
295: }
296: if (cmp) {
297: bb.query = queryTable("bbb");
298: bu.query = queryTable("bbbu");
299: }
300: tt.query = queryTable("ttt");
301: tu.query = queryTable("tttu");
302: }
303:
304: public long rangeTable(String table) throws Exception {
305: long time;
306: System.out.println("Rangeing " + table);
307: String query = "select id,name,tag,mod,doit,sum,val from "
308: + table + " where id>111";
309: Statement stmt = null;
310: ResultSet rset = null;
311: try {
312: stmt = conn.createStatement();
313: time = System.currentTimeMillis();
314: rset = stmt.executeQuery(query);
315: time = System.currentTimeMillis() - time;
316: } finally {
317: if (rset != null)
318: rset.close();
319: if (stmt != null)
320: stmt.close();
321: }
322: System.out.println(time);
323: return time;
324: }
325:
326: public void rangeTables() throws Exception {
327: if (pre) {
328: rangeTable("b00");
329: rangeTable("b00u");
330: rangeTable("t00");
331: rangeTable("t00u");
332: }
333: if (cmp) {
334: bb.range = rangeTable("bbb");
335: bu.range = rangeTable("bbbu");
336: }
337: tt.range = rangeTable("ttt");
338: tu.range = rangeTable("tttu");
339: }
340: }
|