]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/scalability/LargeDataTest.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/scalability/LargeDataTest.java
new file mode 100644 (file)
index 0000000..98a94ca
--- /dev/null
@@ -0,0 +1,106 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.scalability;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.db.testing.common.WriteQuery;
+import org.simantics.layer0.Layer0;
+
+/**
+ * Creates large amount of instances in multiple transactions
+ * and the tries to verify that instances are written into the database.
+ * 
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ *
+ */
+public class LargeDataTest extends ExistingDatabaseTest {
+       
+       private static int INSTANCE_COUNT = 1000; //100000
+       private static int MAX_INSTANCE_COUNT_PER_TRANSACTION = 100; // 1000;
+       private static boolean OPTIMIZED = false;
+       
+       public void testLargeData() throws Exception{
+               final Resource rootLib = getSession().getRootLibrary();
+           final String random = getRandomString();
+               if (DEBUG)
+                   System.out.println("Test start");
+               for (int j = 0; j < INSTANCE_COUNT; j += MAX_INSTANCE_COUNT_PER_TRANSACTION) {
+                       final int base = j;
+                       getSession().syncRequest(new WriteQuery(this) {
+                               @Override
+                               public void run(WriteGraph g) throws Throwable {
+                                       Layer0 b = Layer0.getInstance(g);
+                                       for (int i = 0; i < MAX_INSTANCE_COUNT_PER_TRANSACTION; i++) {
+                                               String newInstanceName = random + (base+i);
+                                               Resource instance = g.newResource();
+                                               if(!OPTIMIZED) {
+                                                       g.claim(instance, b.InstanceOf, null, b.Type);
+                                                       g.claimLiteral(instance, b.HasName, newInstanceName);
+                                                       g.claim(rootLib, b.ConsistsOf, instance);
+                                               } else {
+                                                       Resource invName = g.getInverse(b.HasName);
+                                                       g.claim(instance, b.HasName, invName, b.Type);
+                                                       g.claimLiteral(instance, b.HasName, newInstanceName);
+                                                       g.claim(rootLib, b.ConsistsOf, b.PartOf, instance);
+                                               }
+                                       }
+
+                               }
+                       });
+                       if (DEBUG)
+                           System.out.println(j + MAX_INSTANCE_COUNT_PER_TRANSACTION);
+               }
+               
+               checkException();
+               if (DEBUG)
+                   System.out.println("Reading stage");
+               getSession().syncRequest(new TestReadRequest() {
+                       @Override
+                       public void run(ReadGraph g) throws Throwable {
+                               Layer0 b = Layer0.getInstance(g);
+                               List<Boolean> found = new ArrayList<Boolean>(INSTANCE_COUNT);
+                               for (int i = 0; i < INSTANCE_COUNT; i++) {
+                                       found.add(false);
+                               }
+                               Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
+                               for (Resource r : resources) {
+                                       String name = g.getPossibleRelatedValue(r, b.HasName);
+                    if (null == name || !name.startsWith(random, 0))
+                        continue;
+                    name = name.substring(random.length());
+                    try {
+                        int i = Integer.parseInt(name);
+                                               found.set(i, true);
+
+                    } catch (NumberFormatException e) {
+                        continue; // not a number
+                    }
+                               }
+                               for (int i = 0; i < INSTANCE_COUNT; i++) {
+                                       if (!found.get(i))
+                                               throw new Exception("Created resource " + i + " cannot be found");
+                               }
+                       }
+               });
+               if (DEBUG)
+                   System.out.println("test done");
+               checkException();
+       }
+
+}