use of io.pravega.common.util.BufferView in project pravega by pravega.
the class BenchmarkTests method testSequentialOperations.
private SequentialResult testSequentialOperations(CacheStorage s) {
val writeBuffer = new ByteArraySegment(new byte[ENTRY_SIZE]);
val appendBuffer = new ByteArraySegment(writeBuffer.array(), 0, s.getAppendableLength(ENTRY_SIZE));
val readBuffer = new byte[ENTRY_SIZE];
this.random.nextBytes(writeBuffer.array());
int[] ids = new int[ENTRY_COUNT];
val insert = measure(() -> {
for (int i = 0; i < ENTRY_COUNT; i++) {
ids[i] = s.insert(writeBuffer);
}
});
val append = measure(() -> {
for (int i = 0; i < ENTRY_COUNT; i++) {
s.append(ids[i], writeBuffer.getLength(), appendBuffer);
}
});
val replace = measure(() -> {
for (int i = 0; i < ENTRY_COUNT; i++) {
ids[i] = s.replace(ids[i], writeBuffer);
}
});
val get = measure(() -> {
for (int i = 0; i < ENTRY_COUNT; i++) {
BufferView result = s.get(ids[i]);
result.copyTo(ByteBuffer.wrap(readBuffer));
}
});
val delete = measure(() -> {
for (int i = 0; i < ENTRY_COUNT; i++) {
s.delete(ids[i]);
}
});
return new SequentialResult(insert, replace, append, get, delete);
}
use of io.pravega.common.util.BufferView in project pravega by pravega.
the class DirectMemoryCacheTests method testInsertErrors.
/**
* Tests the ability to roll back any modifications that may have been performed while inserting. The easiest one to
* trigger is {@link CacheFullException}s.
*/
@Test
public void testInsertErrors() {
final int cleanAfterInvocations = DirectMemoryCache.MAX_CLEANUP_ATTEMPTS;
// 2-buffer cache.
final int maxSize = 2 * LAYOUT.bufferSize();
// Leave 3 blocks empty from first cache.
final int firstWriteSize = LAYOUT.bufferSize() - 4 * LAYOUT.blockSize();
// Second write would attempt to overfill the cache.
final int secondWriteSize = maxSize - firstWriteSize;
final BufferView toInsert = new ByteArraySegment(new byte[1]);
@Cleanup val c = new TestCache(maxSize);
val firstWrite = new byte[firstWriteSize];
rnd.nextBytes(firstWrite);
val address = c.insert(new ByteArraySegment(firstWrite));
checkSnapshot(c, (long) firstWriteSize, (long) firstWriteSize + LAYOUT.blockSize(), (long) LAYOUT.blockSize(), (long) LAYOUT.bufferSize(), (long) maxSize);
AssertExtensions.assertThrows("Expected CacheFullException when inserting entry that would overfill.", () -> c.insert(new ByteArraySegment(new byte[secondWriteSize])), ex -> ex instanceof CacheFullException);
AssertExtensions.assertThrows("Expected CacheFullException when replacing entry with entry that would overfill.", () -> c.replace(address, new ByteArraySegment(new byte[secondWriteSize])), ex -> ex instanceof CacheFullException);
// We should still have the same amount of bytes stored, but we should have allocated the second buffer.
checkSnapshot(c, (long) firstWriteSize, firstWriteSize + LAYOUT.blockSize() * 2L, LAYOUT.blockSize() * 2L, (long) maxSize, (long) maxSize);
// Verify our original write is still intact.
checkData(c, address, firstWrite, 0, firstWrite.length);
}
Aggregations