X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fperformance%2Fread%2FTransactionTest.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fperformance%2Fread%2FTransactionTest.java;h=5b84f34f4818eeee7fea94f0b841671de1575d7f;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/TransactionTest.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/TransactionTest.java new file mode 100644 index 000000000..5b84f34f4 --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/TransactionTest.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * 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 + * + */ +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); + } + +}