1 24 25 package org.objectweb.cjdbc.scenario.tools.util; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.SQLException ; 30 import java.sql.Statement ; 31 import java.util.ArrayList ; 32 import java.util.Random ; 33 34 40 public class RequestSender implements Runnable 41 { 42 boolean quit; 43 Random rand; 44 ArrayList exceptions; 45 Connection con; 46 long runtime; 47 QueryGenerator queryGenerator; 48 49 int loopInThread; 50 int doWriteEvery; 51 int queryLoop; 52 int maxIdValue; 53 int maxResponseTime; 54 int requestInterval; 55 boolean useTransactions; 56 int commitIntervalMax; 57 boolean usePreparedStatement; 58 boolean monitorSpeed; 59 60 int requestCount = 0; 62 long average = 0; 63 64 boolean useQueryGenerator; 66 67 static final int LOOP_IN_THREAD = 10; 68 static final int DO_WRITE_EVERY = 5; 69 static final int MAIN_THREAD_QUERY_LOOP = 5; 70 static final int MAX_ID_VALUE = 49; 71 static final int MAX_RESPONSE_TIME = 2000; 72 static final int TIME_BETWEEN_REQUEST = 100; 73 74 static final boolean USE_TRANSACTION = true; 75 static final int DO_COMMIT_RAND_NUMBER = 10; 76 static final boolean USE_PREPARED_STATEMENT = true; 77 static final boolean MONITOR_SPEED = true; 78 static final boolean USE_QUERY_GENERATOR = false; 79 80 85 public RequestSender(Connection con) 86 { 87 this.con = con; 88 loopInThread = LOOP_IN_THREAD; 89 doWriteEvery = DO_WRITE_EVERY; 90 queryLoop = MAIN_THREAD_QUERY_LOOP; 91 maxIdValue = MAX_ID_VALUE; 92 maxResponseTime = MAX_RESPONSE_TIME; 93 requestInterval = TIME_BETWEEN_REQUEST; 94 useTransactions = USE_TRANSACTION; 95 commitIntervalMax = DO_COMMIT_RAND_NUMBER; 96 usePreparedStatement = USE_PREPARED_STATEMENT; 97 monitorSpeed = MONITOR_SPEED; 98 useQueryGenerator = USE_QUERY_GENERATOR; 99 100 quit = false; 101 rand = new Random (System.currentTimeMillis()); 102 exceptions = new ArrayList (); 103 } 104 105 108 public void setMonitorSpeed(boolean monitorSpeed) 109 { 110 this.monitorSpeed = monitorSpeed; 111 } 112 113 private String getSelectStatement(boolean preparedStatement, int id) 114 { 115 if (preparedStatement) 116 return "Select * from DOCUMENT where id=?"; 117 else 118 return "Select * from DOCUMENT where id=" + id; 119 } 120 121 private String getUpdateStatement(boolean preparedStatement, int addressid, 122 int id) 123 { 124 if (preparedStatement) 125 return "update DOCUMENT set ADDRESSID=? where id=?"; 126 else 127 return "update DOCUMENT set ADDRESSID=" + addressid + " where id=" + id; 128 } 129 130 public QueryGenerator getQueryGenerator() throws SQLException 131 { 132 return queryGenerator; 135 } 136 137 140 public void run() 141 { 142 try 143 { 144 Statement pread = null, pwrite = null; 145 146 if (usePreparedStatement) 147 { 148 pread = con.prepareStatement(getSelectStatement(true, 0)); 149 pwrite = con.prepareStatement(getUpdateStatement(true, 0, 0)); 150 } 151 else 152 { 153 pread = con.createStatement(); 154 pwrite = con.createStatement(); 155 } 156 int commitInterval = 1 + rand.nextInt(commitIntervalMax); 157 long start = System.currentTimeMillis(); 158 while (!quit) 159 { 160 for (int i = 0; i < loopInThread; i++) 161 { 162 requestCount++; 163 if (requestInterval > 0) 164 { 165 synchronized (this) 166 { 167 wait(requestInterval); 168 } 169 } 170 if (useTransactions) 171 con.setAutoCommit(false); 172 try 173 { 174 long t1 = System.currentTimeMillis(); 175 176 if (useQueryGenerator) 177 { 178 pread.execute(getQueryGenerator().generateQuery()); 179 } 180 else 181 { 182 183 if (doWriteEvery != -1 && i % doWriteEvery == 0) 184 { 185 int id = rand.nextInt(maxIdValue); 186 int addressid = rand.nextInt(); 187 if (usePreparedStatement) 189 { 190 ((PreparedStatement ) pwrite).setInt(1, id); 191 ((PreparedStatement ) pwrite).setInt(2, addressid); 192 ((PreparedStatement ) pwrite).executeUpdate(); 193 } 194 else 195 { 196 pwrite 197 .executeUpdate(getUpdateStatement(false, addressid, id)); 198 } 199 } 202 else 203 { 204 int id = rand.nextInt(maxIdValue); 205 if (usePreparedStatement) 206 { 207 ((PreparedStatement ) pread).setInt(1, id); 209 ((PreparedStatement ) pread).executeQuery(); 210 } 211 else 212 { 213 pread.executeQuery(getSelectStatement(false, id)); 214 } 215 } 216 } 217 218 if (useTransactions && (i % commitInterval == 0)) 219 con.commit(); 220 221 long t2 = System.currentTimeMillis(); 222 long diff = t2 - t1; 223 if (monitorSpeed) 224 { 225 average = (t2 - start) / requestCount; 226 } 230 if (diff > maxResponseTime) 231 throw new Exception ("Response time to slow for client thread(" 232 + diff + ")"); 233 } 234 catch (Exception e) 235 { exceptions.add(e); 237 e.printStackTrace(); 238 } 239 } 240 } 241 if (useTransactions) 243 con.setAutoCommit(false); 244 245 long end = System.currentTimeMillis(); 247 runtime = end - start; 248 } 249 catch (Exception e) 250 { exceptions.add(e); 252 e.printStackTrace(); 253 } 254 finally 255 { 256 try 257 { 258 this.con.close(); 259 } 260 catch (SQLException e1) 261 { 262 e1.printStackTrace(); 263 } 264 } 265 } 266 267 272 public boolean isQuit() 273 { 274 return quit; 275 } 276 277 282 public void setQuit(boolean quit) 283 { 284 this.quit = quit; 285 } 286 287 292 public ArrayList getExceptions() 293 { 294 return exceptions; 295 } 296 297 302 public void setCommitIntervalMax(int commitIntervalMax) 303 { 304 this.commitIntervalMax = commitIntervalMax; 305 } 306 307 312 public void setDoWriteEvery(int doWriteEvery) 313 { 314 this.doWriteEvery = doWriteEvery; 315 } 316 317 322 public void setLoopInThread(int loopInThread) 323 { 324 this.loopInThread = loopInThread; 325 } 326 327 332 public void setMaxIdValue(int maxIdValue) 333 { 334 this.maxIdValue = maxIdValue; 335 } 336 337 342 public void setMaxResponseTime(int maxResponseTime) 343 { 344 this.maxResponseTime = maxResponseTime; 345 } 346 347 352 public void setQueryLoop(int queryLoop) 353 { 354 this.queryLoop = queryLoop; 355 } 356 357 362 public void setRand(Random rand) 363 { 364 this.rand = rand; 365 } 366 367 372 public void setRequestInterval(int requestInterval) 373 { 374 this.requestInterval = requestInterval; 375 } 376 377 382 public void setUseTransactions(boolean useTransactions) 383 { 384 this.useTransactions = useTransactions; 385 } 386 387 392 public long getRuntime() 393 { 394 return runtime; 395 } 396 397 402 public void setUsePreparedStatement(boolean usePreparedStatement) 403 { 404 this.usePreparedStatement = usePreparedStatement; 405 } 406 407 412 public long getAverage() 413 { 414 return average; 415 } 416 417 422 public int getRequestCount() 423 { 424 return requestCount; 425 } 426 427 432 public void setUseQueryGenerator(boolean useQueryGenerator) 433 { 434 this.useQueryGenerator = useQueryGenerator; 435 } 436 437 442 public void setQueryGenerator(QueryGenerator queryGenerator) 443 { 444 this.queryGenerator = queryGenerator; 445 } 446 } | Popular Tags |