expani commented on code in PR #13521:
URL: https://github.com/apache/lucene/pull/13521#discussion_r1801053720


##########
lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/DocIdEncodingBenchmark.java:
##########
@@ -0,0 +1,404 @@
+/*
+ * 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.
+ */
+package org.apache.lucene.benchmark.jmh;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Scanner;
+import java.util.concurrent.TimeUnit;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.IOContext;
+import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.IndexOutput;
+import org.apache.lucene.store.NIOFSDirectory;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Warmup;
+
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@State(Scope.Benchmark)
+@Warmup(iterations = 3, time = 5)
+@Measurement(iterations = 5, time = 8)
+@Fork(value = 1)
+public class DocIdEncodingBenchmark {
+
+  private static final List<int[]> DOC_ID_SEQUENCES = new ArrayList<>();
+
+  private static int INPUT_SCALE_FACTOR;
+
+  static {
+    parseInput();
+  }
+
+  @Param({"Bit21With3StepsEncoder", "Bit21With2StepsEncoder", "Bit24Encoder", 
"Bit32Encoder"})
+  String encoderName;
+
+  @Param({"encode", "decode"})
+  String methodName;
+
+  private DocIdEncoder docIdEncoder;
+
+  private Path tmpDir;
+
+  private IndexInput in;
+
+  private IndexOutput out;
+
+  private final int[] scratch = new int[512];
+
+  @Setup(Level.Trial)
+  public void init() throws IOException {
+    tmpDir = Files.createTempDirectory("docIdJmh");
+    docIdEncoder = DocIdEncoder.SingletonFactory.fromName(encoderName);
+    // Create file once for decoders to read from in every iteration
+    if (methodName.equalsIgnoreCase("decode")) {
+      String dataFile =
+          String.join("_", "docIdJmhData", 
docIdEncoder.getClass().getSimpleName(), "DecoderInput");
+      try (Directory dir = new NIOFSDirectory(tmpDir)) {
+        out = dir.createOutput(dataFile, IOContext.DEFAULT);
+        encode();
+      } finally {
+        out.close();
+      }
+    }
+  }
+
+  @TearDown(Level.Trial)
+  public void finish() throws IOException {
+    if (methodName.equalsIgnoreCase("decode")) {
+      String dataFile =
+          String.join("_", "docIdJmhData", 
docIdEncoder.getClass().getSimpleName(), "DecoderInput");
+      Files.delete(tmpDir.resolve(dataFile));
+    }
+    Files.delete(tmpDir);
+  }
+
+  @Benchmark
+  public void executeEncodeOrDecode() throws IOException {
+    if (methodName.equalsIgnoreCase("encode")) {
+      String dataFile =
+          String.join(
+              "_",
+              "docIdJmhData",
+              docIdEncoder.getClass().getSimpleName(),
+              String.valueOf(System.nanoTime()));
+      try (Directory dir = new NIOFSDirectory(tmpDir)) {
+        out = dir.createOutput(dataFile, IOContext.DEFAULT);
+        encode();
+      } finally {
+        Files.delete(tmpDir.resolve(dataFile));
+        out.close();
+      }
+    } else if (methodName.equalsIgnoreCase("decode")) {
+      String inputFile =
+          String.join("_", "docIdJmhData", 
docIdEncoder.getClass().getSimpleName(), "DecoderInput");
+      try (Directory dir = new NIOFSDirectory(tmpDir)) {
+        in = dir.openInput(inputFile, IOContext.DEFAULT);
+        decode();
+      } finally {
+        in.close();
+      }
+    } else {
+      throw new IllegalArgumentException("Unknown method: " + methodName);
+    }
+  }
+
+  public void encode() throws IOException {
+    for (int[] docIdSequence : DOC_ID_SEQUENCES) {
+      for (int i = 1; i <= INPUT_SCALE_FACTOR; i++) {
+        docIdEncoder.encode(out, 0, docIdSequence.length, docIdSequence);
+      }
+    }
+  }
+
+  public void decode() throws IOException {
+    for (int[] docIdSequence : DOC_ID_SEQUENCES) {
+      for (int i = 1; i <= INPUT_SCALE_FACTOR; i++) {
+        docIdEncoder.decode(in, 0, docIdSequence.length, scratch);
+        // Uncomment to test the output of Encoder

Review Comment:
   Wrote a UT `TestDocIdEncoding` for covering this scenario 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to