]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/Issue3199Test2.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / regression / bugs / Issue3199Test2.java
1 package org.simantics.db.tests.regression.bugs;
2
3 import java.util.UUID;
4
5 import org.junit.Test;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.Session;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.WriteOnlyGraph;
11 import org.simantics.db.common.request.ReadRequest;
12 import org.simantics.db.common.request.WriteOnlyRequest;
13 import org.simantics.db.common.request.WriteRequest;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.util.SessionGarbageCollection;
16 import org.simantics.db.service.ClusterControl;
17 import org.simantics.db.service.ManagementSupport;
18 import org.simantics.db.service.XSupport;
19 import org.simantics.db.testing.cases.FreshDatabaseTest;
20 import org.simantics.db.testing.common.TestBase;
21 import org.simantics.db.testing.impl.Configuration;
22 import org.simantics.layer0.Layer0;
23
24 public class Issue3199Test2 extends FreshDatabaseTest {
25     static final int RESOURCE_COUNT = Configuration.get().i3199ResourceCount; // 10 * 1000 * 1000;
26     static final int CONSIST_COUNT = 500; // How many resources in the same parent.
27     static final int COMMIT_COUNT = 50 * 1000; // How many resources in the same commit.
28     static final boolean DEBUG = false;
29     static final boolean VERBOSE = false;
30     Session session;
31     Resource testRoot;
32     ClusterControl clusterControl;
33
34     @Test
35     public void testSimanticsIssue3199()
36             throws DatabaseException {
37         session = getSession();
38         session.syncRequest(new Init());
39         long start = System.nanoTime();
40         session.syncRequest(new Create());
41         long duration = System.nanoTime() - start;
42         if (DEBUG)
43             System.out.println("Write done in " + 1e-9 * duration + " seconds.");
44         start = System.nanoTime();
45         session.syncRequest(new Query());
46         long total = duration;
47         if (DEBUG) {
48             duration = System.nanoTime() - start;
49             total += duration;
50             System.out.println("Read done in " + 1e-9 * duration + " seconds.");
51             System.out.println("Both done in " + 1e-9 * total + " seconds.");
52         }
53     }
54
55     class Init extends WriteRequest {
56         @Override
57         public void perform(WriteGraph g) throws DatabaseException {
58             Layer0 l0 = Layer0.getInstance(g);
59             Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
60             testRoot = g.newResource();
61             g.claim(testRoot, l0.InstanceOf, l0.Library);
62             g.claim(rl, l0.ConsistsOf, testRoot);
63         }
64     }
65
66     class Create extends WriteOnlyRequest {
67         @SuppressWarnings("unused")
68         @Override
69         public void perform(WriteOnlyGraph g) throws DatabaseException {
70             Layer0 l0 = Layer0.getInstance(getSession());
71             Resource root = g.newResource();
72             g.claim(testRoot, l0.ConsistsOf, l0.PartOf, root);
73             ManagementSupport ms = g.getService(ManagementSupport.class);
74             XSupport xs = g.getService(XSupport.class);
75             for (int i = 0, j = 1, k = 1; i < RESOURCE_COUNT; i++, j++, k++) {
76                 Resource item = g.newResource();
77                 g.claim(item, l0.InstanceOf, null, l0.String);
78                 g.claim(item, l0.IsWeaklyRelatedTo, null, l0.Abstract);
79                 g.claim(item, l0.IsWeaklyRelatedTo, null, l0.Assertion);
80                 g.claim(item, l0.IsWeaklyRelatedTo, null, l0.Graph);
81                 g.claim(item, l0.IsWeaklyRelatedTo, null, l0.Entity);
82                 g.claim(item, l0.IsWeaklyRelatedTo, null, l0.Relation);
83                 g.claimValue(item, UUID.randomUUID().toString());
84                 if (j == CONSIST_COUNT) {
85                     j = 0;
86                     root = g.newResource();
87                     g.claim(testRoot, l0.ConsistsOf, l0.PartOf, root);
88                 }
89                 assert (null != root);
90                 g.claim(root, l0.ConsistsOf, l0.PartOf, item);
91                 if (k == COMMIT_COUNT) {
92                     if (DEBUG && VERBOSE)
93                         System.out.println("resource count=" + (i + 1));
94                     k = 0;
95                     long beg = ms.getHeadRevisionId();
96                     xs.commitAndContinue(g, this);
97                     long end = ms.getHeadRevisionId();
98                     assertTrue(beg + 1 == end);
99                 }
100             }
101         }
102     }
103
104     class Query extends ReadRequest {
105         @SuppressWarnings("unused")
106         @Override
107         public void run(ReadGraph g) throws DatabaseException {
108             Layer0 l0 = Layer0.getInstance(g);
109             long count = 0;
110 //            ClusterControl cc = g.getService(ClusterControl.class);
111             for (Resource r : g.getObjects(testRoot, l0.ConsistsOf)) {
112                 for (Resource rr : g.getObjects(r, l0.ConsistsOf)) {
113                     if (!g.isInstanceOf(rr, l0.String))
114                         fail("Resource " + rr + " is not instance of String.");
115                     if (!g.isInstanceOf(rr, l0.Entity))
116                         fail("Resource " + rr + " is not instance of Entity.");
117                     ++count;
118                     if (DEBUG && VERBOSE && count % 100000 == 0)
119                         System.out.println("resource count=" + count);
120                 }
121                 // cc.gc(g);
122                 SessionGarbageCollection.gc(g, Integer.MAX_VALUE, 0);
123             }
124             assertTrue(count == RESOURCE_COUNT);
125         }
126     }
127 }