Skip to content

Commit

Permalink
fixed benchmarks not erasing disk
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Nov 30, 2023
1 parent 8f38c09 commit fd0d85b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
11 changes: 4 additions & 7 deletions src/jmh/java/com/tomfran/lsm/tree/LSMTreeAddBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import com.tomfran.lsm.utils.BenchmarkUtils;
import org.openjdk.jmh.annotations.*;

import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Benchmark)
public class LSMTreeAddBenchmark {

static final Path DIR = Path.of("tree_benchmark");
static final Path DIR = Path.of("tree_add_benchmark");
static final int NUM_ITEMS = 1000000;
static final int MEMTABLE_SIZE = 1 << 18;
static final int LEVEL_SIZE = 5;
Expand All @@ -22,16 +21,14 @@ public class LSMTreeAddBenchmark {
ByteArrayPair[] items;

@Setup
public void setup() throws IOException {
public void setup() {
tree = BenchmarkUtils.initTree(DIR, MEMTABLE_SIZE, LEVEL_SIZE);
items = BenchmarkUtils.fillItems(NUM_ITEMS);
}

@TearDown
public void teardown() throws InterruptedException {
tree.stop();
Thread.sleep(5000);
BenchmarkUtils.deleteDir(DIR);
public void teardown() {
BenchmarkUtils.stopTreeAndCleanDisk(tree, DIR);
}

@Benchmark
Expand Down
11 changes: 4 additions & 7 deletions src/jmh/java/com/tomfran/lsm/tree/LSMTreeGetBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

Expand All @@ -15,7 +14,7 @@
@State(Scope.Benchmark)
public class LSMTreeGetBenchmark {

static final Path DIR = Path.of("tree_benchmark");
static final Path DIR = Path.of("tree_get_benchmark");
static final int NUM_ITEMS = 1000000;
static final int MEMTABLE_SIZE = 1 << 18;
static final int LEVEL_SIZE = 5;
Expand All @@ -25,7 +24,7 @@ public class LSMTreeGetBenchmark {
ByteArrayPair[] items;

@Setup
public void setup() throws IOException {
public void setup() {
tree = BenchmarkUtils.initTree(DIR, MEMTABLE_SIZE, LEVEL_SIZE);
items = BenchmarkUtils.fillItems(NUM_ITEMS);
for (var i : items)
Expand All @@ -35,10 +34,8 @@ public void setup() throws IOException {
}

@TearDown
public void teardown() throws InterruptedException {
tree.stop();
Thread.sleep(5000);
BenchmarkUtils.deleteDir(DIR);
public void teardown() {
BenchmarkUtils.stopTreeAndCleanDisk(tree, DIR);
}

@Benchmark
Expand Down
19 changes: 15 additions & 4 deletions src/jmh/java/com/tomfran/lsm/utils/BenchmarkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@
public class BenchmarkUtils {

public static LSMTree initTree(Path dir, int memSize, int levelSize) {
// setup directory
if (Files.exists(dir))
deleteDir(dir);

// setup tree
return new LSMTree(memSize, levelSize, dir.toString());
}

public static void stopTreeAndCleanDisk(LSMTree tree, Path dir) {
try {
tree.stop();
Thread.sleep(5000);
} catch (Exception ignored) {
}

deleteDir(dir);
}

public static ByteArrayPair[] fillItems(int n) {
ByteArrayPair[] items = new ByteArrayPair[n];
for (int i = 0; i < n; i++)
Expand All @@ -39,8 +47,11 @@ public static void shuffleItems(ByteArrayPair[] v) {
}

public static void deleteDir(Path dir) {
try (var f = Files.walk(dir)) {
f.map(Path::toFile).forEach(File::delete);
try {
try (var f = Files.walk(dir)) {
f.map(Path::toFile).forEach(File::delete);
}
Files.delete(dir);
} catch (Exception ignored) {
}
}
Expand Down

0 comments on commit fd0d85b

Please sign in to comment.