1 9 package test.simple; 10 11 import junit.framework.Test; 12 import junit.framework.TestSuite; 13 import org.apache.log4j.Category; 14 import org.ozoneDB.ExternalDatabase; 15 import org.ozoneDB.ExternalTransaction; 16 import org.ozoneDB.OzoneInterface; 17 import test.OzoneTestCase; 18 19 import java.util.Random ; 20 21 25 public class SimpleTest extends OzoneTestCase { 26 27 protected int stopCount; 28 29 32 private static Category fLog = Category.getInstance(SimpleTest.class); 33 34 37 public static Test suite() { 38 TestSuite suite = new TestSuite(); 39 suite.addTestSuite(SimpleTest.class); 40 return suite; 41 } 42 43 47 public SimpleTest(String name) { 48 super(name); 49 } 50 51 final static private String AUTO1_OBJNAME = "auto_obj_0001"; 52 final static private String AUTO2_OBJNAME = "auto_obj_0002"; 53 54 final static private String AUTO1_NAME = "auto_0001"; 55 56 public void testCreateLookupAndDelete() throws Exception { 57 58 59 fLog.debug("testCreateAndLookup(): creating object " + AUTO1_OBJNAME); 60 Auto auto = (Auto) db().objectForName(AUTO1_OBJNAME); 61 if (auto != null) { 62 fLog.debug("testStoreAndLookup(): object name " + AUTO1_OBJNAME + " already exists; deleting."); 63 db().deleteObject(auto); 64 } 65 auto = (Auto) db().createObject(AutoImpl.class.getName(), OzoneInterface.Public, AUTO1_OBJNAME); 66 assertNotNull(auto); 67 68 fLog.debug("testStoreAndLookup(): object created " + auto.toString()); 69 70 71 auto.setName(AUTO1_NAME); 72 auto.setAge(100); 73 auto = null; 74 75 76 auto = (Auto) db().objectForName(AUTO1_OBJNAME); 77 78 assertNotNull(auto); 79 assertEquals(auto.name(), AUTO1_NAME); 80 assertEquals(auto.age().intValue(), 100); 81 82 83 db().deleteObject(auto); 84 auto = (Auto) db().objectForName(AUTO1_OBJNAME); 85 assertNull(auto); 86 } 87 88 public void testNameObject() throws Exception { 89 Auto auto = (Auto) db().createObject(AutoImpl.class.getName()); 90 db().nameObject(auto, AUTO1_OBJNAME); 91 fLog.debug("testNameObject(): name object " + AUTO1_OBJNAME); 92 93 94 auto = (Auto) db().objectForName(AUTO1_OBJNAME); 95 assertNotNull(auto); 96 97 98 db().deleteObject(auto); 99 auto = (Auto) db().objectForName(AUTO1_OBJNAME); 100 assertNull(auto); 101 } 102 103 public void testOnDelete() throws Exception { 104 Auto auto = (Auto) db().createObject(AutoImpl.class.getName()); 105 Auto auto2 = (Auto) db().createObject(AutoImpl.class.getName()); 106 107 db().nameObject(auto, AUTO1_OBJNAME); 108 db().nameObject(auto2, AUTO2_OBJNAME); 109 110 auto2.setName("inner"); 111 auto.setLink(auto2); 112 113 fLog.debug("testOnDelete(): " + auto2 + " is inner to " + auto); 114 115 db().deleteObject(auto); 116 117 120 auto = (Auto) db().objectForName(AUTO1_OBJNAME); 121 assertNull(auto); 122 auto = (Auto) db().objectForName(AUTO2_OBJNAME); 123 assertNull(auto); 124 } 125 126 127 public void doCalls1(int c) throws Exception { 128 Auto auto = (Auto) db().createObject(AutoImpl.class.getName()); 129 fLog.debug("doCalls1(): create new object" + auto); 130 131 Random rand = new Random (); 132 for (int i = 0; i < c; i++) { 133 int time = (int) (rand.nextFloat() * 1000); 134 auto.age(); 137 } 138 139 db().deleteObject(auto); 140 } 141 142 143 public void doCalls2(int c) throws Exception { 144 Auto auto = (Auto) db().createObject(AutoImpl.class.getName()); 145 fLog.debug("doCalls2(): create new object" + auto); 146 147 ExternalTransaction tx = db().newTransaction(); 148 tx.begin(); 149 for (int i = 0; i < c; i++) { 150 auto.age(); 151 } 152 tx.commit(); 153 154 db().deleteObject(auto); 155 } 156 157 158 public void testRMI() throws Exception { 159 final int c = 2000; 160 161 long start = System.currentTimeMillis(); 162 doCalls1(c); 163 long time = System.currentTimeMillis() - start; 164 fLog.debug(c + " iterations in different transactions: " + time + "ms - "); 165 fLog.debug((float) time / c + "ms per call"); 166 167 start = System.currentTimeMillis(); 168 doCalls2(c); 169 time = System.currentTimeMillis() - start; 170 fLog.debug(c + " iterations in one transaction: " + time + "ms - "); 171 fLog.debug((float) time / c + "ms per call"); 172 } 173 174 175 public void testConnections() throws Exception { 176 final int numOfThreads = 10; 177 final int numOfCalls = 100; 178 179 stopCount = 0; 180 181 ClientThread1[] threads1 = new ClientThread1[numOfThreads]; 182 183 long start = System.currentTimeMillis(); 184 for (int i = 0; i < numOfThreads; i++) { 185 threads1[i] = new ClientThread1(this, numOfCalls); 186 threads1[i].start(); 187 } 188 while (stopCount < numOfThreads) { 189 Thread.sleep(500); 190 } 191 long time = System.currentTimeMillis() - start; 192 int iter = numOfThreads * numOfCalls; 193 fLog.debug(iter + " iterations (" + numOfThreads + "*" 194 + numOfCalls + ") in different transactions per thread: " 195 + time + "ms - " + (float) time / iter + "ms per call"); 196 197 stopCount = 0; 198 199 ClientThread2[] threads2 = new ClientThread2[numOfThreads]; 200 201 start = System.currentTimeMillis(); 202 for (int i = 0; i < numOfThreads; i++) { 203 threads2[i] = new ClientThread2(this, numOfCalls); 204 threads2[i].start(); 205 } 206 while (stopCount < numOfThreads) { 207 Thread.sleep(500); 208 } 209 time = System.currentTimeMillis() - start; 210 fLog.debug(iter + " iterations (" + numOfThreads + "*" + numOfCalls + ")in one transaction per thread: " + time + "ms - "); 211 fLog.debug((float) time / iter + "ms per call"); 212 } 213 214 215 protected synchronized void threadStopped() { 216 stopCount++; 217 } 218 } 219 220 221 class ClientThread1 extends Thread { 222 223 SimpleTest test; 224 225 ExternalDatabase db; 226 227 int c; 228 229 ClientThread1(SimpleTest _test, int _c) { 230 test = _test; 231 db = test.db(); 232 c = _c; 233 } 234 235 public void run() { 236 try { 237 test.doCalls1(c); 238 test.threadStopped(); 239 } catch (Exception e) { 240 e.printStackTrace(); 241 throw new RuntimeException (e.toString()); 242 } 243 } 244 } 245 246 247 class ClientThread2 extends Thread { 248 249 SimpleTest test; 250 251 ExternalDatabase db; 252 253 int c; 254 255 ClientThread2(SimpleTest _test, int _c) { 256 test = _test; 257 db = test.db(); 258 c = _c; 259 } 260 261 public void run() { 262 try { 263 test.doCalls2(c); 264 test.threadStopped(); 265 } catch (Exception e) { 266 e.printStackTrace(); 267 throw new RuntimeException (e.toString()); 268 } 269 } 270 } 271 | Popular Tags |