KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > util > RequestSender


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.util;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Statement JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Random JavaDoc;
33
34 /**
35  * This class defines a RequestSender. This will send request in the background.
36  *
37  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
38  * @version 1.0
39  */

40 public class RequestSender implements Runnable JavaDoc
41 {
42   boolean quit;
43   Random JavaDoc rand;
44   ArrayList JavaDoc exceptions;
45   Connection JavaDoc 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   // monitoring
61
int requestCount = 0;
62   long average = 0;
63
64   // request generator
65
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   /**
81    * Creates a new <code>RequestSender</code> object with standard settings
82    *
83    * @param con the connection to the database
84    */

85   public RequestSender(Connection JavaDoc 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 JavaDoc(System.currentTimeMillis());
102     exceptions = new ArrayList JavaDoc();
103   }
104
105   /**
106    * @param monitorSpeed The monitorSpeed to set.
107    */

108   public void setMonitorSpeed(boolean monitorSpeed)
109   {
110     this.monitorSpeed = monitorSpeed;
111   }
112
113   private String JavaDoc 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 JavaDoc 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 JavaDoc
131   {
132     //if (queryGenerator == null)
133
// queryGenerator = new QueryGenerator(con);
134
return queryGenerator;
135   }
136
137   /**
138    * @see java.lang.Runnable#run()
139    */

140   public void run()
141   {
142     try
143     {
144       Statement JavaDoc 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                 // write
188
if (usePreparedStatement)
189                 {
190                   ((PreparedStatement JavaDoc) pwrite).setInt(1, id);
191                   ((PreparedStatement JavaDoc) pwrite).setInt(2, addressid);
192                   ((PreparedStatement JavaDoc) pwrite).executeUpdate();
193                 }
194                 else
195                 {
196                   pwrite
197                       .executeUpdate(getUpdateStatement(false, addressid, id));
198                 }
199                 //int updated = pwrite.executeUpdate();
200
//System.out.println("Updated=" + updated);
201
}
202               else
203               {
204                 int id = rand.nextInt(maxIdValue);
205                 if (usePreparedStatement)
206                 {
207                   // read
208
((PreparedStatement JavaDoc) pread).setInt(1, id);
209                   ((PreparedStatement JavaDoc) 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               //System.out.println("REQUEST TIME:"+diff+"ms");
227
//System.out.println("Request("+requestCount+"):" + average + "
228
// ms");
229
}
230             if (diff > maxResponseTime)
231               throw new Exception JavaDoc("Response time to slow for client thread("
232                   + diff + ")");
233           }
234           catch (Exception JavaDoc e)
235           { // this catch exceptions in the loop
236
exceptions.add(e);
237             e.printStackTrace();
238           }
239         }
240       }
241       // Commit last transaction
242
if (useTransactions)
243         con.setAutoCommit(false);
244
245       // Compute runtime
246
long end = System.currentTimeMillis();
247       runtime = end - start;
248     }
249     catch (Exception JavaDoc e)
250     {// this catch exception while creating connection and statements
251
exceptions.add(e);
252       e.printStackTrace();
253     }
254     finally
255     {
256       try
257       {
258         this.con.close();
259       }
260       catch (SQLException JavaDoc e1)
261       {
262         e1.printStackTrace();
263       }
264     }
265   }
266
267   /**
268    * Returns the quit value.
269    *
270    * @return Returns the quit.
271    */

272   public boolean isQuit()
273   {
274     return quit;
275   }
276
277   /**
278    * Sets the quit value.
279    *
280    * @param quit The quit to set.
281    */

282   public void setQuit(boolean quit)
283   {
284     this.quit = quit;
285   }
286
287   /**
288    * Returns the exceptions value.
289    *
290    * @return Returns the exceptions.
291    */

292   public ArrayList JavaDoc getExceptions()
293   {
294     return exceptions;
295   }
296
297   /**
298    * Sets the commitIntervalMax value.
299    *
300    * @param commitIntervalMax The commitIntervalMax to set.
301    */

302   public void setCommitIntervalMax(int commitIntervalMax)
303   {
304     this.commitIntervalMax = commitIntervalMax;
305   }
306
307   /**
308    * Sets the doWriteEvery value.
309    *
310    * @param doWriteEvery The doWriteEvery to set.
311    */

312   public void setDoWriteEvery(int doWriteEvery)
313   {
314     this.doWriteEvery = doWriteEvery;
315   }
316
317   /**
318    * Sets the loopInThread value.
319    *
320    * @param loopInThread The loopInThread to set.
321    */

322   public void setLoopInThread(int loopInThread)
323   {
324     this.loopInThread = loopInThread;
325   }
326
327   /**
328    * Sets the maxIdValue value.
329    *
330    * @param maxIdValue The maxIdValue to set.
331    */

332   public void setMaxIdValue(int maxIdValue)
333   {
334     this.maxIdValue = maxIdValue;
335   }
336
337   /**
338    * Sets the maxResponseTime value.
339    *
340    * @param maxResponseTime The maxResponseTime to set.
341    */

342   public void setMaxResponseTime(int maxResponseTime)
343   {
344     this.maxResponseTime = maxResponseTime;
345   }
346
347   /**
348    * Sets the queryLoop value.
349    *
350    * @param queryLoop The queryLoop to set.
351    */

352   public void setQueryLoop(int queryLoop)
353   {
354     this.queryLoop = queryLoop;
355   }
356
357   /**
358    * Sets the rand value.
359    *
360    * @param rand The rand to set.
361    */

362   public void setRand(Random JavaDoc rand)
363   {
364     this.rand = rand;
365   }
366
367   /**
368    * Sets the requestInterval value.
369    *
370    * @param requestInterval The requestInterval to set.
371    */

372   public void setRequestInterval(int requestInterval)
373   {
374     this.requestInterval = requestInterval;
375   }
376
377   /**
378    * Sets the useTransactions value.
379    *
380    * @param useTransactions The useTransactions to set.
381    */

382   public void setUseTransactions(boolean useTransactions)
383   {
384     this.useTransactions = useTransactions;
385   }
386
387   /**
388    * Returns the runtime value.
389    *
390    * @return Returns the runtime.
391    */

392   public long getRuntime()
393   {
394     return runtime;
395   }
396
397   /**
398    * Sets the usePreparedStatement value.
399    *
400    * @param usePreparedStatement The usePreparedStatement to set.
401    */

402   public void setUsePreparedStatement(boolean usePreparedStatement)
403   {
404     this.usePreparedStatement = usePreparedStatement;
405   }
406
407   /**
408    * Returns the average value.
409    *
410    * @return Returns the average.
411    */

412   public long getAverage()
413   {
414     return average;
415   }
416
417   /**
418    * Returns the requestCount value.
419    *
420    * @return Returns the requestCount.
421    */

422   public int getRequestCount()
423   {
424     return requestCount;
425   }
426
427   /**
428    * Sets the useQueryGenerator value.
429    *
430    * @param useQueryGenerator The useQueryGenerator to set.
431    */

432   public void setUseQueryGenerator(boolean useQueryGenerator)
433   {
434     this.useQueryGenerator = useQueryGenerator;
435   }
436
437   /**
438    * Sets the queryGenerator value.
439    *
440    * @param queryGenerator The queryGenerator to set.
441    */

442   public void setQueryGenerator(QueryGenerator queryGenerator)
443   {
444     this.queryGenerator = queryGenerator;
445   }
446 }
Popular Tags