001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.
006: //
007: // $Id: SimpleTest.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
008:
009: package org.ozoneDB.test.simple;
010:
011: import java.util.*;
012: import org.ozoneDB.*;
013: import org.ozoneDB.DxLib.*;
014: import org.ozoneDB.test.*;
015: import junit.framework.*;
016:
017: import org.apache.log4j.Category;
018:
019: /**
020: * @author <a href="http://www.softwarebuero.de/">SMB</a>
021: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:31 $
022: */
023: public class SimpleTest extends OzoneTestCase {
024:
025: protected int stopCount;
026:
027: /**
028: * log4j logger
029: */
030: private static Category fLog = Category
031: .getInstance(SimpleTest.class);
032:
033: /**
034: * Reture a suite of Test for JUnit runner
035: */
036: public static Test suite() {
037: TestSuite suite = new TestSuite();
038: suite.addTestSuite(SimpleTest.class);
039: return suite;
040: }
041:
042: /**
043: * Constructor
044: * @param name name of this TestCase
045: */
046: public SimpleTest(String name) {
047: super (name);
048: }
049:
050: final static private String AUTO1_OBJNAME = "auto_obj_0001";
051: final static private String AUTO2_OBJNAME = "auto_obj_0002";
052: final static private String AUTO3_OBJNAME = "auto_obj_0003";
053:
054: final static private String AUTO1_NAME = "auto_0001";
055: final static private String AUTO2_NAME = "auto_0002";
056: final static private String AUTO3_NAME = "auto_0003";
057:
058: public void testCreateLookupAndDelete() throws Exception {
059:
060: /* create a Auto1 */
061: fLog.debug("testCreateAndLookup(): creating object "
062: + AUTO1_OBJNAME);
063: Auto auto = (Auto) db().objectForName(AUTO1_OBJNAME);
064: if (auto != null) {
065: fLog.debug("testStoreAndLookup(): object name "
066: + AUTO1_OBJNAME + " already exists; deleting.");
067: db().deleteObject(auto);
068: }
069: auto = (Auto) db().createObject(AutoImpl.class.getName(),
070: OzoneInterface.Public, AUTO1_OBJNAME);
071: assertNotNull(auto);
072:
073: fLog.debug("testStoreAndLookup(): object created "
074: + auto.toString());
075:
076: /* set some value and try to come back and get it */
077: auto.setName(AUTO1_NAME);
078: auto.setAge(100);
079: auto = null;
080:
081: /* do a lookup on the db for simple1 */
082: auto = (Auto) db().objectForName(AUTO1_OBJNAME);
083:
084: assertNotNull(auto);
085: assertEquals(auto.name(), AUTO1_NAME);
086: assertEquals(auto.age().intValue(), 100);
087:
088: /* delete simple1 and do another lookup. It better not be there*/
089: db().deleteObject(auto);
090: auto = (Auto) db().objectForName(AUTO1_OBJNAME);
091: assertNull(auto);
092: }
093:
094: public void testNameObject() throws Exception {
095: Auto auto = (Auto) db().createObject(AutoImpl.class.getName());
096: db().nameObject(auto, AUTO1_OBJNAME);
097: fLog.debug("testNameObject(): name object " + AUTO1_OBJNAME);
098:
099: /* do a lookup on the db for simple1 */
100: auto = (Auto) db().objectForName(AUTO1_OBJNAME);
101: assertNotNull(auto);
102:
103: /* delete simple1 and do another lookup. It better not be there*/
104: db().deleteObject(auto);
105: auto = (Auto) db().objectForName(AUTO1_OBJNAME);
106: assertNull(auto);
107: }
108:
109: public void testOnDelete() throws Exception {
110: Auto auto = (Auto) db().createObject(AutoImpl.class.getName());
111: Auto auto2 = (Auto) db().createObject(AutoImpl.class.getName());
112:
113: db().nameObject(auto, AUTO1_OBJNAME);
114: db().nameObject(auto2, AUTO2_OBJNAME);
115:
116: auto2.setName("inner");
117: auto.setLink(auto2);
118:
119: fLog.debug("testOnDelete(): " + auto2 + " is inner to " + auto);
120:
121: db().deleteObject(auto);
122:
123: /**
124: * auto and auto2 should be deleted by now
125: */
126: auto = (Auto) db().objectForName(AUTO1_OBJNAME);
127: assertNull(auto);
128: auto = (Auto) db().objectForName(AUTO2_OBJNAME);
129: assertNull(auto);
130: }
131:
132: public void doCalls1(int c) throws Exception {
133: Auto auto = (Auto) db().createObject(AutoImpl.class.getName());
134: fLog.debug("doCalls1(): create new object" + auto);
135:
136: Random rand = new Random();
137: for (int i = 0; i < c; i++) {
138: int time = (int) (rand.nextFloat() * 1000);
139: // fLog.debug( "random sleep: " + time );
140: // Thread.sleep( time );
141: auto.age();
142: }
143:
144: db().deleteObject(auto);
145: }
146:
147: public void doCalls2(int c) throws Exception {
148: Auto auto = (Auto) db().createObject(AutoImpl.class.getName());
149: fLog.debug("doCalls2(): create new object" + auto);
150:
151: ExternalTransaction tx = db().newTransaction();
152: tx.begin();
153: for (int i = 0; i < c; i++) {
154: auto.age();
155: }
156: tx.commit();
157:
158: db().deleteObject(auto);
159: }
160:
161: public void testRMI() throws Exception {
162: final int c = 2000;
163:
164: long start = System.currentTimeMillis();
165: doCalls1(c);
166: long time = System.currentTimeMillis() - start;
167: fLog.debug(c + " iterations in different transactions: " + time
168: + "ms - ");
169: fLog.debug((float) time / c + "ms per call");
170:
171: start = System.currentTimeMillis();
172: doCalls2(c);
173: time = System.currentTimeMillis() - start;
174: fLog.debug(c + " iterations in one transaction: " + time
175: + "ms - ");
176: fLog.debug((float) time / c + "ms per call");
177: }
178:
179: public void testConnections() throws Exception {
180: final int numOfThreads = 10;
181: final int numOfCalls = 100;
182:
183: stopCount = 0;
184:
185: ClientThread1[] threads1 = new ClientThread1[numOfThreads];
186:
187: long start = System.currentTimeMillis();
188: for (int i = 0; i < numOfThreads; i++) {
189: threads1[i] = new ClientThread1(this , numOfCalls);
190: threads1[i].start();
191: }
192: while (stopCount < numOfThreads) {
193: Thread.sleep(500);
194: }
195: long time = System.currentTimeMillis() - start;
196: int iter = numOfThreads * numOfCalls;
197: fLog.debug(iter + " iterations (" + numOfThreads + "*"
198: + numOfCalls
199: + ") in different transactions per thread: " + time
200: + "ms - " + (float) time / iter + "ms per call");
201:
202: stopCount = 0;
203:
204: ClientThread2[] threads2 = new ClientThread2[numOfThreads];
205:
206: start = System.currentTimeMillis();
207: for (int i = 0; i < numOfThreads; i++) {
208: threads2[i] = new ClientThread2(this , numOfCalls);
209: threads2[i].start();
210: }
211: while (stopCount < numOfThreads) {
212: Thread.sleep(500);
213: }
214: time = System.currentTimeMillis() - start;
215: fLog.debug(iter + " iterations (" + numOfThreads + "*"
216: + numOfCalls + ")in one transaction per thread: "
217: + time + "ms - ");
218: fLog.debug((float) time / iter + "ms per call");
219: }
220:
221: protected synchronized void threadStopped() {
222: stopCount++;
223: }
224: }
225:
226: class ClientThread1 extends Thread {
227:
228: SimpleTest test;
229:
230: ExternalDatabase db;
231:
232: int c;
233:
234: ClientThread1(SimpleTest _test, int _c) {
235: test = _test;
236: db = test.db();
237: c = _c;
238: }
239:
240: public void run() {
241: try {
242: test.doCalls1(c);
243: test.threadStopped();
244: } catch (Exception e) {
245: e.printStackTrace();
246: throw new RuntimeException(e.toString());
247: }
248: }
249: }
250:
251: class ClientThread2 extends Thread {
252:
253: SimpleTest test;
254:
255: ExternalDatabase db;
256:
257: int c;
258:
259: ClientThread2(SimpleTest _test, int _c) {
260: test = _test;
261: db = test.db();
262: c = _c;
263: }
264:
265: public void run() {
266: try {
267: test.doCalls2(c);
268: test.threadStopped();
269: } catch (Exception e) {
270: e.printStackTrace();
271: throw new RuntimeException(e.toString());
272: }
273: }
274: }
|