]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/scalability/LargeDataTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / scalability / LargeDataTest.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.scalability;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.testing.base.ExistingDatabaseTest;
22 import org.simantics.db.testing.common.WriteQuery;
23 import org.simantics.layer0.Layer0;
24
25 /**
26  * Creates large amount of instances in multiple transactions
27  * and the tries to verify that instances are written into the database.
28  * 
29  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
30  *
31  */
32 public class LargeDataTest extends ExistingDatabaseTest {
33         
34         private static int INSTANCE_COUNT = 1000; //100000
35         private static int MAX_INSTANCE_COUNT_PER_TRANSACTION = 100; // 1000;
36         private static boolean OPTIMIZED = false;
37         
38         public void testLargeData() throws Exception{
39                 final Resource rootLib = getSession().getRootLibrary();
40             final String random = getRandomString();
41                 if (DEBUG)
42                     System.out.println("Test start");
43                 for (int j = 0; j < INSTANCE_COUNT; j += MAX_INSTANCE_COUNT_PER_TRANSACTION) {
44                         final int base = j;
45                         getSession().syncRequest(new WriteQuery(this) {
46                                 @Override
47                                 public void run(WriteGraph g) throws Throwable {
48                                         Layer0 b = Layer0.getInstance(g);
49                                         for (int i = 0; i < MAX_INSTANCE_COUNT_PER_TRANSACTION; i++) {
50                                                 String newInstanceName = random + (base+i);
51                                                 Resource instance = g.newResource();
52                                                 if(!OPTIMIZED) {
53                                                         g.claim(instance, b.InstanceOf, null, b.Type);
54                                                         g.claimLiteral(instance, b.HasName, newInstanceName);
55                                                         g.claim(rootLib, b.ConsistsOf, instance);
56                                                 } else {
57                                                         Resource invName = g.getInverse(b.HasName);
58                                                         g.claim(instance, b.HasName, invName, b.Type);
59                                                         g.claimLiteral(instance, b.HasName, newInstanceName);
60                                                         g.claim(rootLib, b.ConsistsOf, b.PartOf, instance);
61                                                 }
62                                         }
63
64                                 }
65                         });
66                         if (DEBUG)
67                             System.out.println(j + MAX_INSTANCE_COUNT_PER_TRANSACTION);
68                 }
69                 
70                 checkException();
71                 if (DEBUG)
72                     System.out.println("Reading stage");
73                 getSession().syncRequest(new TestReadRequest() {
74                         @Override
75                         public void run(ReadGraph g) throws Throwable {
76                                 Layer0 b = Layer0.getInstance(g);
77                                 List<Boolean> found = new ArrayList<Boolean>(INSTANCE_COUNT);
78                                 for (int i = 0; i < INSTANCE_COUNT; i++) {
79                                         found.add(false);
80                                 }
81                                 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
82                                 for (Resource r : resources) {
83                                         String name = g.getPossibleRelatedValue(r, b.HasName);
84                     if (null == name || !name.startsWith(random, 0))
85                         continue;
86                     name = name.substring(random.length());
87                     try {
88                         int i = Integer.parseInt(name);
89                                                 found.set(i, true);
90
91                     } catch (NumberFormatException e) {
92                         continue; // not a number
93                     }
94                                 }
95                                 for (int i = 0; i < INSTANCE_COUNT; i++) {
96                                         if (!found.get(i))
97                                                 throw new Exception("Created resource " + i + " cannot be found");
98                                 }
99                         }
100                 });
101                 if (DEBUG)
102                     System.out.println("test done");
103                 checkException();
104         }
105
106 }