Skip to content

Apache Lucene open-source search software

LUCENE-4039: add AddIndexesTask to benchmark

Shai Erera4f38a9d42e8a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 8d5fabc1e1c..686e72462f4 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -853,6 +853,9 @@ New features
* LUCENE-4025: Add maybeRefreshBlocking to ReferenceManager, to let a caller
block until the refresh logic has been executed. (Shai Erera, Mike McCandless)
+
+* LUCENE-4039: Add AddIndexesTask to benchmark, which uses IW.addIndexes.
+ (Shai Erera)
Optimizations
diff --git a/lucene/benchmark/conf/addIndexes.alg b/lucene/benchmark/conf/addIndexes.alg
new file mode 100755
index 00000000000..0aa4bfdc35b
--- /dev/null
+++ b/lucene/benchmark/conf/addIndexes.alg
@@ -0,0 +1,45 @@
+#/**
+# * Licensed to the Apache Software Foundation (ASF) under one or more
+# * contributor license agreements. See the NOTICE file distributed with
+# * this work for additional information regarding copyright ownership.
+# * The ASF licenses this file to You 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.
+# */
+
+writer.version=LUCENE_40
+
+analyzer=org.apache.lucene.analysis.standard.StandardAnalyzer
+directory=FSDirectory
+work.dir=output
+
+# directory to add to the target index
+addindexes.input.dir=input/index
+
+# task at this depth or less would print when they start
+task.max.depth.log=2
+
+# -------------------------------------------------------------------------------------
+
+# call addIndexes (Directory)
+ResetSystemErase
+CreateIndex
+{ "AddIndexesDirectory" AddIndexes(true) >
+CloseIndex
+
+# call addIndexes (IndexReader)
+ResetSystemErase
+CreateIndex
+{ "AddIndexesReader" AddIndexes(false) >
+CloseIndex
+
+RepSumByName
+RepSumByPref AddIndexes
\ No newline at end of file
diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTask.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTask.java
new file mode 100755
index 00000000000..72db9aa5f5a
--- /dev/null
+++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTask.java
@@ -0,0 +1,102 @@
+package org.apache.lucene.benchmark.byTask.tasks;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+import java.io.File;
+
+import org.apache.lucene.benchmark.byTask.PerfRunData;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+
+/**
+ * Adds an input index to an existing index, using
+ * {@link IndexWriter#addIndexes(Directory...)} or
+ * {@link IndexWriter#addIndexes(IndexReader...)}. The location of the input
+ * index is specified by the parameter {@link #ADDINDEXES_INPUT_DIR} and is
+ * assumed to be a directory on the file system.
+ * <p>
+ * Takes optional parameter {@code useAddIndexesDir} which specifies which
+ * addIndexes variant to use (defaults to true, to use addIndexes(Directory)).
+ */
+public class AddIndexesTask extends PerfTask {
+
+ public static final String ADDINDEXES_INPUT_DIR = "addindexes.input.dir";
+
+ public AddIndexesTask(PerfRunData runData) {
+ super(runData);
+ }
+
+ private boolean useAddIndexesDir = true;
+ private FSDirectory inputDir;
+
+ @Override
+ public void setup() throws Exception {
+ super.setup();
+ String inputDirProp = getRunData().getConfig().get(ADDINDEXES_INPUT_DIR, null);
+ if (inputDirProp == null) {
+ throw new IllegalArgumentException("config parameter " + ADDINDEXES_INPUT_DIR + " not specified in configuration");
+ }
+ inputDir = FSDirectory.open(new File(inputDirProp));
+ }
+
+ @Override
+ public int doLogic() throws Exception {
+ IndexWriter writer = getRunData().getIndexWriter();
+ if (useAddIndexesDir) {
+ writer.addIndexes(inputDir);
+ } else {
+ IndexReader r = DirectoryReader.open(inputDir);
+ try {
+ writer.addIndexes(r);
+ } finally {
+ r.close();
+ }
+ }
+ return 1;
+ }
+
+ /**
+ * Set the params (useAddIndexesDir only)
+ *
+ * @param params
+ * {@code useAddIndexesDir=true} for using
+ * {@link IndexWriter#addIndexes(Directory...)} or {@code false} for
+ * using {@link IndexWriter#addIndexes(IndexReader...)}. Defaults to
+ * {@code true}.
+ */
+ @Override
+ public void setParams(String params) {
+ super.setParams(params);
+ useAddIndexesDir = Boolean.parseBoolean(params);
+ }
+
+ @Override
+ public boolean supportsParams() {
+ return true;
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ inputDir.close();
+ super.tearDown();
+ }
+
+}
diff --git a/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTaskTest.java b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTaskTest.java
new file mode 100755
index 00000000000..17688bcf2a5
--- /dev/null
+++ b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTaskTest.java
@@ -0,0 +1,140 @@
+package org.apache.lucene.benchmark.byTask.tasks;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+import java.io.File;
+import java.util.Properties;
+
+import org.apache.lucene.benchmark.BenchmarkTestCase;
+import org.apache.lucene.benchmark.byTask.PerfRunData;
+import org.apache.lucene.benchmark.byTask.utils.Config;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.MockDirectoryWrapper;
+import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.util._TestUtil;
+import org.junit.BeforeClass;
+
+/** Tests the functionality of {@link AddIndexesTask}. */
+public class AddIndexesTaskTest extends BenchmarkTestCase {
+
+ private static File testDir, inputDir;
+
+ @BeforeClass
+ public static void beforeClassAddIndexesTaskTest() throws Exception {
+ testDir = _TestUtil.getTempDir("addIndexesTask");
+
+ // create a dummy index under inputDir
+ inputDir = new File(testDir, "input");
+ MockDirectoryWrapper tmpDir = newFSDirectory(inputDir);
+ try {
+ IndexWriter writer = new IndexWriter(tmpDir, new IndexWriterConfig(TEST_VERSION_CURRENT, null));
+ for (int i = 0; i < 10; i++) {
+ writer.addDocument(new Document());
+ }
+ writer.close();
+ } finally {
+ tmpDir.close();
+ }
+ }
+
+ private PerfRunData createPerfRunData() throws Exception {
+ Properties props = new Properties();
+ props.setProperty("writer.version", TEST_VERSION_CURRENT.toString());
+ props.setProperty("print.props", "false"); // don't print anything
+ props.setProperty("directory", "RAMDirectory");
+ props.setProperty(AddIndexesTask.ADDINDEXES_INPUT_DIR, inputDir.getAbsolutePath());
+ Config config = new Config(props);
+ return new PerfRunData(config);
+ }
+
+ private void assertIndex(PerfRunData runData) throws Exception {
+ Directory taskDir = runData.getDirectory();
+ assertSame(RAMDirectory.class, taskDir.getClass());
+ IndexReader r = DirectoryReader.open(taskDir);
+ try {
+ assertEquals(10, r.numDocs());
+ } finally {
+ r.close();
+ }
+ }
+
+ public void testAddIndexesDefault() throws Exception {
+ PerfRunData runData = createPerfRunData();
+ // create the target index first
+ new CreateIndexTask(runData).doLogic();
+
+ AddIndexesTask task = new AddIndexesTask(runData);
+ task.setup();
+
+ // add the input index
+ task.doLogic();
+
+ // close the index
+ new CloseIndexTask(runData).doLogic();
+
+ assertIndex(runData);
+
+ runData.close();
+ }
+
+ public void testAddIndexesDir() throws Exception {
+ PerfRunData runData = createPerfRunData();
+ // create the target index first
+ new CreateIndexTask(runData).doLogic();
+
+ AddIndexesTask task = new AddIndexesTask(runData);
+ task.setup();
+
+ // add the input index
+ task.setParams("true");
+ task.doLogic();
+
+ // close the index
+ new CloseIndexTask(runData).doLogic();
+
+ assertIndex(runData);
+
+ runData.close();
+ }
+
+ public void testAddIndexesReader() throws Exception {
+ PerfRunData runData = createPerfRunData();
+ // create the target index first
+ new CreateIndexTask(runData).doLogic();
+
+ AddIndexesTask task = new AddIndexesTask(runData);
+ task.setup();
+
+ // add the input index
+ task.setParams("false");
+ task.doLogic();
+
+ // close the index
+ new CloseIndexTask(runData).doLogic();
+
+ assertIndex(runData);
+
+ runData.close();
+ }
+
+}