Search in sources :

Example 21 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class SingleCQFormat2Test method appendMessage.

public void appendMessage(@NotNull ChronicleQueue queue, long expectedIndex, String msg) {
    @NotNull ExcerptAppender appender = queue.acquireAppender();
    switch(appendMode) {
        case 1:
            appender.writeDocument(w -> w.write("msg").text(msg));
            break;
        case 2:
            Bytes bytes = Bytes.elasticByteBuffer();
            new BinaryWire(bytes).write("msg").text(msg);
            appender.writeBytes(bytes);
            bytes.releaseLast();
            break;
        default:
            try (DocumentContext dc = appender.writingDocument()) {
                Wire wire = dc.wire();
                wire.write("msg").text(msg);
            }
            break;
    }
    long index = appender.lastIndexAppended();
    assertHexEquals(expectedIndex, index);
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with Bytes

use of net.openhft.chronicle.bytes.Bytes 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);
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Wire(net.openhft.chronicle.wire.Wire) DocumentContext(net.openhft.chronicle.wire.DocumentContext) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 23 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class RawAccessJavaTest method Tailer.

@Test
public void Tailer() {
    if (!assert_from_cpp())
        return;
    String tmp = "/dev/shm/RawAccessCtoJ";
    // so C++ knows this ran rather than skipped
    System.out.println(tmp);
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
        ExcerptTailer tailer = cq.createTailer();
        for (int i = 0; i < COUNT; ++i) {
            try (DocumentContext dc = tailer.readingDocument()) {
                Bytes bytes = dc.wire().bytes();
                bytes.readSkip(-QUEUE_HEADER_SIZE);
                int header = bytes.readInt();
                // document length, inc 4-byte length
                int length = Wires.lengthOf(header);
                // actual length of data
                int data_length = bytes.readInt();
                assertEquals(bytes.readByte(), (byte) 0xab);
                assertEquals(bytes.readShort(), (short) 12);
                assertEquals(bytes.readInt(), 123);
                assertEquals(bytes.readLong(), 123456789L);
                assertEquals(bytes.readFloat(), 1.234f, 1.0e-7);
                assertEquals(bytes.readDouble(), 123.456, 1.0e-7);
                assertEquals(bytes.readChar(), 'a');
                StringBuilder sb = new StringBuilder();
                bytes.read8bit(sb);
                assertEquals(sb.toString(), "Hello World");
            }
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 24 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class RawAccessJavaTest method Appender.

@Test
public void Appender() {
    if (!assert_from_cpp())
        return;
    String tmp = "/dev/shm/RawAccessJtoC";
    // so C++ knows this ran rather than skipped
    System.out.println(tmp);
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
        ExcerptAppender appender = cq.acquireAppender();
        for (int i = 0; i < COUNT; ++i) {
            try (DocumentContext dc = appender.writingDocument()) {
                Bytes bytes = dc.wire().bytes();
                // will contain the size of the blob
                long start = bytes.writePosition();
                bytes.writeSkip(RAW_SIZE_PREFIX);
                {
                    bytes.writeByte((byte) 0xab);
                    bytes.writeShort((short) 12);
                    bytes.writeInt(123);
                    bytes.writeLong(123456789L);
                    bytes.writeFloat(1.234f);
                    bytes.writeDouble(123.456);
                    bytes.writeChar('a');
                    bytes.write8bit("Hello World");
                }
                long end = bytes.writePosition();
                bytes.writeInt(start, (int) (end - start - RAW_SIZE_PREFIX));
            }
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 25 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class ThreadedQueueTest method testTailerReadingEmptyQueue.

@Test
public void testTailerReadingEmptyQueue() {
    final File path = getTmpDir();
    try (final ChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(TEST_DAILY).build()) {
        final ExcerptTailer tailer = rqueue.createTailer();
        try (final ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(TEST_DAILY).build()) {
            Bytes bytes = Bytes.elasticByteBuffer();
            assertFalse(tailer.readBytes(bytes));
            final ExcerptAppender appender = wqueue.acquireAppender();
            appender.writeBytes(Bytes.wrapForRead("Hello World".getBytes(ISO_8859_1)));
            bytes.clear();
            boolean condition = tailer.readBytes(bytes);
            // TODO FIX, Something in the cache for directory isn't being updated.
            if (!condition) {
                Jvm.pause(1);
                condition = tailer.readBytes(bytes);
            }
            assertTrue(condition);
            assertEquals("Hello World", bytes.toString());
            bytes.releaseLast();
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) File(java.io.File) Test(org.junit.Test)

Aggregations

Bytes (net.openhft.chronicle.bytes.Bytes)53 Test (org.junit.Test)23 NotNull (org.jetbrains.annotations.NotNull)16 File (java.io.File)13 DocumentContext (net.openhft.chronicle.wire.DocumentContext)10 BytesRingBuffer (net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)9 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)8 Wire (net.openhft.chronicle.wire.Wire)7 ByteBuffer (java.nio.ByteBuffer)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)6 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 InternalAppender (net.openhft.chronicle.queue.impl.single.InternalAppender)6 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)6 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)5 NativeBytes (net.openhft.chronicle.bytes.NativeBytes)4 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)4 ParseException (java.text.ParseException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AffinityLock (net.openhft.affinity.AffinityLock)3