KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > cache > ExtensiveCacheScenario


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.raidb1.cache;
26
27 import java.sql.Connection JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.objectweb.cjdbc.common.util.Constants;
31 import org.objectweb.cjdbc.controller.cache.CacheStatistics;
32 import org.objectweb.cjdbc.controller.cache.result.AbstractResultCache;
33 import org.objectweb.cjdbc.scenario.templates.SimpleRaidb1Template;
34 import org.objectweb.cjdbc.scenario.tools.util.RequestSender;
35
36 /**
37  * This class defines a ExtensiveCacheScenario
38  *
39  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
40  * @version 1.0
41  */

42 public class ExtensiveCacheScenario extends SimpleRaidb1Template
43 {
44   static final int LOOPS = 500;
45   static final int INTERVAL = 1;
46   static final boolean DISPLAY_RESULTS = false;
47   static final String JavaDoc HSQLDB = "hsqldb";
48
49   /**
50    * Test result cache with just reads
51    *
52    * @throws Exception if error occurs
53    */

54   public void testReadResultCache() throws Exception JavaDoc
55   {
56     testBatchCache(-1);
57   }
58
59   /**
60    * Test different cache with mixed read and write
61    *
62    * @throws Exception if fails
63    */

64   public void testReadWriteResultCache() throws Exception JavaDoc
65   {
66     testBatchCache(300);
67   }
68
69   private void testBatchCache(int writeRatio) throws Exception JavaDoc
70   {
71     CacheResult cr1 = testCache("hsqldb-raidb1-cache-database.xml", writeRatio);
72     CacheResult cr2 = testCache("hsqldb-raidb1-cache-table.xml", writeRatio);
73     CacheResult cr3 = testCache("hsqldb-raidb1-cache-column.xml", writeRatio);
74     CacheResult cr4 = testCache("hsqldb-raidb1-cache-columnUnique.xml",
75         writeRatio);
76     CacheResult cr5 = testCache("hsqldb-raidb1-cache-nocache.xml", writeRatio);
77     CacheResult cr6 = testCache(HSQLDB, writeRatio);
78
79     cr1.displayResult();
80     cr2.displayResult();
81     cr3.displayResult();
82     cr4.displayResult();
83     cr5.displayResult();
84     cr6.displayResult();
85   }
86
87   private CacheResult testCache(String JavaDoc databaseFile, int writeRatio)
88       throws Exception JavaDoc
89   {
90     Connection JavaDoc con = null;
91     if (databaseFile.equals(HSQLDB))
92     {
93       con = getHypersonicConnection(9001);
94     }
95     else
96     {
97       // Load database
98
cm.loadVirtualDatabases(controller, "myDB", databaseFile);
99       mainVdb = controller.getVirtualDatabase("myDB");
100       mainVdb.enableAllBackends();
101       con = getCJDBCConnection();
102     }
103
104     // Start request sender thread
105
RequestSender rs = new RequestSender(con);
106     rs.setLoopInThread(LOOPS);
107     rs.setRequestInterval(INTERVAL);
108     rs.setDoWriteEvery(writeRatio);
109     Thread JavaDoc t = new Thread JavaDoc(rs);
110
111     // Get timed test
112
long begin = System.currentTimeMillis();
113     t.start();
114     rs.setQuit(true);
115     t.join();
116     long end = System.currentTimeMillis();
117
118     // Test is finished collect data
119
CacheResult cr = new CacheResult();
120     cr.cacheFile = databaseFile;
121     cr.requests = LOOPS;
122     cr.time = end - begin;
123     cr.errors = rs.getExceptions();
124     if (databaseFile.equals(HSQLDB))
125       cr.nocache = true;
126     else
127     {
128       AbstractResultCache arc = mainVdb.getRequestManager().getResultCache();
129       if (arc != null)
130       {
131         CacheStatistics cs = arc.getCacheStatistics();
132         cr.hits = cs.getHits();
133         cr.ratio = cs.getCacheHitRatio();
134       }
135       else
136         cr.nocache = true;
137     }
138
139     // Display results
140
if (DISPLAY_RESULTS)
141       cr.displayResult();
142
143     mainVdb.shutdown(Constants.SHUTDOWN_FORCE);
144
145     // Return data
146
return cr;
147   }
148
149   class CacheResult
150   {
151     String JavaDoc cacheFile;
152     long time = 0;
153     int hits = 0;
154     long ratio = 0;
155     int requests = 0;
156     boolean nocache = false;
157     ArrayList JavaDoc errors;
158
159     void displayResult()
160     {
161       System.out
162           .println("-------------------------------------------------------");
163       System.out.println("Cache File:" + cacheFile);
164       System.out.println("Test lasted:" + time);
165       if (!nocache)
166       {
167         System.out.println("Total hits:" + hits);
168         System.out.println("Hit ratio:" + ratio);
169       }
170       else
171         System.out.println("No Cache Enabled");
172       for (int i = 0; i < errors.size(); i++)
173         System.out.println("ERROR[" + i + "]:"
174             + ((Exception JavaDoc) errors.get(i)).getMessage());
175       System.out
176           .println("-------------------------------------------------------");
177     }
178   }
179 }
Popular Tags