001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.c3p0.stmt;
024:
025: import java.util.*;
026: import java.sql.*;
027: import javax.sql.*;
028: import com.mchange.v2.c3p0.*;
029: import com.mchange.v1.db.sql.*;
030:
031: public final class StatementCacheBenchmark {
032: final static String EMPTY_TABLE_CREATE = "CREATE TABLE emptyyukyuk (a varchar(8), b varchar(8))";
033: final static String EMPTY_TABLE_SELECT = "SELECT * FROM emptyyukyuk";
034: final static String EMPTY_TABLE_DROP = "DROP TABLE emptyyukyuk";
035:
036: final static String EMPTY_TABLE_CONDITIONAL_SELECT = "SELECT * FROM emptyyukyuk where a = ?";
037:
038: final static int NUM_ITERATIONS = 2000;
039:
040: public static void main(String[] argv) {
041: DataSource ds_unpooled = null;
042: DataSource ds_pooled = null;
043: try {
044:
045: String jdbc_url = null;
046: String username = null;
047: String password = null;
048: if (argv.length == 3) {
049: jdbc_url = argv[0];
050: username = argv[1];
051: password = argv[2];
052: } else if (argv.length == 1) {
053: jdbc_url = argv[0];
054: username = null;
055: password = null;
056: } else
057: usage();
058:
059: if (!jdbc_url.startsWith("jdbc:"))
060: usage();
061:
062: ds_unpooled = DriverManagerDataSourceFactory.create(
063: jdbc_url, username, password);
064: ds_pooled = PoolBackedDataSourceFactory.create(jdbc_url,
065: username, password, 5, 20, 5, 0, 100);
066:
067: create(ds_pooled);
068:
069: perform(ds_pooled, "pooled");
070: perform(ds_unpooled, "unpooled");
071: } catch (Exception e) {
072: e.printStackTrace();
073: } finally {
074: try {
075: drop(ds_pooled);
076: } catch (Exception e) {
077: e.printStackTrace();
078: }
079: }
080: }
081:
082: private static void perform(DataSource ds, String name)
083: throws SQLException {
084: Connection c = null;
085: PreparedStatement ps = null;
086: try {
087: c = ds.getConnection();
088: long start = System.currentTimeMillis();
089: for (int i = 0; i < NUM_ITERATIONS; ++i) {
090: PreparedStatement test = c
091: .prepareStatement(EMPTY_TABLE_CONDITIONAL_SELECT);
092: test.close();
093: }
094: long end = System.currentTimeMillis();
095: System.err.println(name + " --> " + (end - start)
096: / (float) NUM_ITERATIONS + " [" + NUM_ITERATIONS
097: + " iterations]");
098: } finally {
099: StatementUtils.attemptClose(ps);
100: ConnectionUtils.attemptClose(c);
101: }
102: }
103:
104: private static void usage() {
105: System.err.println("java "
106: + "-Djdbc.drivers=<comma_sep_list_of_drivers> "
107: + StatementCacheBenchmark.class.getName()
108: + " <jdbc_url> [<username> <password>]");
109: System.exit(-1);
110: }
111:
112: static void create(DataSource ds) throws SQLException {
113: System.err.println("Creating test schema.");
114: Connection con = null;
115: PreparedStatement ps1 = null;
116: try {
117: con = ds.getConnection();
118: ps1 = con.prepareStatement(EMPTY_TABLE_CREATE);
119: ps1.executeUpdate();
120: System.err.println("Test schema created.");
121: } finally {
122: StatementUtils.attemptClose(ps1);
123: ConnectionUtils.attemptClose(con);
124: }
125: }
126:
127: static void drop(DataSource ds) throws SQLException {
128: Connection con = null;
129: PreparedStatement ps1 = null;
130: try {
131: con = ds.getConnection();
132: ps1 = con.prepareStatement(EMPTY_TABLE_DROP);
133: ps1.executeUpdate();
134: } finally {
135: StatementUtils.attemptClose(ps1);
136: ConnectionUtils.attemptClose(con);
137: }
138: System.err.println("Test schema dropped.");
139: }
140: }
|