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.AsyncReadGraph;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Session;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.procedure.AsyncProcedure;
24 import org.simantics.db.procedure.SyncProcedure;
25 import org.simantics.db.request.AsyncRead;
26 import org.simantics.db.testing.annotation.Fails;
27 import org.simantics.db.testing.base.TestCommonPerf;
28 import org.simantics.layer0.Layer0;
30 public class SyncRequestTest extends TestCommonPerf {
32 private static final int COUNT = 1000000;
34 private static final int DEADLINE = 10000;
38 public void test() throws Exception {
40 final Session session = getSession();
41 final Layer0 b = Layer0.getInstance(session);
43 final HashSet<Integer> check = new HashSet<Integer>();
44 for(int i=0;i<COUNT;i++) check.add(i);
46 final Semaphore s = new Semaphore(0);
48 long start = System.nanoTime();
50 session.asyncRequest(new AsyncRead<String>() {
53 public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
54 for(int i=0;i<COUNT;i++) {
58 graph.forPossibleRelatedValue(rl, b.HasName, new SyncProcedure<String>() {
61 public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
62 throwable.printStackTrace();
66 public void execute(ReadGraph graph, String result) throws DatabaseException {
69 if(check.isEmpty()) s.release();
80 public int threadHash() {
85 public int getFlags() {
91 boolean success = s.tryAcquire(DEADLINE, TimeUnit.MILLISECONDS);
94 long end = System.nanoTime();
96 System.out.println("success in " + 1e-9*(double)(end-start) + " s.");