]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/TransactionTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / performance / read / TransactionTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.performance.read;
13
14 import java.util.concurrent.CountDownLatch;
15
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;
25
26 /**
27  * Tests for Transaction
28  *
29  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
30  *
31  */
32 public class TransactionTest extends TestCommonPerf {
33
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;
38
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);
47     }
48         public void loopSyncWriteTransactions(Session session) {
49             double totalTime = 0;
50             counter = 0;
51             for (int j = 0; j < REPEAT_COUNT; ++j) {
52             long start = System.nanoTime();
53             for(int i = 0; i < REQUEST_COUNT; ++i) {
54                 try {
55                                         session.syncRequest(new WriteQuery(this) {
56                                             @Override
57                                             public void run(WriteGraph graph) {
58                                                 ++counter;
59                                             }
60                                         });
61                                 } catch (DatabaseException e) {
62                                         e.printStackTrace();
63                                 }
64             }
65             long end = System.nanoTime();
66             double time = (double)(end-start) * (double)(1e-9);
67             totalTime += time;
68 //            int percent = (int)((j+1) / (double)REPEAT_COUNT * 100);
69 //            System.out.println(percent + " precent done.");
70             }
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)
78 //              fail(t);
79         }
80
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) {
87                                 @Override
88                                 public void run(WriteGraph graph) {
89                                     latch.countDown();
90                                 }
91                         });
92                 }
93                 latch.await();
94         long end = System.nanoTime();
95                 checkException();
96 //              int oldCounter = 0;
97 //              while (counter != REPEAT_COUNT * REQUEST_COUNT) {
98 //            checkException();
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.");
105 //              }
106 //        checkException();
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)
113                 fail(t);
114         }
115
116 }