-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip list impl test and benchmark, bloom benchmark
- Loading branch information
Showing
14 changed files
with
397 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/jmh/java/com/tomfran/lsm/bloom/BloomFilterBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.tomfran.lsm.bloom; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.tomfran.lsm.TestUtils.getRandomByteArray; | ||
|
||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
@State(Scope.Benchmark) | ||
public class BloomFilterBenchmark { | ||
|
||
BloomFilter bf; | ||
|
||
byte[][] keys; | ||
|
||
int N = 1000000; | ||
int index = 0; | ||
|
||
@Setup | ||
public void setup() { | ||
|
||
bf = new BloomFilter(N, 0.01); | ||
|
||
keys = new byte[N][]; | ||
|
||
for (int i = 0; i < N; i++) | ||
keys[i] = getRandomByteArray(); | ||
} | ||
|
||
@Benchmark | ||
public void add() { | ||
bf.add(keys[index]); | ||
|
||
index = (index + 1) % N; | ||
} | ||
|
||
@Benchmark | ||
public void contains(Blackhole bh) { | ||
bh.consume(bf.mightContain(keys[index])); | ||
|
||
index = (index + 1) % N; | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/jmh/java/com/tomfran/lsm/memtable/SkipListBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.tomfran.lsm.memtable; | ||
|
||
import com.tomfran.lsm.types.Item; | ||
import it.unimi.dsi.fastutil.objects.ObjectArrayList; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.tomfran.lsm.TestUtils.getRandomItem; | ||
|
||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
@State(Scope.Benchmark) | ||
public class SkipListBenchmark { | ||
|
||
SkipList l; | ||
Item[] items; | ||
|
||
int NUM_ITEMS = 200000; | ||
int index = 0; | ||
|
||
boolean[] addRemove; | ||
|
||
@Setup | ||
public void setup() { | ||
|
||
l = new SkipList(NUM_ITEMS / 2); | ||
|
||
// generate random items and insert half | ||
ObjectArrayList<Item> tmp = new ObjectArrayList<>(); | ||
for (int i = 0; i < NUM_ITEMS; i++) { | ||
var it = getRandomItem(); | ||
if (i < NUM_ITEMS / 2) | ||
l.add(it); | ||
|
||
tmp.add(it); | ||
} | ||
|
||
items = tmp.toArray(new Item[0]); | ||
|
||
// 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; | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void get(Blackhole bh) { | ||
var key = items[index].key(); | ||
var found = l.get(key); | ||
|
||
bh.consume(found); | ||
|
||
index = (index + 1) % NUM_ITEMS; | ||
} | ||
|
||
@Benchmark | ||
public void addRemove(Blackhole bh) { | ||
var key = items[index].key(); | ||
|
||
if (addRemove[index]) { | ||
l.add(items[index]); | ||
} else { | ||
l.remove(key); | ||
} | ||
|
||
index = (index + 1) % NUM_ITEMS; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.tomfran.lsm.memtable; | ||
|
||
import com.tomfran.lsm.sstable.SSTable; | ||
import com.tomfran.lsm.types.Item; | ||
|
||
import java.util.LinkedList; | ||
|
||
public class Memtable { | ||
|
||
SkipList mutableData; | ||
LinkedList<SkipList> immutableData; | ||
LinkedList<LinkedList<SSTable>> sstables; | ||
|
||
public Memtable() { | ||
mutableData = new SkipList(); | ||
immutableData = new LinkedList<>(); | ||
} | ||
|
||
public void add(Item item) { | ||
mutableData.add(item); | ||
} | ||
|
||
public void get(byte[] key) { | ||
mutableData.get(key); | ||
} | ||
|
||
public void remove(byte[] key) { | ||
mutableData.remove(key); | ||
} | ||
|
||
public int size() { | ||
return mutableData.size(); | ||
} | ||
|
||
private void replaceMutableData() { | ||
immutableData.addFirst(mutableData); | ||
mutableData = new SkipList(); | ||
} | ||
|
||
public SSTable flush() { | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.tomfran.lsm.memtable; | ||
|
||
import com.tomfran.lsm.types.Item; | ||
import it.unimi.dsi.util.XoRoShiRo128PlusRandom; | ||
|
||
import java.util.Iterator; | ||
|
||
import static com.tomfran.lsm.comparator.ByteArrayComparator.compare; | ||
import static java.lang.Math.ceil; | ||
import static java.lang.Math.log; | ||
|
||
public class SkipList implements Iterable<Item> { | ||
|
||
static final int DEFAULT_ELEMENTS = 1 << 16; | ||
final Node sentinel; | ||
private final Node[] buffer; | ||
private final XoRoShiRo128PlusRandom rn; | ||
int levels; | ||
int size; | ||
|
||
public SkipList() { | ||
this(DEFAULT_ELEMENTS); | ||
} | ||
|
||
public SkipList(int numElements) { | ||
levels = (int) ceil(log(numElements) / log(2)); | ||
size = 0; | ||
sentinel = new Node(null, levels); | ||
rn = new XoRoShiRo128PlusRandom(); | ||
buffer = new Node[levels]; | ||
} | ||
|
||
public void add(Item item) { | ||
Node current = sentinel; | ||
for (int i = levels - 1; i >= 0; i--) { | ||
while (current.next[i] != null && current.next[i].value.compareTo(item) < 0) | ||
current = current.next[i]; | ||
buffer[i] = current; | ||
} | ||
|
||
if (current.next[0] != null && current.next[0].value.compareTo(item) == 0) { | ||
current.next[0].value = item; | ||
return; | ||
} | ||
|
||
Node newNode = new Node(item, levels); | ||
for (int i = 0; i < randomLevel(); i++) { | ||
newNode.next[i] = buffer[i].next[i]; | ||
buffer[i].next[i] = newNode; | ||
} | ||
size++; | ||
} | ||
|
||
private int randomLevel() { | ||
int level = 1; | ||
while (rn.nextBoolean() && level < levels) | ||
level++; | ||
return level; | ||
} | ||
|
||
public Item get(byte[] key) { | ||
Node current = sentinel; | ||
for (int i = levels - 1; i >= 0; i--) { | ||
while (current.next[i] != null && compare(current.next[i].value.key(), key) < 0) | ||
current = current.next[i]; | ||
} | ||
|
||
if (current.next[0] != null && compare(current.next[0].value.key(), key) == 0) | ||
return current.next[0].value; | ||
|
||
return null; | ||
} | ||
|
||
public void remove(byte[] key) { | ||
Node current = sentinel; | ||
for (int i = levels - 1; i >= 0; i--) { | ||
while (current.next[i] != null && compare(current.next[i].value.key(), key) < 0) | ||
current = current.next[i]; | ||
buffer[i] = current; | ||
} | ||
|
||
if (current.next[0] != null && compare(current.next[0].value.key(), key) == 0) { | ||
boolean last = current.next[0].next[0] == null; | ||
for (int i = 0; i < levels; i++) { | ||
if (buffer[i].next[i] != current.next[0]) | ||
break; | ||
buffer[i].next[i] = last ? null : current.next[0].next[i]; | ||
} | ||
size--; | ||
} | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public Iterator<Item> iterator() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = levels - 1; i >= 0; i--) { | ||
sb.append(String.format("Level %2d: ", i)); | ||
Node current = sentinel; | ||
while (current.next[i] != null) { | ||
sb.append(current.next[i].value).append(" -> "); | ||
current = current.next[i]; | ||
} | ||
sb.append("END\n"); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
static final class Node { | ||
Item value; | ||
Node[] next; | ||
|
||
Node(Item value, int numLevels) { | ||
this.value = value; | ||
this.next = new Node[numLevels]; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.