home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 77 / IOPROG_77.ISO / tips / Java / ConnectionPool / src / it / favaroni / db / connectionpool / Tester.java < prev   
Encoding:
Java Source  |  2003-12-19  |  5.2 KB  |  147 lines

  1. package it.favaroni.db.connectionpool;
  2.  
  3. /**
  4.  * Utility per effettuare stress test e benchmark fra connessioni in pool e connessioni singole.
  5.  * Data di creazione: (27/06/2003 22.32.45)
  6.  * @author: Roberto Favaroni
  7.  */
  8. import java.sql.*;
  9. import java.util.*;
  10.  
  11. public class Tester implements Runnable {
  12.     private String type = null;
  13.     private ConnectionParameters connParams = new ConnectionParameters();
  14.     private static final String SINGLE = "single";
  15.     private static final String POOL = "pool";
  16.     private static final Vector singleConns = new Vector();
  17.     private static final Vector poolConns = new Vector();
  18.     private ConnectionPool connectionPool = ConnectionPool.getInstance();
  19.  
  20. /**
  21.  * Commento del constructor Tester.
  22.  */
  23. public Tester(String type) {
  24.     super();
  25.     this.type = type;
  26.  
  27. }
  28. /**
  29.  * Commento del constructor Tester.
  30.  */
  31. public Tester(String type, ConnectionParameters connParams) {
  32.     this.type = type;
  33.     this.connParams = connParams;
  34. }
  35. private static final long computeAverage(Vector vector) {
  36.     long tot = 0;
  37.     for (Enumeration e = vector.elements(); e.hasMoreElements();) {
  38.         tot += ((Integer) e.nextElement()).intValue();
  39.     }
  40.     return (tot / vector.size());
  41. }
  42. /**
  43.  * 
  44.  * @param args java.lang.String[]
  45.  */
  46. public static void main(String[] args) {
  47.     try {     
  48.         
  49.          
  50.         it.favaroni.wrapper.PropertiesFileHandler props = it.favaroni.wrapper.PropertiesFileHandler.getInstance("connectionpool.properties");        
  51.         int tryNumber = Integer.parseInt(props.readProperty("stress.try.number"));
  52.         System.out.println("--------------------    numero tentativi: " + tryNumber);        
  53.         
  54.          
  55.         //POOL
  56.         for (int i = 0; i < tryNumber; i++) {
  57.             Tester tester = new Tester(POOL);
  58.             (new Thread(tester)).start();
  59.         }
  60.         while (Tester.poolConns.size() != tryNumber) {
  61.             Thread.sleep(5000);            
  62.         }
  63.         
  64.                 
  65.         //SINGLE
  66.         ConnectionParameters connParams = new ConnectionParameters();          
  67.         connParams.setDriver(props.readProperty("jdbc.driver"));
  68.         connParams.setUrl(props.readProperty("jdbc.url"));
  69.         connParams.setUser(props.readProperty("jdbc.user"));
  70.         connParams.setPassword(props.readProperty("jdbc.password"));
  71.         Class.forName(connParams.getDriver());                
  72.         
  73.         for (int i = 0; i < tryNumber; i++) {
  74.             Tester tester = new Tester(SINGLE, connParams);
  75.             (new Thread(tester)).start();
  76.         }
  77.         while (Tester.singleConns.size() != tryNumber) {
  78.             Thread.sleep(5000);            
  79.         }
  80.  
  81.          
  82.         System.out.println("\n\n\n");
  83.         System.out.println("###########################################################################");
  84.         System.out.println("###########################################################################");
  85.         System.out.println("-- media acquisizione e rilascio connessioni dal pool: " + Tester.computeAverage(Tester.poolConns) + " millisecondi");                    
  86.         System.out.println("-- media acquisizione e rilascio connessioni singole:  " + Tester.computeAverage(Tester.singleConns) + " millisecondi"); 
  87.         System.out.println("###########################################################################");
  88.         System.out.println("###########################################################################");
  89.         System.out.println("\npremi un tasto per terminare");            
  90.         System.out.println("\n\n\n");    
  91.         System.in.read(); 
  92.         
  93.  
  94.         ConnectionPool.getInstance().finalize();
  95.         
  96.        
  97.     } catch (Throwable e) {
  98.         e.printStackTrace();
  99.     }
  100. }
  101. public void run() {
  102.     java.util.Date before = new java.util.Date();
  103.     Vector vector = null;
  104.     if (type.equals(SINGLE)) {
  105.         testSingleConnection(
  106.             connParams.getUrl(),
  107.             connParams.getUser(),
  108.             connParams.getPassword());
  109.         vector = Tester.singleConns;
  110.     } else if (type.equals(POOL)) {
  111.         testConnectionPool();
  112.         vector = Tester.poolConns;
  113.     }
  114.     java.util.Date after = new java.util.Date();
  115.     long diff = after.getTime() - before.getTime();
  116.     vector.add(new Integer((int) diff));
  117.     System.out.println(
  118.         type + " TEST:    utilizzo connection in " + diff + " millisecondi");
  119. }
  120. public void testConnectionPool() {
  121.     try {
  122.         Connection connection = connectionPool.getFreeConnection();
  123. /*        Statement stm = connection.createStatement();
  124.         ResultSet rst = stm.executeQuery("SELECT * FROM STRESS_TABLE");
  125.         while (rst.next()) {
  126.             System.out.println(rst.getString("field1"));
  127.         }*/
  128.         connectionPool.releaseConnection(connection);
  129. //        System.out.println("POOL - ottenuta e rilasciata connessione");
  130.     } catch (Exception e) {
  131.         e.printStackTrace();
  132.     }
  133.  
  134. }
  135. public void testSingleConnection(String url, String user, String password) {
  136.     try {
  137.         //creazione connessione
  138.         Connection newConnection = DriverManager.getConnection(url, user, password);
  139.         newConnection.close();
  140. //        System.out.println("SINGLE - ottenuta e rilasciata connessione");
  141.     } catch (Exception e) {
  142.         e.printStackTrace();
  143.     }
  144.  
  145. }
  146. }
  147.