KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > db > TestConnection


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.db;
15
16 import java.sql.*;
17
18 /**
19  * Test Connection (speed)
20  *
21  * @author Jorg Janke
22  * @version $Id: TestConnection.java,v 1.3 2003/01/20 05:41:37 jjanke Exp $
23  */

24 public class TestConnection
25 {
26
27     /**
28      * Test Connection
29      *
30      * @param jdbcURL JDBC URL
31      * @param uid user
32      * @param pwd password
33      */

34     public TestConnection (String JavaDoc jdbcURL, String JavaDoc uid, String JavaDoc pwd)
35     {
36         System.out.println("Test Connection for " + jdbcURL);
37         m_jdbcURL = jdbcURL;
38         m_uid = uid;
39         m_pwd = pwd;
40         init();
41         if (m_conn != null)
42         {
43             long time = test();
44             time += test();
45             time += test();
46             time += test();
47             System.out.println("");
48             System.out.println("Total Average (" + m_jdbcURL + ")= " + (time/4) + "ms");
49         }
50     } // TestConnection
51

52     private String JavaDoc m_jdbcURL;
53     private String JavaDoc m_uid = "compiere";
54     private String JavaDoc m_pwd = "compiere";
55     private String JavaDoc m_sql = "SELECT * FROM AD_Element";
56     private Connection m_conn;
57
58     /**
59      * Initialize & Open Connection
60      */

61     private void init()
62     {
63         long start = System.currentTimeMillis();
64         Driver driver = null;
65         try
66         {
67             driver = DriverManager.getDriver(m_jdbcURL);
68         }
69         catch (SQLException ex)
70         {
71         // System.err.println("Init - get Driver: " + ex);
72
}
73         if (driver == null)
74         {
75             try
76             {
77                 DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
78             }
79             catch (SQLException ex)
80             {
81                 System.err.println("Init = register Driver: " + ex);
82             }
83         }
84         long end = System.currentTimeMillis();
85         System.out.println("(1) Driver = " + (end - start) + "ms");
86         //
87
start = System.currentTimeMillis();
88         try
89         {
90             m_conn = DriverManager.getConnection(m_jdbcURL, m_uid, m_pwd);
91         }
92         catch (SQLException ex)
93         {
94             System.err.println("Init = get Connection: " + ex);
95         }
96         end = System.currentTimeMillis();
97         System.out.println("(2) Get Connection = " + (end - start) + "ms");
98         //
99
start = System.currentTimeMillis();
100         try
101         {
102             if (m_conn != null)
103                 m_conn.close();
104         }
105         catch (SQLException ex)
106         {
107             System.err.println("Init = close Connection: " + ex);
108         }
109         end = System.currentTimeMillis();
110         System.out.println("(3) Close Connection = " + (end - start) + "ms");
111     } // init
112

113     /**
114      * Test ResultSet
115      * @return time in ms
116      */

117     private long test()
118     {
119         System.out.println("");
120         long totalStart = System.currentTimeMillis();
121         long start = System.currentTimeMillis();
122         try
123         {
124             m_conn = DriverManager.getConnection(m_jdbcURL, m_uid, m_pwd);
125         }
126         catch (SQLException ex)
127         {
128             System.err.println("Test get Connection: " + ex);
129             return -1;
130         }
131         long end = System.currentTimeMillis();
132         System.out.println("(A) Get Connection = " + (end - start) + "ms");
133         //
134
try
135         {
136             start = System.currentTimeMillis();
137             Statement stmt = m_conn.createStatement();
138             end = System.currentTimeMillis();
139             System.out.println("(B) Create Statement = " + (end - start) + "ms");
140             //
141
start = System.currentTimeMillis();
142             ResultSet rs = stmt.executeQuery(m_sql);
143             end = System.currentTimeMillis();
144             System.out.println("(C) Execute Query = " + (end - start) + "ms");
145             //
146
int no = 0;
147             start = System.currentTimeMillis();
148             while (rs.next())
149             {
150                 int i = rs.getInt("AD_Client_ID");
151                 String JavaDoc s = rs.getString("Name");
152                 i += s.length();
153                 no++;
154             }
155             end = System.currentTimeMillis();
156             System.out.println("(D) Read ResultSet = " + (end - start) + "ms - per 10 rows " + ((end - start)/(no/10)) + "ms");
157             //
158
start = System.currentTimeMillis();
159             rs.close();
160             end = System.currentTimeMillis();
161             System.out.println("(E) Close ResultSet = " + (end - start) + "ms");
162             //
163
start = System.currentTimeMillis();
164             stmt.close();
165             end = System.currentTimeMillis();
166             System.out.println("(F) Close Statement = " + (end - start) + "ms");
167         }
168         catch (SQLException e)
169         {
170             System.err.println("Test: " + e);
171         }
172         //
173
start = System.currentTimeMillis();
174         try
175         {
176             if (m_conn != null)
177                 m_conn.close();
178         }
179         catch (SQLException ex)
180         {
181             System.err.println("Test close Connection: " + ex);
182         }
183         end = System.currentTimeMillis();
184         System.out.println("(G) Close Connection = " + (end - start) + "ms");
185
186         long totalEnd = System.currentTimeMillis();
187         System.out.println("Total Test = " + (totalEnd - totalStart) + "ms");
188         return (totalEnd - totalStart);
189     } // test
190

191     /*************************************************************************/
192
193     /**
194      * Test Connection.
195      * java -cp dbPort.jar;oracle.jar org.compiere.db.TestConnection
196      * @param args arguments optional <jdbcURL> <uid> <pwd>
197      * Example: jdbc:oracle:thin:@dev:1521:dev compiere compiere
198      */

199     public static void main(String JavaDoc[] args)
200     {
201         String JavaDoc url = "jdbc:oracle:thin:@24.151.26.64:1521:lap11";
202         String JavaDoc uid = "compiere";
203         String JavaDoc pwd = "compiere";
204         //
205
if (args.length == 0)
206         {
207             System.out.println("TestConnection <jdbcUrl> <uid> <pwd>");
208             System.out.println("Example: jdbc:oracle:thin:@dev:1521:dev compiere compiere");
209             System.out.println("Example: jdbc:oracle:oci8:@dev compiere compiere");
210         }
211         else if (args.length > 0)
212             url = args[0];
213         else if (args.length > 1)
214             url = args[1];
215         else if (args.length > 2)
216             url = args[2];
217
218         System.out.println("");
219         TestConnection test = new TestConnection(url, uid, pwd);
220     } // main
221

222 } // TestConnection
Popular Tags