]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/CachedQueryTest.java
Multiple reader thread support for db client
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / performance / read / CachedQueryTest.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 org.simantics.db.ReadGraph;
15 import org.simantics.db.Session;
16 import org.simantics.db.procedure.Procedure;
17 import org.simantics.db.request.Read;
18 import org.simantics.db.testing.base.TestCommonPerf;
19
20
21 /**
22  * Tests for CachedQueryTest
23  * 
24  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
25  *
26  */
27 public class CachedQueryTest extends TestCommonPerf {
28
29         final static int QUERIES = 5000;
30         final static double TIME = 1.0;
31         
32         int counter = 0;
33         
34         public void testTransactions() throws Exception {
35                 
36                 Session session = getSession();
37                 
38                 Read<Boolean> req = new Read<Boolean>() {
39
40                         @Override
41                         public Boolean perform(ReadGraph graph) {
42                                 return true;
43                         }
44                         
45                 };
46                 
47                 Procedure<Boolean> proc = new Procedure<Boolean>() {
48                         
49                         @Override
50                         public void execute(Boolean result) {
51                                 synchronized(this) {
52                                         counter++;
53                                 }
54                         }
55
56                 public void exception(Throwable t) {
57                 System.out.print("CachedQueryTest request failed: ");
58                 t.printStackTrace();
59             }
60
61                 };
62                 
63                 session.syncRequest(req, proc);
64
65                 long start = System.nanoTime();
66
67                 for(int i = 0; i < QUERIES ; i++) {
68                         session.syncRequest(req, proc);
69                 }
70                 
71                 if(counter != (QUERIES + 1)) throw new Error("Invalid amount of notifications (got " + counter + ", expexted " + (QUERIES + 1) + ").");
72                 
73                 checkException();
74
75                 long end = System.nanoTime();
76                 
77                 double time = (double)(end-start) * (double)(1e-9);
78                 
79                 String t = "CachedQueryTest finished with time " + time + ". Limit was < " + TIME;
80                 System.out.println(t);
81                 if (time > TIME)
82                         fail(t); 
83         }
84
85 }