1 24 25 package org.objectweb.cjdbc.scenario.raidb1.cache; 26 27 import java.sql.Connection ; 28 import java.util.ArrayList ; 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 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 HSQLDB = "hsqldb"; 48 49 54 public void testReadResultCache() throws Exception 55 { 56 testBatchCache(-1); 57 } 58 59 64 public void testReadWriteResultCache() throws Exception 65 { 66 testBatchCache(300); 67 } 68 69 private void testBatchCache(int writeRatio) throws Exception 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 databaseFile, int writeRatio) 88 throws Exception 89 { 90 Connection con = null; 91 if (databaseFile.equals(HSQLDB)) 92 { 93 con = getHypersonicConnection(9001); 94 } 95 else 96 { 97 cm.loadVirtualDatabases(controller, "myDB", databaseFile); 99 mainVdb = controller.getVirtualDatabase("myDB"); 100 mainVdb.enableAllBackends(); 101 con = getCJDBCConnection(); 102 } 103 104 RequestSender rs = new RequestSender(con); 106 rs.setLoopInThread(LOOPS); 107 rs.setRequestInterval(INTERVAL); 108 rs.setDoWriteEvery(writeRatio); 109 Thread t = new Thread (rs); 110 111 long begin = System.currentTimeMillis(); 113 t.start(); 114 rs.setQuit(true); 115 t.join(); 116 long end = System.currentTimeMillis(); 117 118 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 if (DISPLAY_RESULTS) 141 cr.displayResult(); 142 143 mainVdb.shutdown(Constants.SHUTDOWN_FORCE); 144 145 return cr; 147 } 148 149 class CacheResult 150 { 151 String cacheFile; 152 long time = 0; 153 int hits = 0; 154 long ratio = 0; 155 int requests = 0; 156 boolean nocache = false; 157 ArrayList 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 ) errors.get(i)).getMessage()); 175 System.out 176 .println("-------------------------------------------------------"); 177 } 178 } 179 } | Popular Tags |