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.api.request.misc;
14 import java.util.HashSet;
15 import java.util.concurrent.Semaphore;
16 import java.util.concurrent.TimeUnit;
18 import org.junit.Test;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Session;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.SyncListener;
23 import org.simantics.db.request.Read;
24 import org.simantics.db.testing.base.TestCommonPerf;
25 import org.simantics.layer0.Layer0;
27 public class SyncRequestTest3 extends TestCommonPerf {
29 private static final int COUNT = 10000;
31 private static final int DEADLINE = 1000;
34 public void test() throws Exception {
36 final Session session = getSession();
38 final HashSet<Integer> check = new HashSet<Integer>();
39 for(int i=0;i<COUNT;i++) check.add(i);
41 final Semaphore s = new Semaphore(0);
43 long start = System.nanoTime();
45 for(int i=0;i<COUNT;i++) {
49 session.asyncRequest(new Read<String>() {
52 public String perform(ReadGraph graph) throws DatabaseException {
53 Layer0 l0 = Layer0.getInstance(graph);
54 return graph.getPossibleRelatedValue(rl, l0.HasName);
57 }, new SyncListener<String>() {
60 public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
61 throwable.printStackTrace();
65 public void execute(ReadGraph graph, String result) throws DatabaseException {
68 if(check.isEmpty()) s.release();
73 public boolean isDisposed() {
81 boolean success = s.tryAcquire(DEADLINE, TimeUnit.MILLISECONDS);
83 long end = System.nanoTime();
85 String t = "SyncRequestTest3 finished in " + 1e-9*(double)(end-start) + " s."
86 + " Limit was < " + (double)DEADLINE / 1000.0 + " s.";
87 System.out.println(t);