Skip to content

Commit

Permalink
Add reference performance test for Java binding
Browse files Browse the repository at this point in the history
  • Loading branch information
nblintao committed Mar 4, 2022
1 parent 2eeef55 commit c1a1515
Showing 1 changed file with 49 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,13 @@ void comparePerformance() {
FDB fdb = FDB.selectAPIVersion(710);
try (Database db = openFDB()) {
insertRecordsWithIndexes(numRecords, db);
// TODO: Update rangeQueryAndGet to support range query.
// instrument(rangeQueryAndGet, "rangeQueryAndGet", db);
instrument(rangeQueryAndThenRangeQueries, "rangeQueryAndThenRangeQueries", db);
instrument(mappedRangeQuery, "mappedRangeQuery", db);
}
}

private void instrument(final RangeQueryWithIndex query, final String name, final Database db) {
System.out.printf("Starting %s (numQueries:%d, numRecordsPerQuery:%d)\n", name, numQueries, numRecordsPerQuery);
System.out.printf("Starting %s (numQueries:%d, numRecordsPerQuery:%d, validation:%s)\n", name, numQueries, numRecordsPerQuery, validate ? "on" : "off");
long startTime = System.currentTimeMillis();
for (int queryId = 0; queryId < numQueries; queryId++) {
int begin = ThreadLocalRandom.current().nextInt(numRecords - numRecordsPerQuery);
Expand Down Expand Up @@ -152,43 +151,45 @@ public interface RangeQueryWithIndex {
void run(int begin, int end, Database db);
}

/*
RangeQueryWithIndex rangeQueryAndGet = (int begin, int end, Database db) -> db.run(tr -> {
try {
List<KeyValue> kvs = tr.getRange(KeySelector.firstGreaterOrEqual(indexEntryKey(begin)),
KeySelector.firstGreaterOrEqual(indexEntryKey(end)),
ReadTransaction.ROW_LIMIT_UNLIMITED, false, StreamingMode.WANT_ALL)
.asList()
.get();
Assertions.assertEquals(end - begin, kvs.size());
RangeQueryWithIndex rangeQueryAndThenRangeQueries = (int begin, int end, Database db) -> db.run(tr -> {
try {
List<KeyValue> kvs = tr.getRange(KeySelector.firstGreaterOrEqual(indexEntryKey(begin)),
KeySelector.firstGreaterOrEqual(indexEntryKey(end)),
ReadTransaction.ROW_LIMIT_UNLIMITED, false, StreamingMode.WANT_ALL)
.asList()
.get();
Assertions.assertEquals(end - begin, kvs.size());

// Get the records of each index entry IN PARALLEL.
List<CompletableFuture<byte[]>> resultFutures = new ArrayList<>();
// In reality, we need to get the record key by parsing the index entry key. But considering this is a
// performance test, we just ignore the returned key and simply generate it from recordKey.
for (int id = begin; id < end; id++) {
resultFutures.add(tr.get(recordKey(id)));
}
AsyncUtil.whenAll(resultFutures).get();
// Get the records of each index entry IN PARALLEL.
List<CompletableFuture<List<KeyValue>>> resultFutures = new ArrayList<>();
// In reality, we need to get the record key by parsing the index entry key. But considering this is a
// performance test, we just ignore the returned key and simply generate it from recordKey.
for (int id = begin; id < end; id++) {
resultFutures.add(tr.getRange(Range.startsWith(recordKeyPrefix(id)),
ReadTransaction.ROW_LIMIT_UNLIMITED, false, StreamingMode.WANT_ALL).asList());
}
AsyncUtil.whenAll(resultFutures).get();

if (validate) {
final Iterator<KeyValue> indexes = kvs.iterator();
final Iterator<CompletableFuture<List<KeyValue>>> records = resultFutures.iterator();
for (int id = begin; id < end; id++) {
Assertions.assertTrue(indexes.hasNext());
assertByteArrayEquals(indexEntryKey(id), indexes.next().getKey());

Assertions.assertTrue(records.hasNext());
List<KeyValue> rangeResult = records.next().get();
validateRangeResult(id, rangeResult);
}
Assertions.assertFalse(indexes.hasNext());
Assertions.assertFalse(records.hasNext());
}
} catch (Exception e) {
Assertions.fail("Unexpected exception", e);
}
return null;
});

if (validate) {
final Iterator<KeyValue> indexes = kvs.iterator();
final Iterator<CompletableFuture<byte[]>> records = resultFutures.iterator();
for (int id = begin; id < end; id++) {
Assertions.assertTrue(indexes.hasNext());
assertByteArrayEquals(indexEntryKey(id), indexes.next().getKey());
Assertions.assertTrue(records.hasNext());
assertByteArrayEquals(recordValue(id), records.next().get());
}
Assertions.assertFalse(indexes.hasNext());
Assertions.assertFalse(records.hasNext());
}
} catch (Exception e) {
Assertions.fail("Unexpected exception", e);
}
return null;
});
*/
RangeQueryWithIndex mappedRangeQuery = (int begin, int end, Database db) -> db.run(tr -> {
try {
List<MappedKeyValue> kvs =
Expand All @@ -214,12 +215,7 @@ public interface RangeQueryWithIndex {
assertByteArrayEquals(prefix, mappedKeyValue.getRangeEnd());

List<KeyValue> rangeResult = mappedKeyValue.getRangeResult();
Assertions.assertEquals(rangeResult.size(), SPLIT_SIZE);
for (int split = 0; split < SPLIT_SIZE; split++) {
KeyValue keyValue = rangeResult.get(split);
assertByteArrayEquals(recordKey(id, split), keyValue.getKey());
assertByteArrayEquals(recordValue(id, split), keyValue.getValue());
}
validateRangeResult(id, rangeResult);
}
Assertions.assertFalse(results.hasNext());
}
Expand All @@ -229,6 +225,15 @@ public interface RangeQueryWithIndex {
return null;
});

void validateRangeResult(int id, List<KeyValue> rangeResult) {
Assertions.assertEquals(rangeResult.size(), SPLIT_SIZE);
for (int split = 0; split < SPLIT_SIZE; split++) {
KeyValue keyValue = rangeResult.get(split);
assertByteArrayEquals(recordKey(id, split), keyValue.getKey());
assertByteArrayEquals(recordValue(id, split), keyValue.getValue());
}
}

void assertByteArrayEquals(byte[] expected, byte[] actual) {
Assertions.assertEquals(ByteArrayUtil.printable(expected), ByteArrayUtil.printable(actual));
}
Expand Down

0 comments on commit c1a1515

Please sign in to comment.