--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.performance.read;
+
+import java.util.concurrent.CountDownLatch;
+
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.TransactionPolicyKeep;
+import org.simantics.db.common.TransactionPolicyRelease;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.TransactionPolicySupport;
+import org.simantics.db.testing.base.TestCommonPerf;
+import org.simantics.db.testing.common.Tests;
+import org.simantics.db.testing.common.WriteQuery;
+
+/**
+ * Tests for Transaction
+ *
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ *
+ */
+public class TransactionTest extends TestCommonPerf {
+
+ final int REPEAT_COUNT = 2;//0;
+ final int REQUEST_COUNT = 50; //0; // 1000;
+ final int SPEED_LIMIT = 3000;
+ volatile int counter = 0;
+
+ public void testSyncWriteTransactions() throws DatabaseException {
+ Session session = Tests.getTestHandler().getSession();
+ System.out.println("Transaction policy is release.");
+ session.registerService(TransactionPolicySupport.class, new TransactionPolicyRelease());
+ loopSyncWriteTransactions(session);
+ session.registerService(TransactionPolicySupport.class, new TransactionPolicyKeep());
+ System.out.println("Transaction policy is keep.");
+ loopSyncWriteTransactions(session);
+ }
+ public void loopSyncWriteTransactions(Session session) {
+ double totalTime = 0;
+ counter = 0;
+ for (int j = 0; j < REPEAT_COUNT; ++j) {
+ long start = System.nanoTime();
+ for(int i = 0; i < REQUEST_COUNT; ++i) {
+ try {
+ session.syncRequest(new WriteQuery(this) {
+ @Override
+ public void run(WriteGraph graph) {
+ ++counter;
+ }
+ });
+ } catch (DatabaseException e) {
+ e.printStackTrace();
+ }
+ }
+ long end = System.nanoTime();
+ double time = (double)(end-start) * (double)(1e-9);
+ totalTime += time;
+// int percent = (int)((j+1) / (double)REPEAT_COUNT * 100);
+// System.out.println(percent + " precent done.");
+ }
+ if (REPEAT_COUNT * REQUEST_COUNT != counter)
+ fail("Transaction count does not match. Transaction count was " + counter);
+ double speed = counter / totalTime;
+ String t = "Transaction speed was " + speed + " empty synchronous write transactions per second."
+ + " Limit was > " + SPEED_LIMIT + ".";
+ System.out.println(t);
+// if (speed < SPEED_LIMIT)
+// fail(t);
+ }
+
+ public void testEmptyAsyncWriteTransactions() throws Exception {
+ final CountDownLatch latch = new CountDownLatch(REPEAT_COUNT * REQUEST_COUNT);
+ Session session = getSession();
+ long start = System.nanoTime();
+ for (int i = 0; i < REPEAT_COUNT * REQUEST_COUNT; i++) {
+ session.asyncRequest(new WriteQuery(this) {
+ @Override
+ public void run(WriteGraph graph) {
+ latch.countDown();
+ }
+ });
+ }
+ latch.await();
+ long end = System.nanoTime();
+ checkException();
+// int oldCounter = 0;
+// while (counter != REPEAT_COUNT * REQUEST_COUNT) {
+// checkException();
+// oldCounter = counter;
+//// Thread.sleep(1000);
+// int percent = (int)(counter / (double)(REPEAT_COUNT * REQUEST_COUNT) * 100);
+// System.out.println(percent + " precent done.");
+// if (oldCounter == counter)
+// fail("Too long time betwween two asynchronous transactions.");
+// }
+// checkException();
+ double totalTime = (double)(end-start) * (double)(1e-9);
+ double speed = REPEAT_COUNT / totalTime * REQUEST_COUNT;
+ String t = "Transaction speed was " + speed + " empty asynchronous write transactions per second."
+ + " Limit was > " + SPEED_LIMIT + ".";
+ System.out.println(t);
+ if (speed < SPEED_LIMIT)
+ fail(t);
+ }
+
+}