KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > dbPort > JdbcTestPG


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-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.dbPort;
15
16 import java.sql.*;
17 import javax.sql.*;
18
19 /**
20  * JDBC Performance Test.
21  *
22  * @author Jorg Janke
23  * @version $Id: JdbcTestPG.java,v 1.1 2002/05/09 02:49:54 jjanke Exp $
24  */

25 public class JdbcTestPG extends Thread JavaDoc
26 {
27 /*****************************************************************************
28
29 Multiple Connections Fetch=10 Conn=407 Stmt=15 Query=1516 Retrieve=203 ClRs=0 ClStmt=0 ClConn=0 - Total=2141 Stmt=1734 Query=1719
30 Multiple Connections Fetch=10 Conn=47 Stmt=0 Query=1234 Retrieve=31 ClRs=0 ClStmt=0 ClConn=0 - Total=1312 Stmt=1265 Query=1265
31 Multiple Connections Fetch=10 Conn=31 Stmt=0 Query=1266 Retrieve=15 ClRs=0 ClStmt=0 ClConn=0 - Total=1312 Stmt=1281 Query=1281
32 Shared Connection Threads=10 Yield=false ms= 13047 each= 1304
33 Shared Connection Threads=10 Yield=false ms= 12891 each= 1289
34 Shared Connection Threads=10 Yield=true ms= 13422 each= 1342
35 Shared Connection Threads=10 Yield=true ms= 12969 each= 1296
36 Multiple Connections Threads=10 Yield=false ms= 13046 each= 1304
37 Multiple Connections Threads=10 Yield=false ms= 12891 each= 1289
38 Multiple Connections Threads=10 Yield=true ms= 13062 each= 1306
39 Multiple Connections Threads=10 Yield=true ms= 13062 each= 1306
40 Multiple PreCreated Threads=10 Yield=false ms= 9968 each= 996
41 Multiple PreCreated Threads=10 Yield=false ms= 10250 each= 1025
42 Multiple PreCreated Threads=10 Yield=true ms= 10109 each= 1010
43 Multiple PreCreated Threads=10 Yield=true ms= 9906 each= 990
44
45 ******************************************************************************/

46
47     // Default no of threads to 10
48
private static final int NUM_OF_THREADS = 10;
49
50     private static final String JavaDoc CONNECTION =
51         "jdbc:postgresql://linux:5432/compiere";
52
53     private static final String JavaDoc UID = "compiere";
54     private static final String JavaDoc PWD = "compiere";
55     private static final String JavaDoc STATEMENT = "SELECT * FROM AD_Column";
56     private static final boolean WITH_OUTPUT = false;
57
58     private static boolean s_do_yield = true;
59
60     private static Connection s_sconn = null;
61     private static Connection[] s_conn = null;
62
63     private static int s_fetchSize = 10;
64
65     // Connection
66
private static int s_cType = 0;
67     private static final String JavaDoc[] C_INFO = {
68         "Shared Connection ",
69         "Multiple Connections ",
70         "Multiple PreCreated ",
71     // "Data Source ",
72
// "Connection Cache "
73
};
74     private static final int C_SHARED = 0;
75     private static final int C_MULTIPLE = 1;
76     private static final int C_PRECREATED = 2;
77 // private static final int C_DATASOURCE = 3;
78
// private static final int C_CACHE = 4;
79

80     // Data
81
private static int s_rType = 0;
82     private static final String JavaDoc[] R_INFO = {
83         "ResultSet ",
84 // "Cached RowSet ",
85
// "JDBC RowSet "
86
};
87     private static final int R_RESULTSET = 0;
88 // private static final int R_CACHED_ROWSET = 1;
89
// private static final int R_JDBC_ROWSET = 2;
90

91
92     /**
93      * Main Test
94      * @param args
95      */

96     public static void main (String JavaDoc args [])
97     {
98         try
99         {
100             /* Load the JDBC driver */
101             DriverManager.registerDriver(new org.postgresql.Driver());
102
103             s_cType = C_MULTIPLE;
104             statementTiming();
105             statementTiming();
106             statementTiming();
107             //
108
s_fetchSize = 10; // standard value
109

110             //
111
s_cType = C_SHARED;
112             s_do_yield = false;
113             runTest();
114             runTest();
115             s_do_yield = true;
116             runTest();
117             runTest();
118             //
119
s_cType = C_MULTIPLE;
120             s_do_yield = false;
121             runTest();
122             runTest();
123             s_do_yield = true;
124             runTest();
125             runTest();
126             //
127
s_cType = C_PRECREATED;
128             s_do_yield = false;
129             runTest();
130             runTest();
131             s_do_yield = true;
132             runTest();
133             runTest();
134             //
135

136         }
137         catch (Exception JavaDoc e)
138         {
139             e.printStackTrace();
140         }
141     } // main
142

143     /*************************************************************************/
144
145     /**
146      * Run the test
147      * @throws Exception
148      */

149     static void runTest() throws Exception JavaDoc
150     {
151         // Create the threads
152
Thread JavaDoc[] threadList = new Thread JavaDoc[NUM_OF_THREADS];
153         s_conn = new Connection[NUM_OF_THREADS];
154
155         if (s_cType == C_SHARED)
156             s_sconn = DriverManager.getConnection (CONNECTION, UID, PWD);
157         //
158
// spawn threads
159
for (int i = 0; i < NUM_OF_THREADS; i++)
160         {
161             if (WITH_OUTPUT)
162                 System.out.println("Starting #" + i);
163             if (s_cType == C_PRECREATED)
164                 s_conn[i] = DriverManager.getConnection (CONNECTION, UID, PWD);
165             //
166
threadList[i] = new JdbcTestPG(i);
167             threadList[i].start();
168         }
169
170         // Start everyone at the same time
171
long start = System.currentTimeMillis();
172         setGreenLight ();
173
174         // wait for all threads to end
175
for (int i = 0; i < NUM_OF_THREADS; i++)
176             threadList[i].join();
177         //
178
if (s_sconn != null)
179             s_sconn.close();
180         s_sconn = null;
181         for (int i = 0; i < NUM_OF_THREADS; i++)
182         {
183             if (s_conn[i] != null)
184                 s_conn[i].close();
185             s_conn[i] = null;
186         }
187         long result = System.currentTimeMillis() - start;
188         System.out.print (C_INFO[s_cType]
189             + "Threads=" + NUM_OF_THREADS
190             + " \tYield=" + s_do_yield
191             + " \tms= " + result
192             + " \teach= " + (result/NUM_OF_THREADS));
193         System.out.println();
194     } // runTest
195

196
197     /**
198      * Statement Timing
199      */

200     private static void statementTiming()
201     {
202         try
203         {
204             long startConnection = System.currentTimeMillis();
205             Connection conn = null;
206             if (s_cType == C_MULTIPLE)
207                 conn = DriverManager.getConnection (CONNECTION, UID, PWD);
208
209             long startStatement = System.currentTimeMillis();
210             Statement stmt = conn.createStatement ();
211         // stmt.setFetchSize(s_fetchSize);
212

213             long startQuery = System.currentTimeMillis();
214             ResultSet rs = stmt.executeQuery (STATEMENT);
215
216             int i = 0;
217             long startRetrieve = System.currentTimeMillis();
218             while (rs.next())
219             {
220                 rs.getString(1);
221                 i++;
222             }
223             long endRetrieve = System.currentTimeMillis();
224         // System.out.println(i);
225

226             rs.close();
227             rs = null;
228             long endQuery = System.currentTimeMillis();
229
230             stmt.close();
231             stmt = null;
232             long endStatement = System.currentTimeMillis();
233
234             conn.close();
235             conn = null;
236             long endConnection = System.currentTimeMillis();
237
238             //
239
System.out.println(C_INFO[s_cType]
240                 + "Fetch=" + s_fetchSize
241                 + " \tConn=" + (startStatement - startConnection)
242                 + " \tStmt=" + (startQuery - startStatement)
243                 + " \tQuery=" + (startRetrieve - startQuery)
244                 + " \tRetrieve=" + (endRetrieve - startRetrieve)
245                 + " \tClRs=" + (endQuery - endRetrieve)
246                 + " \tClStmt=" + (endStatement - endQuery)
247                 + " \tClConn=" + (endConnection - endStatement)
248                 + " \t- Total=" + (endConnection - startConnection)
249                 + " \tStmt=" + (endStatement - startStatement)
250                 + " \tQuery=" + (endQuery - startQuery));
251         }
252         catch (SQLException e)
253         {
254             e.printStackTrace();
255         }
256     } // statementTiming
257

258
259     /*************************************************************************/
260
261     /**
262      * JdbcTest Thread
263      * @param id Thread ID
264      */

265     public JdbcTestPG (int id)
266     {
267         super();
268         m_myId = id;
269     }
270
271     private int m_myId = 0;
272
273     /**
274      * Async Worker
275      */

276     public void run()
277     {
278         ResultSet rs = null;
279         Statement stmt = null;
280
281         try
282         {
283             if (WITH_OUTPUT)
284                 System.out.println("Thread " + m_myId + " waiting");
285             while (!getGreenLight())
286                 yield();
287             if (WITH_OUTPUT)
288                 System.out.println("Thread " + m_myId + " started");
289
290             // Get the connection & statement
291
if (s_cType == C_SHARED)
292                 stmt = s_sconn.createStatement ();
293             else if (s_cType == C_MULTIPLE)
294             {
295                 s_conn[m_myId] = DriverManager.getConnection (CONNECTION, UID, PWD);
296                 stmt = s_conn[m_myId].createStatement ();
297             }
298             else if (s_cType == C_PRECREATED)
299             {
300                 stmt = s_conn[m_myId].createStatement ();
301             }
302         // stmt.setFetchSize(s_fetchSize);
303

304             // Execute the Query
305
rs = stmt.executeQuery (STATEMENT);
306
307             // Loop through the results
308
while (rs.next())
309             {
310                 if (s_do_yield)
311                     yield(); // Yield To other threads
312
}
313
314             // Close all the resources
315
rs.close();
316             rs = null;
317
318             // Close the statement
319
stmt.close();
320             stmt = null;
321
322             // Close the local connection
323
if (s_cType == C_SHARED || s_cType == C_PRECREATED)
324                 ;
325             else
326             {
327                 s_conn[m_myId].close();
328                 s_conn[m_myId] = null;
329             }
330         }
331         catch (Exception JavaDoc e)
332         {
333             System.out.println("Thread " + m_myId + " got Exception: " + e);
334             e.printStackTrace();
335             return;
336         }
337         if (WITH_OUTPUT)
338             System.out.println("Thread " + m_myId + " finished");
339     }
340
341     /*************************************************************************/
342
343     static boolean greenLight = false;
344     static synchronized void setGreenLight () { greenLight = true; }
345     synchronized boolean getGreenLight () { return greenLight; }
346
347 } // JdbcTestPG
348
Popular Tags