package org.jboss.test.cache.perf.basic;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.lock.IsolationLevel;
import org.jboss.cache.lock.LockStrategyFactory;
import org.jboss.cache.transaction.DummyTransactionManager;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.util.ArrayList;
import java.util.Properties;
public class LocalPerfTestCase extends TestCase
{
TreeCache cache_;
int cachingMode_ = TreeCache.LOCAL;
final static Properties p_;
String oldFactory_ = null;
final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
ArrayList nodeList_;
static final int depth_ = 3;
static final int children_ = 4;
DummyTransactionManager tm_;
static
{
p_ = new Properties();
p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
}
public LocalPerfTestCase(String name)
{
super(name);
}
public void setUp() throws Exception
{
super.setUp();
oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
DummyTransactionManager.getInstance();
initCaches(TreeCache.LOCAL);
nodeList_ = nodeGen(depth_, children_);
tm_ = new DummyTransactionManager();
log("LocalPerfAopTest: cacheMode=LOCAL, one cache");
}
public void tearDown() throws Exception
{
super.tearDown();
DummyTransactionManager.destroy();
destroyCaches();
if (oldFactory_ != null) {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
oldFactory_ = null;
}
}
void initCaches(int caching_mode) throws Exception
{
cachingMode_ = caching_mode;
cache_ = new TreeCache();
PropertyConfigurator config = new PropertyConfigurator();
config.configure(cache_, "META-INF/local-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
cache_.startService();
}
void destroyCaches() throws Exception
{
cache_.stopService();
cache_ = null;
}
public void testAll() throws Exception
{
log("=== No transaction ===");
DecimalFormat form = new DecimalFormat("#.00");
FieldPosition fieldPos = new FieldPosition(0);
StringBuffer dumbStr = new StringBuffer();
boolean hasTx = false;
long time1 = System.currentTimeMillis();
int nOps = _add(hasTx);
long time2 = System.currentTimeMillis();
double d = (double) (time2 - time1) / nOps;
log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
+ " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
" msec.");
dumbStr = new StringBuffer();
time1 = System.currentTimeMillis();
nOps = _get(hasTx);
time2 = System.currentTimeMillis();
d = (double) (time2 - time1) / nOps;
log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
+ " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
" msec.");
dumbStr = new StringBuffer();
time1 = System.currentTimeMillis();
nOps = _remove(hasTx);
time2 = System.currentTimeMillis();
d = (double) (time2 - time1) / nOps;
log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
+ " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
" msec.");
}
protected void setLevelRW()
{
log("set lock level to RWUpgrade ...");
LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
}
protected void setLevelSerial()
{
log("set lock level to SimpleLock ...");
LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE);
}
public void testAllTx_RWLock() throws Exception
{
setLevelRW();
allTx();
}
public void testAllTx_SimpleLock() throws Exception
{
setLevelSerial();
allTx();
}
protected void allTx() throws Exception
{
log("=== With transaction ===");
DecimalFormat form = new DecimalFormat("#.00");
FieldPosition fieldPos = new FieldPosition(0);
StringBuffer dumbStr = new StringBuffer();
boolean hasTx = true;
long time1 = System.currentTimeMillis();
int nOps = _add(hasTx);
long time2 = System.currentTimeMillis();
double d = (double) (time2 - time1) / nOps;
log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
+ " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
" msec.");
dumbStr = new StringBuffer();
time1 = System.currentTimeMillis();
nOps = _get(hasTx);
time2 = System.currentTimeMillis();
d = (double) (time2 - time1) / nOps;
log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
+ " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
" msec.");
dumbStr = new StringBuffer();
time1 = System.currentTimeMillis();
nOps = _remove(hasTx);
time2 = System.currentTimeMillis();
d = (double) (time2 - time1) / nOps;
log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
+ " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
" msec.");
}
private int _add(boolean hasTx) throws Exception
{
UserTransaction tx = null;
if (hasTx) {
tx = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
}
for (int i = 0; i < nodeList_.size(); i++) {
String key = Integer.toString(i);
String value = Integer.toString(i);
if (hasTx) {
tx.begin();
cache_.put((String) nodeList_.get(i), key, value);
tx.commit();
} else {
cache_.put((String) nodeList_.get(i), key, value);
}
}
return nodeList_.size();
}
private int _get(boolean hasTx) throws Exception
{
UserTransaction tx = null;
if (hasTx) {
tx = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
}
for (int i = 0; i < nodeList_.size(); i++) {
String key = Integer.toString(i);
if (hasTx) {
tx.begin();
cache_.get((String) nodeList_.get(i), key);
tx.commit();
} else {
cache_.get((String) nodeList_.get(i), key);
}
}
return nodeList_.size();
}
private int _remove(boolean hasTx) throws Exception
{
UserTransaction tx = null;
if (hasTx) {
tx = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
}
for (int i = 0; i < nodeList_.size(); i++) {
String key = Integer.toString(i);
if (hasTx) {
tx.begin();
cache_.remove((String) nodeList_.get(i), key);
tx.commit();
} else {
cache_.remove((String) nodeList_.get(i), key);
}
}
return nodeList_.size();
}
private ArrayList nodeGen(int depth, int children)
{
ArrayList strList = new ArrayList();
ArrayList oldList = new ArrayList();
ArrayList newList = new ArrayList();
oldList.add("/");
newList.add("/");
strList.add("/");
while (depth > 0) {
newList = new ArrayList();
for (int i = 0; i < oldList.size(); i++) {
for (int j = 0; j < children; j++) {
String tmp = (String) oldList.get(i);
tmp += Integer.toString(j);
if (depth != 1) tmp += "/";
newList.add(tmp);
}
}
strList.addAll(newList);
oldList = newList;
depth--;
}
log("Nodes generated: " + strList.size());
return strList;
}
public static Test suite() throws Exception
{
return new TestSuite(LocalPerfTestCase.class);
}
private void log(String str)
{
System.out.println(str);
}
}