📄 simpletest.java
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.
//
// $Id$
package test.simple;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.log4j.Category;
import org.ozoneDB.ExternalDatabase;
import org.ozoneDB.ExternalTransaction;
import org.ozoneDB.OzoneInterface;
import test.OzoneTestCase;
import java.util.Random;
/**
* @author <a href="http://www.softwarebuero.de/">SMB</a>
* @version $Revision$Date$
*/
public class SimpleTest extends OzoneTestCase {
protected int stopCount;
/**
* log4j logger
*/
private static Category fLog = Category.getInstance(SimpleTest.class);
/**
* Reture a suite of Test for JUnit runner
*/
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(SimpleTest.class);
return suite;
}
/**
* Constructor
* @param name name of this TestCase
*/
public SimpleTest(String name) {
super(name);
}
final static private String AUTO1_OBJNAME = "auto_obj_0001";
final static private String AUTO2_OBJNAME = "auto_obj_0002";
final static private String AUTO1_NAME = "auto_0001";
public void testCreateLookupAndDelete() throws Exception {
/* create a Auto1 */
fLog.debug("testCreateAndLookup(): creating object " + AUTO1_OBJNAME);
Auto auto = (Auto) db().objectForName(AUTO1_OBJNAME);
if (auto != null) {
fLog.debug("testStoreAndLookup(): object name " + AUTO1_OBJNAME + " already exists; deleting.");
db().deleteObject(auto);
}
auto = (Auto) db().createObject(AutoImpl.class.getName(), OzoneInterface.Public, AUTO1_OBJNAME);
assertNotNull(auto);
fLog.debug("testStoreAndLookup(): object created " + auto.toString());
/* set some value and try to come back and get it */
auto.setName(AUTO1_NAME);
auto.setAge(100);
auto = null;
/* do a lookup on the db for simple1 */
auto = (Auto) db().objectForName(AUTO1_OBJNAME);
assertNotNull(auto);
assertEquals(auto.name(), AUTO1_NAME);
assertEquals(auto.age().intValue(), 100);
/* delete simple1 and do another lookup. It better not be there*/
db().deleteObject(auto);
auto = (Auto) db().objectForName(AUTO1_OBJNAME);
assertNull(auto);
}
public void testNameObject() throws Exception {
Auto auto = (Auto) db().createObject(AutoImpl.class.getName());
db().nameObject(auto, AUTO1_OBJNAME);
fLog.debug("testNameObject(): name object " + AUTO1_OBJNAME);
/* do a lookup on the db for simple1 */
auto = (Auto) db().objectForName(AUTO1_OBJNAME);
assertNotNull(auto);
/* delete simple1 and do another lookup. It better not be there*/
db().deleteObject(auto);
auto = (Auto) db().objectForName(AUTO1_OBJNAME);
assertNull(auto);
}
public void testOnDelete() throws Exception {
Auto auto = (Auto) db().createObject(AutoImpl.class.getName());
Auto auto2 = (Auto) db().createObject(AutoImpl.class.getName());
db().nameObject(auto, AUTO1_OBJNAME);
db().nameObject(auto2, AUTO2_OBJNAME);
auto2.setName("inner");
auto.setLink(auto2);
fLog.debug("testOnDelete(): " + auto2 + " is inner to " + auto);
db().deleteObject(auto);
/**
* auto and auto2 should be deleted by now
*/
auto = (Auto) db().objectForName(AUTO1_OBJNAME);
assertNull(auto);
auto = (Auto) db().objectForName(AUTO2_OBJNAME);
assertNull(auto);
}
public void doCalls1(int c) throws Exception {
Auto auto = (Auto) db().createObject(AutoImpl.class.getName());
fLog.debug("doCalls1(): create new object" + auto);
Random rand = new Random();
for (int i = 0; i < c; i++) {
int time = (int) (rand.nextFloat() * 1000);
// fLog.debug( "random sleep: " + time );
// Thread.sleep( time );
auto.age();
}
db().deleteObject(auto);
}
public void doCalls2(int c) throws Exception {
Auto auto = (Auto) db().createObject(AutoImpl.class.getName());
fLog.debug("doCalls2(): create new object" + auto);
ExternalTransaction tx = db().newTransaction();
tx.begin();
for (int i = 0; i < c; i++) {
auto.age();
}
tx.commit();
db().deleteObject(auto);
}
public void testRMI() throws Exception {
final int c = 2000;
long start = System.currentTimeMillis();
doCalls1(c);
long time = System.currentTimeMillis() - start;
fLog.debug(c + " iterations in different transactions: " + time + "ms - ");
fLog.debug((float) time / c + "ms per call");
start = System.currentTimeMillis();
doCalls2(c);
time = System.currentTimeMillis() - start;
fLog.debug(c + " iterations in one transaction: " + time + "ms - ");
fLog.debug((float) time / c + "ms per call");
}
public void testConnections() throws Exception {
final int numOfThreads = 10;
final int numOfCalls = 100;
stopCount = 0;
ClientThread1[] threads1 = new ClientThread1[numOfThreads];
long start = System.currentTimeMillis();
for (int i = 0; i < numOfThreads; i++) {
threads1[i] = new ClientThread1(this, numOfCalls);
threads1[i].start();
}
while (stopCount < numOfThreads) {
Thread.sleep(500);
}
long time = System.currentTimeMillis() - start;
int iter = numOfThreads * numOfCalls;
fLog.debug(iter + " iterations (" + numOfThreads + "*"
+ numOfCalls + ") in different transactions per thread: "
+ time + "ms - " + (float) time / iter + "ms per call");
stopCount = 0;
ClientThread2[] threads2 = new ClientThread2[numOfThreads];
start = System.currentTimeMillis();
for (int i = 0; i < numOfThreads; i++) {
threads2[i] = new ClientThread2(this, numOfCalls);
threads2[i].start();
}
while (stopCount < numOfThreads) {
Thread.sleep(500);
}
time = System.currentTimeMillis() - start;
fLog.debug(iter + " iterations (" + numOfThreads + "*" + numOfCalls + ")in one transaction per thread: " + time + "ms - ");
fLog.debug((float) time / iter + "ms per call");
}
protected synchronized void threadStopped() {
stopCount++;
}
}
class ClientThread1 extends Thread {
SimpleTest test;
ExternalDatabase db;
int c;
ClientThread1(SimpleTest _test, int _c) {
test = _test;
db = test.db();
c = _c;
}
public void run() {
try {
test.doCalls1(c);
test.threadStopped();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.toString());
}
}
}
class ClientThread2 extends Thread {
SimpleTest test;
ExternalDatabase db;
int c;
ClientThread2(SimpleTest _test, int _c) {
test = _test;
db = test.db();
c = _c;
}
public void run() {
try {
test.doCalls2(c);
test.threadStopped();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -