home *** CD-ROM | disk | FTP | other *** search
Java Source | 2003-12-19 | 5.2 KB | 147 lines |
- package it.favaroni.db.connectionpool;
-
- /**
- * Utility per effettuare stress test e benchmark fra connessioni in pool e connessioni singole.
- * Data di creazione: (27/06/2003 22.32.45)
- * @author: Roberto Favaroni
- */
- import java.sql.*;
- import java.util.*;
-
- public class Tester implements Runnable {
- private String type = null;
- private ConnectionParameters connParams = new ConnectionParameters();
- private static final String SINGLE = "single";
- private static final String POOL = "pool";
- private static final Vector singleConns = new Vector();
- private static final Vector poolConns = new Vector();
- private ConnectionPool connectionPool = ConnectionPool.getInstance();
-
- /**
- * Commento del constructor Tester.
- */
- public Tester(String type) {
- super();
- this.type = type;
-
- }
- /**
- * Commento del constructor Tester.
- */
- public Tester(String type, ConnectionParameters connParams) {
- this.type = type;
- this.connParams = connParams;
- }
- private static final long computeAverage(Vector vector) {
- long tot = 0;
- for (Enumeration e = vector.elements(); e.hasMoreElements();) {
- tot += ((Integer) e.nextElement()).intValue();
- }
- return (tot / vector.size());
- }
- /**
- *
- * @param args java.lang.String[]
- */
- public static void main(String[] args) {
- try {
-
-
- it.favaroni.wrapper.PropertiesFileHandler props = it.favaroni.wrapper.PropertiesFileHandler.getInstance("connectionpool.properties");
- int tryNumber = Integer.parseInt(props.readProperty("stress.try.number"));
- System.out.println("-------------------- numero tentativi: " + tryNumber);
-
-
- //POOL
- for (int i = 0; i < tryNumber; i++) {
- Tester tester = new Tester(POOL);
- (new Thread(tester)).start();
- }
- while (Tester.poolConns.size() != tryNumber) {
- Thread.sleep(5000);
- }
-
-
- //SINGLE
- ConnectionParameters connParams = new ConnectionParameters();
- connParams.setDriver(props.readProperty("jdbc.driver"));
- connParams.setUrl(props.readProperty("jdbc.url"));
- connParams.setUser(props.readProperty("jdbc.user"));
- connParams.setPassword(props.readProperty("jdbc.password"));
- Class.forName(connParams.getDriver());
-
- for (int i = 0; i < tryNumber; i++) {
- Tester tester = new Tester(SINGLE, connParams);
- (new Thread(tester)).start();
- }
- while (Tester.singleConns.size() != tryNumber) {
- Thread.sleep(5000);
- }
-
-
- System.out.println("\n\n\n");
- System.out.println("###########################################################################");
- System.out.println("###########################################################################");
- System.out.println("-- media acquisizione e rilascio connessioni dal pool: " + Tester.computeAverage(Tester.poolConns) + " millisecondi");
- System.out.println("-- media acquisizione e rilascio connessioni singole: " + Tester.computeAverage(Tester.singleConns) + " millisecondi");
- System.out.println("###########################################################################");
- System.out.println("###########################################################################");
- System.out.println("\npremi un tasto per terminare");
- System.out.println("\n\n\n");
- System.in.read();
-
-
- ConnectionPool.getInstance().finalize();
-
-
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- public void run() {
- java.util.Date before = new java.util.Date();
- Vector vector = null;
- if (type.equals(SINGLE)) {
- testSingleConnection(
- connParams.getUrl(),
- connParams.getUser(),
- connParams.getPassword());
- vector = Tester.singleConns;
- } else if (type.equals(POOL)) {
- testConnectionPool();
- vector = Tester.poolConns;
- }
- java.util.Date after = new java.util.Date();
- long diff = after.getTime() - before.getTime();
- vector.add(new Integer((int) diff));
- System.out.println(
- type + " TEST: utilizzo connection in " + diff + " millisecondi");
- }
- public void testConnectionPool() {
- try {
- Connection connection = connectionPool.getFreeConnection();
- /* Statement stm = connection.createStatement();
- ResultSet rst = stm.executeQuery("SELECT * FROM STRESS_TABLE");
- while (rst.next()) {
- System.out.println(rst.getString("field1"));
- }*/
- connectionPool.releaseConnection(connection);
- // System.out.println("POOL - ottenuta e rilasciata connessione");
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- public void testSingleConnection(String url, String user, String password) {
- try {
- //creazione connessione
- Connection newConnection = DriverManager.getConnection(url, user, password);
- newConnection.close();
- // System.out.println("SINGLE - ottenuta e rilasciata connessione");
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- }
-