mirror of https://github.com/apache/lucene.git
Added teardown to properly cleanup files
This commit is contained in:
parent
04361d35cf
commit
f35605a6b1
|
@ -17,11 +17,8 @@
|
||||||
package org.apache.lucene.benchmark.jmh;
|
package org.apache.lucene.benchmark.jmh;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.FileVisitResult;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.SimpleFileVisitor;
|
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.store.IOContext;
|
import org.apache.lucene.store.IOContext;
|
||||||
|
@ -39,13 +36,14 @@ import org.openjdk.jmh.annotations.Param;
|
||||||
import org.openjdk.jmh.annotations.Scope;
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
import org.openjdk.jmh.annotations.Setup;
|
import org.openjdk.jmh.annotations.Setup;
|
||||||
import org.openjdk.jmh.annotations.State;
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
import org.openjdk.jmh.annotations.TearDown;
|
||||||
import org.openjdk.jmh.annotations.Warmup;
|
import org.openjdk.jmh.annotations.Warmup;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||||
@State(Scope.Benchmark)
|
@State(Scope.Benchmark)
|
||||||
@Warmup(iterations = 1, time = 5)
|
@Warmup(iterations = 1, time = 5)
|
||||||
@Measurement(iterations = 10, time = 8)
|
@Measurement(iterations = 5, time = 8)
|
||||||
@Fork(value = 1)
|
@Fork(value = 1)
|
||||||
public class DocIdEncodingBenchmark {
|
public class DocIdEncodingBenchmark {
|
||||||
|
|
||||||
|
@ -108,32 +106,30 @@ public class DocIdEncodingBenchmark {
|
||||||
|
|
||||||
private DocIdEncoder docIdEncoder;
|
private DocIdEncoder docIdEncoder;
|
||||||
|
|
||||||
private static final Path TMP_DIR;
|
private Path tmpDir;
|
||||||
|
|
||||||
static {
|
|
||||||
try {
|
|
||||||
TMP_DIR = Files.createTempDirectory("docIdJmh");
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final int[] scratch = new int[512];
|
private final int[] scratch = new int[512];
|
||||||
|
|
||||||
@Setup(Level.Iteration)
|
@Setup(Level.Trial)
|
||||||
public void init() throws IOException {
|
public void init() throws IOException {
|
||||||
|
tmpDir = Files.createTempDirectory("docIdJmh");
|
||||||
docIdEncoder = DocIdEncoder.Factory.fromName(encoderName);
|
docIdEncoder = DocIdEncoder.Factory.fromName(encoderName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TearDown
|
||||||
|
public void finish() throws IOException {
|
||||||
|
Files.delete(tmpDir);
|
||||||
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void performEncodeDecode() throws IOException {
|
public void performEncodeDecode() throws IOException {
|
||||||
try (Directory dir = new NIOFSDirectory(TMP_DIR)) {
|
String dataFile =
|
||||||
String dataFile =
|
String.join(
|
||||||
String.join(
|
"_",
|
||||||
"_",
|
"docIdJmhData_",
|
||||||
"docIdJmhData_",
|
docIdEncoder.getClass().getSimpleName(),
|
||||||
docIdEncoder.getClass().getSimpleName(),
|
String.valueOf(System.nanoTime()));
|
||||||
String.valueOf(System.nanoTime()));
|
try (Directory dir = new NIOFSDirectory(tmpDir)) {
|
||||||
try (IndexOutput out = dir.createOutput(dataFile, IOContext.DEFAULT)) {
|
try (IndexOutput out = dir.createOutput(dataFile, IOContext.DEFAULT)) {
|
||||||
for (int i = 1; i <= INPUT_SCALE_FACTOR; i++) {
|
for (int i = 1; i <= INPUT_SCALE_FACTOR; i++) {
|
||||||
docIdEncoder.encode(out, 0, DOC_IDS.length, DOC_IDS);
|
docIdEncoder.encode(out, 0, DOC_IDS.length, DOC_IDS);
|
||||||
|
@ -145,23 +141,7 @@ public class DocIdEncodingBenchmark {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
Files.walkFileTree(
|
Files.delete(tmpDir.resolve(dataFile));
|
||||||
TMP_DIR,
|
|
||||||
new SimpleFileVisitor<>() {
|
|
||||||
@Override
|
|
||||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
|
|
||||||
throws IOException {
|
|
||||||
Files.delete(file);
|
|
||||||
return FileVisitResult.CONTINUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
|
|
||||||
throws IOException {
|
|
||||||
Files.delete(dir);
|
|
||||||
return FileVisitResult.CONTINUE;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue