use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RareAppenderLatencyTest method testRareAppenderLatency.
@Test
public void testRareAppenderLatency() throws IOException, InterruptedException, ExecutionException {
System.setProperty("ignoreHeaderCountIfNumberOfExcerptsBehindExceeds", "" + (1 << 12));
if (Jvm.isDebug())
// this is a performance test so should not be run in debug mode
return;
assert (isAssertionsOn = true);
if (isAssertionsOn)
// this is a performance test so should not be run with assertions turned on
return;
System.out.println("starting test");
String pathname = OS.getTarget() + "/testRareAppenderLatency-" + System.nanoTime();
new File(pathname).deleteOnExit();
// Shared queue between two threads appending. One appends very rarely, another heavily.
ChronicleQueue queue = new SingleChronicleQueueBuilder(pathname).rollCycle(RollCycles.HOURLY).build();
String text = getText();
// Write a some messages with an appender from Main thread.
ExcerptAppender rareAppender = queue.acquireAppender();
for (int i = 0; i < RARE_MSGS; i++) {
try (DocumentContext ctx = rareAppender.writingDocument()) {
ctx.wire().write(() -> "ts").int64(System.currentTimeMillis()).write(() -> "msg").text(text);
}
}
// Write a bunch of messages from another thread.
Future f = appenderES.submit(() -> {
ExcerptAppender appender = queue.acquireAppender();
long start = System.currentTimeMillis();
for (int i = 0; i < HEAVY_MSGS; i++) {
try (DocumentContext ctx = appender.writingDocument()) {
ctx.wire().write(() -> "ts").int64(System.currentTimeMillis()).write(() -> "msg").text(text);
}
if (appenderES.isShutdown())
return;
}
System.out.println("Wrote heavy " + HEAVY_MSGS + " msgs in " + (System.currentTimeMillis() - start) + " ms");
});
f.get();
// Write a message from the Main thread again (this will have unacceptable latency!)
rareAppender = queue.acquireAppender();
long now = System.currentTimeMillis();
try (DocumentContext ctx = rareAppender.writingDocument()) {
ctx.wire().write(() -> "ts").int64(System.currentTimeMillis()).write(() -> "msg").text(text);
}
long l = System.currentTimeMillis() - now;
// Write another message from the Main thread (this will be fast since we are caught up)
now = System.currentTimeMillis();
try (DocumentContext ctx = rareAppender.writingDocument()) {
ctx.wire().write(() -> "ts").int64(System.currentTimeMillis()).write(() -> "msg").text(text);
}
System.out.println("Wrote first rare one in " + l + " ms");
System.out.println("Wrote another rare one in " + (System.currentTimeMillis() - now) + " ms");
assertFalse("Appending from rare thread latency too high!", l > 150);
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class ReadWriteTest method testReadFromReadOnlyChronicle.
@Test
public void testReadFromReadOnlyChronicle() {
try (SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(chroniclePath).testBlockSize().readOnly(!OS.isWindows()).build()) {
// check dump
assertTrue(out.dump().length() > 1);
// and tailer
ExcerptTailer tailer = out.createTailer();
assertEquals(STR1, tailer.readText());
try (DocumentContext dc = tailer.readingDocument()) {
assertEquals(STR2, dc.wire().bytes().readUtf8());
// even though this is read-only we can still call dc.wire().bytes().write... which causes java.lang.InternalError
// Fixing this in a type-safe manner would require on Read/WriteDocumentContext to return WireIn/WireOut
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class ReadWriteTest method setup.
@Before
public void setup() {
chroniclePath = new File(OS.TARGET, "read_only");
try (SingleChronicleQueue readWrite = SingleChronicleQueueBuilder.binary(chroniclePath).readOnly(false).testBlockSize().build()) {
final ExcerptAppender appender = readWrite.acquireAppender();
appender.writeText(STR1);
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().bytes().writeUtf8(STR2);
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class TailerDirectionTest method uninitialisedTailerCreatedBeforeFirstAppendWithDirectionNoneShouldNotFindDocument.
@Test
public void uninitialisedTailerCreatedBeforeFirstAppendWithDirectionNoneShouldNotFindDocument() {
final AtomicLong clock = new AtomicLong(System.currentTimeMillis());
String path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
final ChronicleQueue queue = ChronicleQueueBuilder.single(path).timeProvider(clock::get).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).build();
final ExcerptTailer tailer = queue.createTailer();
tailer.direction(TailerDirection.NONE);
final ExcerptAppender excerptAppender = queue.acquireAppender();
for (int i = 0; i < 10; i++) {
excerptAppender.writeDocument(i, (out, value) -> {
out.int32(value);
});
}
DocumentContext document = tailer.readingDocument();
assertFalse(document.isPresent());
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class AppenderFileHandleLeakTest method tailerShouldReleaseFileHandlesAsQueueRolls.
@Test
public void tailerShouldReleaseFileHandlesAsQueueRolls() throws Exception {
System.gc();
Thread.sleep(100);
assumeThat(OS.isLinux(), is(true));
final int messagesPerThread = 10;
try (SingleChronicleQueue queue = createQueue(currentTime::get)) {
final long openFileHandleCount = countFileHandlesOfCurrentProcess();
final List<Path> fileHandlesAtStart = new ArrayList<>(lastFileHandles);
final List<Future<Boolean>> futures = new LinkedList<>();
for (int i = 0; i < THREAD_COUNT; i++) {
futures.add(threadPool.submit(() -> {
for (int j = 0; j < messagesPerThread; j++) {
writeMessage(j, queue);
currentTime.addAndGet(100);
}
return Boolean.TRUE;
}));
}
for (Future<Boolean> future : futures) {
assertThat(future.get(1, TimeUnit.MINUTES), is(true));
}
waitForFileHandleCountToDrop(openFileHandleCount, fileHandlesAtStart);
fileHandlesAtStart.clear();
final long tailerOpenFileHandleCount = countFileHandlesOfCurrentProcess();
final ExcerptTailer tailer = queue.createTailer();
tailer.toStart();
final int expectedMessageCount = THREAD_COUNT * messagesPerThread;
int messageCount = 0;
storeFileListener.reset();
while (true) {
try (final DocumentContext ctx = tailer.readingDocument()) {
if (!ctx.isPresent()) {
break;
}
messageCount++;
}
}
assertThat(messageCount, is(expectedMessageCount));
assertThat(storeFileListener.toString(), storeFileListener.releasedCount, is(withinDelta(storeFileListener.acquiredCount, 3)));
waitForFileHandleCountToDrop(tailerOpenFileHandleCount, fileHandlesAtStart);
}
}
Aggregations