1 14 package org.compiere.dbPort; 15 16 import java.sql.*; 17 import javax.sql.*; 18 19 25 public class JdbcTestPG extends Thread 26 { 27 46 47 private static final int NUM_OF_THREADS = 10; 49 50 private static final String CONNECTION = 51 "jdbc:postgresql://linux:5432/compiere"; 52 53 private static final String UID = "compiere"; 54 private static final String PWD = "compiere"; 55 private static final String 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 private static int s_cType = 0; 67 private static final String [] C_INFO = { 68 "Shared Connection ", 69 "Multiple Connections ", 70 "Multiple PreCreated ", 71 }; 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 80 private static int s_rType = 0; 82 private static final String [] R_INFO = { 83 "ResultSet ", 84 }; 87 private static final int R_RESULTSET = 0; 88 91 92 96 public static void main (String args []) 97 { 98 try 99 { 100 101 DriverManager.registerDriver(new org.postgresql.Driver()); 102 103 s_cType = C_MULTIPLE; 104 statementTiming(); 105 statementTiming(); 106 statementTiming(); 107 s_fetchSize = 10; 110 s_cType = C_SHARED; 112 s_do_yield = false; 113 runTest(); 114 runTest(); 115 s_do_yield = true; 116 runTest(); 117 runTest(); 118 s_cType = C_MULTIPLE; 120 s_do_yield = false; 121 runTest(); 122 runTest(); 123 s_do_yield = true; 124 runTest(); 125 runTest(); 126 s_cType = C_PRECREATED; 128 s_do_yield = false; 129 runTest(); 130 runTest(); 131 s_do_yield = true; 132 runTest(); 133 runTest(); 134 136 } 137 catch (Exception e) 138 { 139 e.printStackTrace(); 140 } 141 } 143 144 145 149 static void runTest() throws Exception 150 { 151 Thread [] threadList = new Thread [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 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 threadList[i] = new JdbcTestPG(i); 167 threadList[i].start(); 168 } 169 170 long start = System.currentTimeMillis(); 172 setGreenLight (); 173 174 for (int i = 0; i < NUM_OF_THREADS; i++) 176 threadList[i].join(); 177 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 } 196 197 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 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 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 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 } 258 259 260 261 265 public JdbcTestPG (int id) 266 { 267 super(); 268 m_myId = id; 269 } 270 271 private int m_myId = 0; 272 273 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 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 304 rs = stmt.executeQuery (STATEMENT); 306 307 while (rs.next()) 309 { 310 if (s_do_yield) 311 yield(); } 313 314 rs.close(); 316 rs = null; 317 318 stmt.close(); 320 stmt = null; 321 322 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 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 } | Popular Tags |