+/*******************************************************************************
+ * 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.performance.read;
+
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.TransactionPolicyKeep;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
+import org.simantics.db.request.WriteOnlyResult;
+import org.simantics.db.service.QueryControl;
+import org.simantics.db.service.TransactionPolicySupport;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.layer0.Layer0;
+
+/*
+ *
+ * Some statistics:
+ *
+ * == With WriteOnlyGraph ===
+ *
+ * -Creation of 50M write only resources (50M res, 0 stm)
+ * -Stream off = 1.88s
+ * -UpdateTransaction off = 5.11s
+ * -Creation of 50M write only resources with single instanceof (50M res, 50M stm)
+ * -Stream off = 5.36s
+ * -UpdateTransaction off = 16.05s
+ * -Creation of 50M write only resources with instanceof and consists of hierarchy (50M res, 150M stm)
+ * -Stream off = 12.20s
+ * -UpdateTransaction off = 39.3s
+ * -Creation of 50M write only resources with string name (no instanceof) (100M res, 150M stm)
+ * -Stream off = 31.66s
+ * -Creation of 50M write only resources full (100M res, 300M stm)
+ * -Stream off = 43.39s
+ *
+ * == With ClusterBuilder ==
+ *
+ * -Creation of 50M write only resources (50M res, 0 stm)
+ * -Stream off = 1.62s
+ * -UpdateTransaction off = 5.07
+ * -Creation of 50M write only resources with single instanceof (50M res, 50M stm)
+ * -Stream off = 3.33s
+ * -UpdateTransaction off = 14.26s
+ * -Creation of 50M write only resources with instanceof and consists of hierarchy (50M res, 150M stm)
+ * -Stream off = 6.09s
+ * -UpdateTransaction off = 32.42s
+ * -Creation of 50M write only resources with string name (no instanceof) (100M res, 150M stm)
+ * -Stream off = 23.97s
+ * -Creation of 50M write only resources full (100M res, 300M stm, 50M literals)
+ * -Stream off = 27.85s
+ *
+ */
+public class ReadHierarchicalNamesAndTypes extends ExistingDatabaseTest {
+
+ final private static String name = "name";
+ int[] sizes = { 244, 64, 64 };
+
+ private Resource time(String label, WriteOnlyResult<Resource> request) throws DatabaseException {
+
+ long start = System.nanoTime();
+ Resource result = getSession().syncRequest(request);
+ long duration = System.nanoTime() - start;
+ System.out.println(label + " = " + 1e-9*duration);
+ return result;
+
+ }
+
+ private void time(String label, Read<Object> request) throws DatabaseException {
+
+ long start = System.nanoTime();
+ getSession().syncRequest(request);
+ long duration = System.nanoTime() - start;
+ System.out.println(label + " = " + 1e-9*duration);
+
+ }
+
+// private void clearCaches() throws DatabaseException {
+//
+// Session session = Tests.getTestHandler().getSession();
+// QueryControl cc = session.getService(QueryControl.class);
+// ClusterControl ccr = session.getService(ClusterControl.class);
+// cc.flush();
+// ccr.collectClusters(ccr.used());
+// System.gc();
+// System.gc();
+// System.gc();
+//
+// }
+
+ public void test() throws Exception {
+
+ Layer0.getInstance(getSession());
+
+ getSession().registerService(TransactionPolicySupport.class, new TransactionPolicyKeep());
+// getSession().registerService(TransactionPolicySupport.class, new TransactionPolicyRelease());
+
+ // First build the test data
+ Resource root = time("write", HierarchicalNames.writeOnly(name, sizes));
+
+ // Read everything after write
+ time("response", HierarchicalNames.readAsyncTypes(root));
+
+ Session session = getSession();
+ QueryControl cc = session.getService(QueryControl.class);
+ cc.flush();
+
+ System.out.println("wait");
+ Thread.sleep(3000);
+ System.out.println("wait done");
+
+ // Clusters are now loaded
+ time("async", HierarchicalNames.readAsyncTypes(root));
+// System.err.println("done");
+// time("async2", HierarchicalNames.readAsync2(root));
+// time("sync", HierarchicalNames.readSync(root));
+// time("sync2", HierarchicalNames.readSync2(root));
+
+ }
+
+}