From 9bda0e4f8af8a8e5c9e52ba621e6c20edf26c5f7 Mon Sep 17 00:00:00 2001
From: jakubs <jakubs>
Date: Thu, 27 Sep 2012 13:24:21 +0000
Subject: [PATCH] BIS-201 create simple sample registration benchmark testcase

SVN: 26838
---
 .../sample-benchmark/plugin.properties        |  6 ++
 .../sample-benchmark-data-set-handler.py      | 69 ++++++++++++++
 .../SampleBatchImportBenchmarkSystemTest.java | 94 +++++++++++++++++++
 3 files changed, 169 insertions(+)
 create mode 100644 datastore_server/sourceTest/core-plugins/generic-test/1/dss/drop-boxes/sample-benchmark/plugin.properties
 create mode 100644 datastore_server/sourceTest/core-plugins/generic-test/1/dss/drop-boxes/sample-benchmark/sample-benchmark-data-set-handler.py
 create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/SampleBatchImportBenchmarkSystemTest.java

diff --git a/datastore_server/sourceTest/core-plugins/generic-test/1/dss/drop-boxes/sample-benchmark/plugin.properties b/datastore_server/sourceTest/core-plugins/generic-test/1/dss/drop-boxes/sample-benchmark/plugin.properties
new file mode 100644
index 00000000000..c8294e2b07b
--- /dev/null
+++ b/datastore_server/sourceTest/core-plugins/generic-test/1/dss/drop-boxes/sample-benchmark/plugin.properties
@@ -0,0 +1,6 @@
+incoming-dir = ${root-dir}/incoming-sample-benchmark
+incoming-dir-create = true
+incoming-data-completeness-condition = auto-detection
+top-level-data-set-handler = ch.systemsx.cisd.etlserver.registrator.api.v2.JythonTopLevelDataSetHandlerV2
+script-path = sample-benchmark-data-set-handler.py
+storage-processor = ch.systemsx.cisd.etlserver.DefaultStorageProcessor
diff --git a/datastore_server/sourceTest/core-plugins/generic-test/1/dss/drop-boxes/sample-benchmark/sample-benchmark-data-set-handler.py b/datastore_server/sourceTest/core-plugins/generic-test/1/dss/drop-boxes/sample-benchmark/sample-benchmark-data-set-handler.py
new file mode 100644
index 00000000000..16c2a636422
--- /dev/null
+++ b/datastore_server/sourceTest/core-plugins/generic-test/1/dss/drop-boxes/sample-benchmark/sample-benchmark-data-set-handler.py
@@ -0,0 +1,69 @@
+from time import time
+from ch.systemsx.cisd.common.mail import EMailAddress
+
+# modify those values to test the performance. 
+# sample type can be important, as it can contain validations for instance.
+# The values present here are not too high, so that the test runs quickly in normal circumstances.
+
+SAMPLES_COUNT = 100
+SAMPLE_TYPE = 'WELL'
+
+SPACE_CODE = "SAMPLE_BENCHMARK_SPACE"
+PROJECT_ID = "/SAMPLE_BENCHMARK_SPACE/SAMPLE_BENCHMARK_PROJECT"
+EXPERIMENT_ID = "/SAMPLE_BENCHMARK_SPACE/SAMPLE_BENCHMARK_PROJECT/SAMPLE_BENCHMARK_EXPERIMENT"
+
+# the hooks
+
+def sendMail(context, subject, message):
+    mailClient = context.getGlobalState().getMailClient();
+    addressFrom = EMailAddress("example@example.com")
+    addressTo = EMailAddress("example@example.com", "example name")
+    mailClient.sendEmailMessage(subject, message, None,
+            addressFrom, addressTo) 
+
+def pre_metadata_registration(context):
+    context.getPersistentMap().put("pre_registration", time())
+
+def post_metadata_registration(context):
+    start = context.getPersistentMap().get("start")
+    pre = context.getPersistentMap().get("pre_registration")
+    post = time()
+    
+    contentPattern = "sample_benchmark_test\nTime in seconds\n  jython script:   %s\n  as registration: %s\n  total            %s"
+    content =  contentPattern % (str(pre-start), str(post-pre), str(post - start))
+    sendMail(context, "Subject", content)
+
+def create_space_if_needed(transaction):
+    space = transaction.getSpace(SPACE_CODE)
+    if None == space:
+        space = transaction.createNewSpace(SPACE_CODE, None)
+        space.setDescription("A demo space")
+
+def create_project_if_needed(transaction):
+    project = transaction.getProject(PROJECT_ID)
+    if None == project:
+        create_space_if_needed(transaction)
+        project = transaction.createNewProject(PROJECT_ID)
+        project.setDescription("A demo project")
+
+def create_experiment_if_needed(transaction):
+    exp = transaction.getExperiment(EXPERIMENT_ID)
+    if None == exp:
+        create_project_if_needed(transaction)
+        exp = transaction.createNewExperiment(EXPERIMENT_ID, 'SIRNA_HCS')
+        exp.setPropertyValue("DESCRIPTION", "A sample experiment")
+
+    return exp
+
+def createSamples(transaction):
+    for i in range(SAMPLES_COUNT):
+        sample = transaction.createNewSample('/'+SPACE_CODE+'/SAMPLE'+str(i), SAMPLE_TYPE)
+
+def process(transaction):
+    transaction.getRegistrationContext().getPersistentMap().put("start", time())
+    
+    # create experiment
+    experiment = create_experiment_if_needed(transaction)
+    
+    # register samples
+    createSamples(transaction)
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/SampleBatchImportBenchmarkSystemTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/SampleBatchImportBenchmarkSystemTest.java
new file mode 100644
index 00000000000..8bbfcd37212
--- /dev/null
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/SampleBatchImportBenchmarkSystemTest.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2012 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.datastoreserver.systemtests;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.common.filesystem.FileUtilities;
+
+/**
+ * This test tests little functionality, but focuses on benchmarking the sample registration process
+ * during development. Run this test on "before" and "after" versions of your code to measure the
+ * performance changes.
+ * <p>
+ * It starts a dropbox that does nothing, but registers the given big number of samples with certain
+ * type. Edit the constants in the dropbox script to change the number, and the type. The default
+ * values are small, to avoid having a long-running test, that doesn't do much. You might also want
+ * to update the timeout value in method <code>dataSetImportWaitDurationInSeconds()</code> if your
+ * benchmark takes too much time.
+ * <p>
+ * After the test completes it prints to the console how much time took the jython script part and
+ * the registration in application server.
+ * 
+ * @author Jakub Straszewski
+ */
+public class SampleBatchImportBenchmarkSystemTest extends SystemTestCase
+{
+    // for jython script go to
+    // sourceTest/core-plugins/generic-test/1/dss/drop-boxes/sample-benchmark/sample-benchmark-data-set-handler.py
+
+    @Override
+    protected File getIncomingDirectory()
+    {
+        return new File(rootDir, "incoming-sample-benchmark");
+    }
+
+    @Override
+    protected int dataSetImportWaitDurationInSeconds()
+    {
+        return 60;
+    }
+
+    @Test
+    public void testSampleBenchmark() throws Exception
+    {
+        dropSomeFileInADropbox();
+
+        waitUntilDataSetImported();
+
+        assertEmailHasBeenSentFromHook();
+    }
+
+    private void assertEmailHasBeenSentFromHook()
+    {
+        File emailDirectory = new File(new File(workingDirectory, "dss-root"), "email");
+
+        for (File f : FileUtilities.listFiles(emailDirectory))
+        {
+            String content = FileUtilities.loadExactToString(f);
+            if (content.contains("sample_benchmark_test"))
+            {
+                System.out.println(content);
+                // with the persistent map
+                return; // assert ok
+            }
+        }
+        fail("No email found!");
+    }
+
+    private void dropSomeFileInADropbox() throws IOException
+    {
+        File exampleDataSet = new File(workingDirectory, "my-data");
+        exampleDataSet.mkdirs();
+        FileUtilities.writeToFile(new File(exampleDataSet, "set1.txt"), "hello world");
+        moveFileToIncoming(exampleDataSet);
+    }
+
+}
-- 
GitLab