use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.
the class IndexTest method shouldShortCircuitIndexLookupWhenNewIndexIsCloseToPreviousIndex.
@Test
public void shouldShortCircuitIndexLookupWhenNewIndexIsCloseToPreviousIndex() {
try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).testBlockSize().wireType(this.wireType).build()) {
final ExcerptAppender appender = queue.acquireAppender();
final int messageCount = INDEXING_LINEAR_SCAN_THRESHOLD + 5;
final long[] indices = new long[messageCount];
for (int i = 0; i < messageCount; i++) {
try (final DocumentContext ctx = appender.writingDocument()) {
ctx.wire().write("event").int32(i);
indices[i] = ctx.index();
}
}
final StoreTailer tailer = (StoreTailer) queue.createTailer();
tailer.moveToIndex(indices[0]);
assertEquals(indices[0], tailer.index());
assertEquals(1, tailer.getIndexMoveCount());
tailer.moveToIndex(indices[0]);
assertEquals(indices[0], tailer.index());
assertEquals(1, tailer.getIndexMoveCount());
tailer.moveToIndex(indices[2]);
assertEquals(indices[2], tailer.index());
assertEquals(1, tailer.getIndexMoveCount());
tailer.moveToIndex(indices[INDEXING_LINEAR_SCAN_THRESHOLD + 2]);
assertEquals(indices[INDEXING_LINEAR_SCAN_THRESHOLD + 2], tailer.index());
assertEquals(2, tailer.getIndexMoveCount());
// document that moving backwards requires an index scan
tailer.moveToIndex(indices[INDEXING_LINEAR_SCAN_THRESHOLD - 1]);
assertEquals(indices[INDEXING_LINEAR_SCAN_THRESHOLD - 1], tailer.index());
assertEquals(3, tailer.getIndexMoveCount());
}
}
use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.
the class QueueExamples1 method main.
public static void main(String[] args) {
ChronicleQueue queue = ChronicleQueue.single("./myQueueDir");
Printer printer = queue.methodWriter(Printer.class);
printer.print("hello world");
}
use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.
the class ThroughputPerfMain method main.
public static void main(String[] args) {
String base = path + "/delete-" + Time.uniqueId() + ".me";
long start = System.nanoTime();
long count = 0;
nbs = BytesStore.nativeStoreWithFixedCapacity(size);
long blockSize = OS.is64Bit() ? 4L << 30 : 256L << 20;
try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
ExcerptAppender appender = q.acquireAppender();
long lastIndex = -1;
do {
int defaultIndexSpacing = q.rollCycle().defaultIndexSpacing();
Wire wire = appender.wire();
int writeCount = (int) (defaultIndexSpacing - (lastIndex & (defaultIndexSpacing - 1)) - 1);
if (wire != null && writeCount > 0) {
MappedBytes bytes = (MappedBytes) wire.bytes();
long address = bytes.addressForWrite(bytes.writePosition());
long bstart = bytes.start();
long bcap = bytes.realCapacity();
long canWrite = bcap - (bytes.writePosition() - bstart);
long lengthCount = writeMessages(address, canWrite, writeCount);
bytes.writeSkip((int) lengthCount);
lastIndex += lengthCount >> 32;
count += lengthCount >> 32;
} else {
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().bytes().write(nbs);
}
lastIndex = appender.lastIndexAppended();
count++;
}
} while (start + time * 1e9 > System.nanoTime());
}
nbs.releaseLast();
long mid = System.nanoTime();
long time1 = mid - start;
Bytes bytes = Bytes.allocateElasticDirect(64);
try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
ExcerptTailer tailer = q.createTailer();
for (long i = 0; i < count; i++) {
try (DocumentContext dc = tailer.readingDocument()) {
bytes.clear();
bytes.write(dc.wire().bytes());
}
}
}
bytes.releaseLast();
long end = System.nanoTime();
long time2 = end - mid;
System.out.printf("Writing %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time1 / 1e9, (long) (1e9 * count / time1));
System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time2 / 1e9, (long) (1e9 * count / time2));
// make sure its cleaned up for windows to delete.
System.gc();
IOTools.deleteDirWithFiles(base, 2);
}
use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.
the class CheckIndicesTest method test.
@Ignore("stress test to run manually")
@Test
public void test() throws ExecutionException, InterruptedException {
try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).epoch(System.currentTimeMillis()).build()) {
queue0 = queue;
newSingleThreadScheduledExecutor().scheduleAtFixedRate(this::appendToQueue, 0, 1, TimeUnit.MICROSECONDS);
Future f = newSingleThreadScheduledExecutor().submit(this::checkIndices);
Future f2 = newSingleThreadScheduledExecutor().submit(this::checkIndices);
for (; ; ) {
if (f.isDone())
f.get();
if (f2.isDone())
f2.get();
Thread.sleep(500);
}
}
}
use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.
the class MessageHistoryTest method shouldAccessMessageHistory.
@Test
public void shouldAccessMessageHistory() {
try (final ChronicleQueue inputQueue = createQueue(inputQueueDir, 1);
final ChronicleQueue outputQueue = createQueue(outputQueueDir, 2)) {
generateTestData(inputQueue, outputQueue);
final ExcerptTailer tailer = outputQueue.createTailer(named ? "named" : null);
final ValidatingSecond validatingSecond = new ValidatingSecond();
final MethodReader validator = tailer.methodReader(validatingSecond);
assertTrue(validator.readOne());
assertTrue(validatingSecond.messageHistoryPresent());
}
}
Aggregations