1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.performance.read;
14 import java.util.concurrent.CountDownLatch;
16 import org.simantics.db.Session;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.TransactionPolicyKeep;
19 import org.simantics.db.common.TransactionPolicyRelease;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.service.TransactionPolicySupport;
22 import org.simantics.db.testing.base.TestCommonPerf;
23 import org.simantics.db.testing.common.Tests;
24 import org.simantics.db.testing.common.WriteQuery;
27 * Tests for Transaction
29 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
32 public class TransactionTest extends TestCommonPerf {
34 final int REPEAT_COUNT = 2;//0;
35 final int REQUEST_COUNT = 50; //0; // 1000;
36 final int SPEED_LIMIT = 3000;
37 volatile int counter = 0;
39 public void testSyncWriteTransactions() throws DatabaseException {
40 Session session = Tests.getTestHandler().getSession();
41 System.out.println("Transaction policy is release.");
42 session.registerService(TransactionPolicySupport.class, new TransactionPolicyRelease());
43 loopSyncWriteTransactions(session);
44 session.registerService(TransactionPolicySupport.class, new TransactionPolicyKeep());
45 System.out.println("Transaction policy is keep.");
46 loopSyncWriteTransactions(session);
48 public void loopSyncWriteTransactions(Session session) {
51 for (int j = 0; j < REPEAT_COUNT; ++j) {
52 long start = System.nanoTime();
53 for(int i = 0; i < REQUEST_COUNT; ++i) {
55 session.syncRequest(new WriteQuery(this) {
57 public void run(WriteGraph graph) {
61 } catch (DatabaseException e) {
65 long end = System.nanoTime();
66 double time = (double)(end-start) * (double)(1e-9);
68 // int percent = (int)((j+1) / (double)REPEAT_COUNT * 100);
69 // System.out.println(percent + " precent done.");
71 if (REPEAT_COUNT * REQUEST_COUNT != counter)
72 fail("Transaction count does not match. Transaction count was " + counter);
73 double speed = counter / totalTime;
74 String t = "Transaction speed was " + speed + " empty synchronous write transactions per second."
75 + " Limit was > " + SPEED_LIMIT + ".";
76 System.out.println(t);
77 // if (speed < SPEED_LIMIT)
81 public void testEmptyAsyncWriteTransactions() throws Exception {
82 final CountDownLatch latch = new CountDownLatch(REPEAT_COUNT * REQUEST_COUNT);
83 Session session = getSession();
84 long start = System.nanoTime();
85 for (int i = 0; i < REPEAT_COUNT * REQUEST_COUNT; i++) {
86 session.asyncRequest(new WriteQuery(this) {
88 public void run(WriteGraph graph) {
94 long end = System.nanoTime();
96 // int oldCounter = 0;
97 // while (counter != REPEAT_COUNT * REQUEST_COUNT) {
99 // oldCounter = counter;
100 //// Thread.sleep(1000);
101 // int percent = (int)(counter / (double)(REPEAT_COUNT * REQUEST_COUNT) * 100);
102 // System.out.println(percent + " precent done.");
103 // if (oldCounter == counter)
104 // fail("Too long time betwween two asynchronous transactions.");
107 double totalTime = (double)(end-start) * (double)(1e-9);
108 double speed = REPEAT_COUNT / totalTime * REQUEST_COUNT;
109 String t = "Transaction speed was " + speed + " empty asynchronous write transactions per second."
110 + " Limit was > " + SPEED_LIMIT + ".";
111 System.out.println(t);
112 if (speed < SPEED_LIMIT)