Skip to content

Commit

Permalink
updated jmh
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Oct 20, 2023
1 parent bf0872b commit 4176c4a
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 243 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ c.t.l.memtable.SkipListBenchmark.get thrpt 10 823423.128 ± 83028.354 ops/s
```

### Tree

```
...
```

---

## Implementation status
Expand All @@ -157,16 +163,15 @@ c.t.l.memtable.SkipListBenchmark.get thrpt 10 823423.128 ± 83028.354 ops/s
- [x] Bloom filter
- [x] Indexes persistence
- [x] File initialization
- [ ] Handle tombstones
- [ ] Skip-List
- [x] Skip-List
- [x] Operations
- [x] Iterator
- [ ] Tree
- [x] Tree
- [x] Operations
- [x] Background flush
- [ ] Background compaction
- [ ] Benchmarks
- [x] Background compaction
- [x] Benchmarks
- [x] SSTable
- [x] Bloom filter
- [x] Skip-List
- [ ] Tree
- [x] Tree
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ tasks.test {
}

jmh {
includes = ['LSMTreeBenchmark.*']
includes = ["LSMTreeAddBenchmark.*"]
fork = 1
warmupIterations = 0
iterations = 1
benchmarkMode = ['avgt']
benchmarkMode = ['thrpt']
jmhTimeout = '15s'
jmhVersion = '1.37'
resultFormat = 'JSON'
Expand Down
49 changes: 26 additions & 23 deletions src/jmh/java/com/tomfran/lsm/bloom/BloomFilterBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,43 @@

import static com.tomfran.lsm.TestUtils.getRandomByteArray;

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

BloomFilter bf;
@Benchmark
public void add(BloomState s) {
s.f.add(s.keys[s.index]);

byte[][] keys;
s.index = (s.index + 1) % BloomState.N;
}

int N = 1000000;
int index = 0;
@Benchmark
public void contains(BloomState s, Blackhole bh) {
bh.consume(s.f.mightContain(s.keys[s.index]));

@Setup
public void setup() {
s.index = (s.index + 1) % BloomState.N;
}

bf = new BloomFilter(N, 0.01);
@State(Scope.Thread)
public static class BloomState {

keys = new byte[N][];
static final int N = 1000000;

for (int i = 0; i < N; i++)
keys[i] = getRandomByteArray();
}
BloomFilter f;
byte[][] keys = new byte[N][];
int index;

@Benchmark
public void add() {
bf.add(keys[index]);

index = (index + 1) % N;
}
@Setup
public void setup() {
f = new BloomFilter(N);
index = 0;
for (int i = 0; i < N; i++)
keys[i] = getRandomByteArray();

@Benchmark
public void contains(Blackhole bh) {
bh.consume(bf.mightContain(keys[index]));
for (int i = 0; i < N / 2; i++)
f.add(keys[i]);
}

index = (index + 1) % N;
}

}
82 changes: 42 additions & 40 deletions src/jmh/java/com/tomfran/lsm/memtable/SkipListBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,65 @@

import static com.tomfran.lsm.TestUtils.getRandomPair;

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

SkipList l;
ByteArrayPair[] items;
@Benchmark
public void addRemove(ListState s, Blackhole bh) {
var key = s.items[s.index].key();

int NUM_ITEMS = 200000;
int index = 0;
if (s.addRemove[s.index]) {
s.l.add(s.items[s.index]);
} else {
s.l.remove(key);
}

boolean[] addRemove;
s.index = (s.index + 1) % ListState.N;
}

@Setup
public void setup() {
@Benchmark
public void get(ListState s, Blackhole bh) {
var key = s.items[s.index].key();
var found = s.l.get(key);

l = new SkipList(NUM_ITEMS / 2);
bh.consume(found);

// generate random items and insert half
ObjectArrayList<ByteArrayPair> tmp = new ObjectArrayList<>();
for (int i = 0; i < NUM_ITEMS; i++) {
var it = getRandomPair();
if (i < NUM_ITEMS / 2)
l.add(it);
s.index = (s.index + 1) % ListState.N;
}

tmp.add(it);
}
@State(Scope.Thread)
public static class ListState {

items = tmp.toArray(new ByteArrayPair[0]);
static final int N = 200000;

// generate sequence of add/remove operations
addRemove = new boolean[NUM_ITEMS];
for (int i = 0; i < NUM_ITEMS; i++) {
addRemove[i] = Math.random() < 0.5;
}
}
SkipList l;
ByteArrayPair[] items;
int index;
boolean[] addRemove;

@Benchmark
public void get(Blackhole bh) {
var key = items[index].key();
var found = l.get(key);
@Setup
public void setup() {
l = new SkipList(N / 2);

bh.consume(found);
ObjectArrayList<ByteArrayPair> tmp = new ObjectArrayList<>();
for (int i = 0; i < N; i++) {
var it = getRandomPair();
if (i < N / 2)
l.add(it);

index = (index + 1) % NUM_ITEMS;
}
tmp.add(it);
}

@Benchmark
public void addRemove(Blackhole bh) {
var key = items[index].key();
items = tmp.toArray(new ByteArrayPair[0]);

if (addRemove[index]) {
l.add(items[index]);
} else {
l.remove(key);
index = 0;

addRemove = new boolean[N];
for (int i = 0; i < N; i++) {
addRemove[i] = Math.random() < 0.5;
}
}

index = (index + 1) % NUM_ITEMS;
}

}
133 changes: 64 additions & 69 deletions src/jmh/java/com/tomfran/lsm/sstable/SSTableBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,97 +14,92 @@
import java.util.concurrent.TimeUnit;

import static com.tomfran.lsm.TestUtils.getRandomPair;
import static com.tomfran.lsm.utils.BenchmarkUtils.deleteDir;

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

static final Path DIR = Path.of("sst_benchmark");
static final int NUM_ITEMS = 100000;
static final int SAMPLE_SIZE = NUM_ITEMS / 1000;

static ByteArrayPair[] insertedArray;
static ByteArrayPair[] skippedArray;
static SSTable sstable;
@Benchmark
public void randomAccess(TableState s, Blackhole bh) {
var item = s.insertedArray[s.index];
var it = s.sstable.get(item.key());

static int index = 0;
bh.consume(it);

@Setup
public void setup() throws IOException {
// setup directory
if (Files.exists(DIR))
deleteDir();
s.index = (s.index + 1) % s.insertedArray.length;
}

Files.createDirectory(DIR);
@Benchmark
public void negativeAccess(TableState s, Blackhole bh) {
var item = s.skippedArray[s.index];
var it = s.sstable.get(item.key());

// generate random items
var l = new ObjectOpenHashSet<ByteArrayPair>();
for (int i = 0; i < NUM_ITEMS * 2; i++) {
l.add(getRandomPair());
}
bh.consume(it);

// sort and divide into inserted and skipped
var items = l.stream()
.sorted((a, b) -> ByteArrayComparator.compare(a.key(), b.key()))
.toList();
s.index = (s.index + 1) % s.skippedArray.length;
}

var inserted = new ObjectArrayList<ByteArrayPair>();
var skipped = new ObjectArrayList<ByteArrayPair>();
@State(Scope.Thread)
public static class TableState {

for (int i = 0; i < items.size(); i++) {
var e = items.get(i);
if (i % 2 == 0)
inserted.add(e);
else
skipped.add(e);
}
final int NUM_ITEMS = 100000;
final int SAMPLE_SIZE = 1000;

sstable = new SSTable(DIR + "/sst", inserted.iterator(), SAMPLE_SIZE);
ByteArrayPair[] insertedArray;
ByteArrayPair[] skippedArray;
SSTable sstable;
Path dir = Path.of("sst_benchmark_");

// shuffle to avoid sequential access
Collections.shuffle(inserted);
Collections.shuffle(skipped);
insertedArray = inserted.toArray(ByteArrayPair[]::new);
skippedArray = skipped.toArray(ByteArrayPair[]::new);
}
int index = 0;

@TearDown
public void teardown() throws IOException {
sstable.close();
deleteDir();
}
@Setup
public void setup() throws IOException {
// setup directory
dir = Path.of(dir.toString() + System.currentTimeMillis());

private void deleteDir() throws IOException {
try (var files = Files.list(DIR)) {
files.forEach(f -> {
try {
Files.delete(f);
} catch (IOException e) {
e.printStackTrace();
}
});
}
Files.delete(DIR);
}
if (Files.exists(dir))
deleteDir(dir);

@Benchmark
public void randomAccess(Blackhole bh) {
var item = insertedArray[index];
var it = sstable.get(item.key());
Files.createDirectory(dir);

bh.consume(it);
// generate random items
var l = new ObjectOpenHashSet<ByteArrayPair>();
for (int i = 0; i < NUM_ITEMS * 2; i++) {
l.add(getRandomPair());
}

index = (index + 1) % insertedArray.length;
}
// sort and divide into inserted and skipped
var items = l.stream()
.sorted((a, b) -> ByteArrayComparator.compare(a.key(), b.key()))
.toList();

@Benchmark
public void negativeAccess(Blackhole bh) {
var item = skippedArray[index];
var it = sstable.get(item.key());
var inserted = new ObjectArrayList<ByteArrayPair>();
var skipped = new ObjectArrayList<ByteArrayPair>();

bh.consume(it);
for (int i = 0; i < items.size(); i++) {
var e = items.get(i);
if (i % 2 == 0)
inserted.add(e);
else
skipped.add(e);
}

sstable = new SSTable(dir + "/sst", inserted.iterator(), SAMPLE_SIZE);

Collections.shuffle(inserted);
Collections.shuffle(skipped);
insertedArray = inserted.toArray(ByteArrayPair[]::new);
skippedArray = skipped.toArray(ByteArrayPair[]::new);
}

@TearDown
public void teardown() throws IOException {
sstable.close();
deleteDir(dir);
}

index = (index + 1) % skippedArray.length;
}

}
Loading

0 comments on commit 4176c4a

Please sign in to comment.