You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/01/30 19:15:24 UTC
cvs commit: jakarta-ojb/src/test/org/apache/ojb/broker PerformanceTest2.java
arminw 2003/01/30 10:15:24
Modified: src/test/org/apache/ojb/broker PerformanceTest2.java
Log:
update test
Revision Changes Path
1.13 +168 -121 jakarta-ojb/src/test/org/apache/ojb/broker/PerformanceTest2.java
Index: PerformanceTest2.java
===================================================================
RCS file: /home/cvs/jakarta-ojb/src/test/org/apache/ojb/broker/PerformanceTest2.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- PerformanceTest2.java 9 Jan 2003 17:21:14 -0000 1.12
+++ PerformanceTest2.java 30 Jan 2003 18:15:24 -0000 1.13
@@ -10,11 +10,14 @@
import org.apache.ojb.broker.util.ObjectModificationDefaultImpl;
import org.apache.ojb.broker.util.logging.Logger;
import org.apache.ojb.broker.util.logging.LoggerFactory;
-import org.apache.ojb.broker.singlevm.PersistenceBrokerConfiguration;
import org.apache.ojb.odmg.OJB;
-import org.odmg.*;
+import org.odmg.Database;
+import org.odmg.Implementation;
+import org.odmg.OQLQuery;
+import org.odmg.Transaction;
import java.util.Iterator;
+import java.util.List;
/**
* stress test against one database using several threads
@@ -32,12 +35,12 @@
private static int whichTest = 3;
/**
- * times[0] startTime/test length
- * times[1] inserting times
- * times[2] fetching times
- * times[3] deleting times
+ * testTimes[0] startTime/test length
+ * testTimes[1] inserting times
+ * testTimes[2] fetching times
+ * testTimes[3] deleting times
*/
- private long[] times;
+ private long[] testTimes;
private int threadCount;
private Logger log = LoggerFactory.getLogger(PerformanceTest2.class);
public ThreadGroup threadGroup = new ThreadGroup("PerfTest");
@@ -48,30 +51,18 @@
this.threadCount = concurrentThreads;
}
- /**
- * generate client made ids
- */
- public synchronized static int getId()
- {
- return ++id;
- }
-
- public int getArticleCount()
+ public static void main(String[] args)
{
- Criteria c = new Criteria();
- Query q = new QueryByCriteria(PerformanceArticle.class, c);
- int count = 0;
- try
- {
- PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
- count = broker.getCount(q);
- broker.close();
- }
- catch (Exception e)
+ int loops = args.length > 4 ? Integer.parseInt(args[4]) : 1;
+ System.out.println("#######################################" +
+ "\n### Start stress test - do " + loops + " loop ###" +
+ "\n#######################################");
+ for (int i = 0; i < loops; i++)
{
- e.printStackTrace();
+ System.out.println("\n## perform loop " + (i + 1) + " ##");
+ performTest(args);
}
- return count;
+ System.exit(0);
}
public ThreadGroup getThreadGroup()
@@ -86,7 +77,7 @@
public synchronized void addTime(int position, long time)
{
- times[position] = times[position] + time;
+ testTimes[position] = testTimes[position] + time;
}
public synchronized int activeThreads(boolean notify)
@@ -110,13 +101,12 @@
*/
public void init() throws Exception
{
-// log.info("Start init performance test");
- times = new long[4];
+ log.info("init test");
+ testTimes = new long[4];
threadCount = concurrentThreads;
- if (whichTest==1 || whichTest ==3)
+ if (whichTest == 1 || whichTest == 3)
{
-// log.info("Init PB-API");
PerformanceArticle art = createArticle(1000);
PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
broker.beginTransaction();
@@ -129,9 +119,8 @@
broker.close();
}
- if (whichTest==2 || whichTest ==3)
+ if (whichTest == 2 || whichTest == 3)
{
-// log.info("Init ODMG-API");
PerformanceArticle art2 = createArticle(1001);
Implementation ojb = OJB.getInstance();
Database db = ojb.newDatabase();
@@ -145,41 +134,9 @@
db.deletePersistent(art2);
tx.commit();
db.close();
-// log.info("End init performance test");
- }
- }
-
- /**
- * factory method that createa an PerformanceArticle
- * @return the created PerformanceArticle object
- */
- private PerformanceArticle createArticle(int id)
- {
- PerformanceArticle a = new PerformanceArticle();
- if(clientKeyGeneration) a.setArticleId(getId());
- a.setArticleName("New Performance Article " + id);
- a.setMinimumStock(100);
- a.setOrderedUnits(17);
- a.setPrice(0.45);
- a.setProductGroupId(1);
- a.setStock(234);
- a.setSupplierId(4);
- a.setUnit("bottle");
- return a;
- }
- public static void main(String[] args)
- {
- int loops = args.length > 4 ? Integer.parseInt(args[4]) : 1;
- System.out.println("#######################################"+
- "\n### Start stress test - do "+loops+" loop ###"+
- "\n#######################################");
- for (int i = 0; i<loops; i++)
- {
- System.out.println("\n## perform loop "+(i+1)+" ##");
- performTest(args);
+ log.info("init test end");
}
- System.exit(0);
}
private static void performTest(String[] args)
@@ -206,15 +163,15 @@
int objectCount = 0;
int objectCountAfter = 0;
- if (whichTest==1 || whichTest ==3)
+ if (whichTest == 1 || whichTest == 3)
{
int activeThreadsStart = test.threadGroup.activeCount();
test.init();
objectCount = test.getArticleCount();
test.testMultithreaded_PB_api();
- while(test.threadGroup.activeCount()>activeThreadsStart)
+ while (test.threadGroup.activeCount() > activeThreadsStart)
{
- System.err.println("# active threads: "+ test.threadGroup.activeCount());
+ System.err.println("# active threads: " + test.threadGroup.activeCount());
try
{
Thread.sleep(1500);
@@ -223,23 +180,22 @@
{
}
}
-
- System.err.println("Test-Info: Objects in DB before PB test: "+ objectCount);
objectCountAfter = test.getArticleCount();
- System.err.println("Test-Info: Objects in DB after PB test: "+objectCountAfter);
- System.err.println("Test-Info: Stress test was successful? - "+(objectCount==objectCountAfter)+" -");
+ System.out.println("Test-Info: Objects in DB before PB test: " + objectCount);
+ System.out.println("Test-Info: Objects in DB after PB test: " + objectCountAfter);
+ System.out.println("Test-Info: Stress test was successful? - " + (objectCount == objectCountAfter) + " -");
}
- if (whichTest==2 || whichTest ==3)
+ if (whichTest == 2 || whichTest == 3)
{
int activeThreadsStart = test.threadGroup.activeCount();
test.init();
objectCount = test.getArticleCount();
test.testMultithreaded_ODMG_api();
- while(test.threadGroup.activeCount()>activeThreadsStart)
+ while (test.threadGroup.activeCount() > activeThreadsStart)
{
- System.err.println("# active threads: "+ test.threadGroup.activeCount());
+ System.err.println("# active threads: " + test.threadGroup.activeCount());
try
{
Thread.sleep(1500);
@@ -248,11 +204,10 @@
{
}
}
-
- System.err.println("Test-Info: Objects in DB before ODMG test: "+ objectCount);
objectCountAfter = test.getArticleCount();
- System.err.println("Test-Info: Objects in DB after ODMG test: "+objectCountAfter);
- System.err.println("Test-Info: Stress test was successful? - "+(objectCount==objectCountAfter)+" -");
+ System.out.println("Test-Info: Objects in DB before ODMG test: " + objectCount);
+ System.out.println("Test-Info: Objects in DB after ODMG test: " + objectCountAfter);
+ System.out.println("Test-Info: Stress test was successful? - " + (objectCount == objectCountAfter) + " -");
}
}
catch (Exception e)
@@ -264,8 +219,8 @@
public void testMultithreaded_PB_api()
{
String sep = System.getProperty("line.separator");
- times[0] = System.currentTimeMillis();
- log.info(sep+sep+"++ Start thread generation for PB api test ++");
+ testTimes[0] = System.currentTimeMillis();
+ log.info(sep + sep + "++ Start thread generation for PB api test ++");
log.info("Begin with performance test, " + concurrentThreads +
" concurrent threads, handle " + iterationsPerThread + " articles form each thread");
for (int i = 0; i < concurrentThreads; i++)
@@ -281,15 +236,15 @@
//this thread,
//cause we do not want to trace this loop
}
- times[0] = (long) (System.currentTimeMillis() - times[0]);
+ testTimes[0] = (long) (System.currentTimeMillis() - testTimes[0]);
log.info(buildTestSummary("PB API"));
- log.info("++ End of performance test PB api ++"+sep+sep);
+ log.info("++ End of performance test PB api ++" + sep + sep);
}
public void testMultithreaded_ODMG_api()
{
String sep = System.getProperty("line.separator");
- times[0] = System.currentTimeMillis();
+ testTimes[0] = System.currentTimeMillis();
log.info("++ Start thread generation for ODMG api test ++");
log.info("Begin with performance test, " + concurrentThreads +
" concurrent threads, handle " + iterationsPerThread + " articles form each thread");
@@ -305,9 +260,54 @@
//this thread,
//cause we do not want to trace this loop
}
- times[0] = (long) (System.currentTimeMillis() - times[0]);
+ testTimes[0] = (long) (System.currentTimeMillis() - testTimes[0]);
log.info(buildTestSummary("ODMG"));
- log.info("++ End of performance test ODMG api ++"+sep);
+ log.info("++ End of performance test ODMG api ++" + sep);
+ }
+
+ /**
+ * factory method that createa an PerformanceArticle
+ * @return the created PerformanceArticle object
+ */
+ private PerformanceArticle createArticle(int id)
+ {
+ PerformanceArticle a = new PerformanceArticle();
+ if (clientKeyGeneration) a.setArticleId(getId());
+ a.setArticleName("New Performance Article " + id);
+ a.setMinimumStock(100);
+ a.setOrderedUnits(17);
+ a.setPrice(0.45);
+ a.setProductGroupId(1);
+ a.setStock(234);
+ a.setSupplierId(4);
+ a.setUnit("bottle");
+ return a;
+ }
+
+ /**
+ * generate client made ids
+ */
+ public synchronized static int getId()
+ {
+ return ++id;
+ }
+
+ private int getArticleCount()
+ {
+ Criteria c = new Criteria();
+ Query q = new QueryByCriteria(PerformanceArticle.class, c);
+ int count = 0;
+ try
+ {
+ PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
+ count = broker.getCount(q);
+ broker.close();
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ return count;
}
private String buildTestSummary(String key)
@@ -319,27 +319,33 @@
buf.append(sep);
buf.append("TEST SUMMARY - " + key);
buf.append(sep);
- buf.append(concurrentThreads+" concurrent threads, handle " + iterationsPerThread + " articles per thread");
+ buf.append(concurrentThreads + " concurrent threads, handle " + iterationsPerThread + " articles per thread");
buf.append(sep);
- buf.append("Test period: " + (double) (((double) times[0]) / 1000) + " [sec]");
+ buf.append("Test period: " + (double) (((double) testTimes[0]) / 1000) + " [sec]");
buf.append(sep);
- buf.append("Inserting period: " + (times[1] / (concurrentThreads)) + " [msec]");
+ buf.append("Inserting period: " + (testTimes[1] / (concurrentThreads)) + " [msec]");
buf.append(sep);
- buf.append("Fetching period: " + (times[2] / (concurrentThreads)) + " [msec]");
+ buf.append("Fetching period: " + (testTimes[2] / (concurrentThreads)) + " [msec]");
buf.append(sep);
- buf.append("Deleting period: " + (times[3] / (concurrentThreads)) + " [msec]");
+ buf.append("Deleting period: " + (testTimes[3] / (concurrentThreads)) + " [msec]");
buf.append(sep);
buf.append("----------------------------------------------------");
return buf.toString();
}
+
+
+ // =====================================================================================
+ // Inner class
+ // =====================================================================================
+
/**
* ODMG-api test class
*/
class PerfomanceTestClientODMG implements Runnable
{
- private static final String PRE_NAME = "A_";
+ private static final String PRE_NAME = "ODMG_";
private PerformanceArticle[] arr;
private Implementation odmg;
private String threadName;
@@ -356,7 +362,7 @@
arr = new PerformanceArticle[iterationsPerThread];
for (int i = 0; i < iterationsPerThread; i++)
{
- PerformanceArticle a = createArticle(i, threadName);
+ PerformanceArticle a = createArticle(threadName);
arr[i] = a;
}
@@ -382,10 +388,10 @@
* factory method that createa an PerformanceArticle
* @return the created PerformanceArticle object
*/
- private PerformanceArticle createArticle(int id, String name)
+ private PerformanceArticle createArticle(String name)
{
PerformanceArticle a = new PerformanceArticle();
- if(clientKeyGeneration) a.setArticleId(getId());
+ if (clientKeyGeneration) a.setArticleId(getId());
a.setArticleName(PRE_NAME + name);
a.setMinimumStock(100);
a.setOrderedUnits(17);
@@ -411,7 +417,7 @@
tx.commit();
db.close();
long stop = System.currentTimeMillis();
- times[3] = times[3] + (stop - start);
+ addTime(3, stop - start);
}
protected void insertNewArticles() throws Exception
@@ -428,7 +434,7 @@
tx.commit();
db.close();
long stop = System.currentTimeMillis();
- times[1] = times[1] + (stop - start);
+ addTime(1, stop - start);
}
protected void readArticlesByCursor() throws Exception
@@ -438,15 +444,15 @@
Database db = odmg.newDatabase();
db.open(databaseName, Database.OPEN_READ_WRITE);
-// for better performance we could read without a tx
-// Transaction tx = ojb.newTransaction();
-// tx.begin();
+
+ Transaction tx = odmg.newTransaction();
+ tx.begin();
OQLQuery query = odmg.newOQLQuery();
String sql = "select allArticles from " + PerformanceArticle.class.getName() +
- " where articleName = \"" + PRE_NAME + threadName+"\"";
+ " where articleName = \"" + PRE_NAME + threadName + "\"";
query.create(sql);
- DList allProducts = (DList) query.execute();
-// tx.commit();
+ List allProducts = (List) query.execute();
+ tx.commit();
Iterator iter = allProducts.iterator();
int fetchCount = 0;
while (iter.hasNext())
@@ -454,33 +460,48 @@
fetchCount++;
PerformanceArticle a = (PerformanceArticle) iter.next();
}
+ if (fetchCount != arr.length)
+ {
+ System.out.println("## Could not found all inserted objects, expected " +
+ (arr.length) + " found " + fetchCount + " ##");
+ }
db.close();
long stop = System.currentTimeMillis();
- times[2] = times[2] + (stop - start);
+ addTime(2, stop - start);
}
}
+
+ // =====================================================================================
+ // Inner class
+ // =====================================================================================
+
/**
* PB-api test class
*/
class PerfomanceTestClientPB implements Runnable
{
private PerformanceArticle[] arr;
+ private static final String PRE_NAME = "PB_";
+ private String threadName;
public PerfomanceTestClientPB()
{
+
+ }
+
+ public void run()
+ {
+ threadName = Thread.currentThread().toString();
arr = new PerformanceArticle[iterationsPerThread];
for (int i = 0; i < iterationsPerThread; i++)
{
- PerformanceArticle a = createArticle(i);
+ PerformanceArticle a = createArticle(threadName);
arr[i] = a;
}
- //log.info("Articles created");
- }
- public void run()
- {
+
//log.info("Thread "+this+" run");
try
{
@@ -503,11 +524,11 @@
* factory method that createa an PerformanceArticle
* @return the created PerformanceArticle object
*/
- private PerformanceArticle createArticle(int id)
+ private PerformanceArticle createArticle(String name)
{
PerformanceArticle a = new PerformanceArticle();
- if(clientKeyGeneration) a.setArticleId(getId());
- a.setArticleName("New Performance Article " + id);
+ if (clientKeyGeneration) a.setArticleId(getId());
+ a.setArticleName(PRE_NAME + name);
a.setMinimumStock(100);
a.setOrderedUnits(17);
a.setPrice(0.45);
@@ -539,9 +560,32 @@
}
long stop = System.currentTimeMillis();
- times[3] = times[3] + (stop - start);
+ addTime(3, stop - start);
}
+// protected void deleteArticles() throws Exception
+// {
+// long start = System.currentTimeMillis();
+//
+// PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
+// try
+// {
+// broker.beginTransaction();
+// for (int i = 0; i < arr.length; i++)
+// {
+// broker.delete(arr[i]);
+// }
+// broker.commitTransaction();
+// }
+// finally
+// {
+// if(broker != null) broker.close();
+// }
+//
+// long stop = System.currentTimeMillis();
+// times[3] = times[3] + (stop - start);
+// }
+
protected void insertNewArticles() throws Exception
{
@@ -568,7 +612,7 @@
}
long stop = System.currentTimeMillis();
- times[1] = times[1] + (stop - start);
+ addTime(1, stop - start);
}
protected void readArticlesByCursor() throws Exception
@@ -577,7 +621,7 @@
int artId = arr[0].articleId;
Criteria c = new Criteria();
- c.addBetween("articleId", new Integer(artId), new Integer(artId + iterationsPerThread));
+ c.addLike("articleName", PRE_NAME+threadName);
Query q = new QueryByCriteria(PerformanceArticle.class, c);
PersistenceBroker broker = null;
@@ -591,15 +635,18 @@
fetchCount++;
PerformanceArticle a = (PerformanceArticle) iter.next();
}
+ if (fetchCount != arr.length)
+ {
+ System.out.println("## Could not found all inserted objects, expected " +
+ (arr.length) + " found " + fetchCount + " ##");
+ }
}
finally
{
if (broker != null) broker.close();
}
-
-
long stop = System.currentTimeMillis();
- times[2] = times[2] + (stop - start);
+ addTime(2, stop - start);
}
}
}