Search in sources :

Example 36 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class ThroughputMain method main.

public static void main(String[] args) {
    System.out.println("Testing with " + "-Dtime=" + time + " " + "-Dthreads=" + threads + " " + "-Dsize=" + size + " " + "-Dpath=" + path + " " + "-DfullWrite=" + fullWrite);
    long start = System.nanoTime();
    String base = path + "/delete-" + Time.uniqueId() + ".me.";
    long blockSize = OS.is64Bit() ? OS.isLinux() ? 4L << 30 : 1L << 30 : 256L << 20;
    AtomicLong count = new AtomicLong();
    IntStream.range(0, threads).parallel().forEach(i -> {
        long count2 = 0;
        BytesStore<?, Void> nbs = BytesStore.nativeStoreWithFixedCapacity(size);
        try (ChronicleQueue q = ChronicleQueue.singleBuilder(base + i).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 (!fullWrite && 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, nbs);
                    bytes.writeSkip((int) lengthCount);
                    lastIndex += lengthCount >> 32;
                    count2 += lengthCount >> 32;
                } else {
                    try (DocumentContext dc = appender.writingDocument()) {
                        Wire wire2 = dc.wire();
                        wire2.bytes().write(nbs);
                        addToEndOfCache(wire2);
                    }
                    lastIndex = appender.lastIndexAppended();
                    count2++;
                }
            } while (start + time * 1e9 > System.nanoTime());
        }
        // System.out.println("... All data written, now reading ...");
        nbs.releaseLast();
        count.addAndGet(count2);
    });
    long time1 = System.nanoTime() - start;
    Jvm.pause(1000);
    System.gc();
    long mid = System.nanoTime();
    IntStream.range(0, threads).parallel().forEach(i -> {
        Bytes bytes = Bytes.allocateElasticDirect(64);
        try (ChronicleQueue q = ChronicleQueue.singleBuilder(base + i).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
            ExcerptTailer tailer = q.createTailer();
            for (; ; ) {
                try (DocumentContext dc = tailer.readingDocument()) {
                    if (!dc.isPresent())
                        break;
                    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.longValue(), time1 / 1e9, 1000 * (long) (1e6 * count.get() / time1));
    System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n", count.longValue(), time2 / 1e9, 1000 * (long) (1e6 * count.get() / time2));
    Jvm.pause(200);
    // make sure its cleaned up for windows to delete.
    System.gc();
    IntStream.range(0, threads).forEach(i -> IOTools.deleteDirWithFiles(base + i, 2));
}
Also used : Wire(net.openhft.chronicle.wire.Wire) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes) AtomicLong(java.util.concurrent.atomic.AtomicLong) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Example 37 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class MoveToIndexTest method testRandomMove.

// https://github.com/OpenHFT/Chronicle-Queue/issues/401
@Test
public void testRandomMove() throws IOException {
    final Map<Long, String> messageByIndex = new HashMap<>();
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpFolder.newFolder()).build()) {
        // create a queue and add some excerpts
        final ExcerptAppender appender = queue.acquireAppender();
        for (int i = 0; i < 10; i++) {
            final String message = "msg" + i;
            appender.writeDocument(w -> w.write("message").object(message));
            final long appendIndex = appender.lastIndexAppended();
            messageByIndex.put(appendIndex, message);
        }
        final Random random = new Random(1510298038000L);
        final List<Long> indices = new ArrayList<>(messageByIndex.keySet());
        final ExcerptTailer tailer = queue.createTailer();
        final AtomicReference<String> capturedMessage = new AtomicReference<>();
        for (int i = 0; i < 100; i++) {
            final long randomIndex = indices.get(random.nextInt(messageByIndex.keySet().size()));
            tailer.moveToIndex(randomIndex);
            tailer.readDocument(w -> capturedMessage.set((String) w.read("message").object()));
            assertEquals(messageByIndex.get(randomIndex), capturedMessage.get());
            tailer.readDocument(w -> w.read("message").object());
        }
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Test(org.junit.Test)

Example 38 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class RestartableTailerTest method restartable.

@Test
public void restartable() {
    String tmp = OS.getTarget() + "/restartable-" + Time.uniqueId();
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
        for (int i = 0; i < 7; i++) cq.acquireAppender().writeText("test " + i);
    }
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build();
        ExcerptTailer atailer = cq.createTailer("a")) {
        assertEquals("test 0", atailer.readText());
        assertEquals("test 1", atailer.readText());
        assertEquals("test 2", atailer.readText());
        try (ExcerptTailer btailer = cq.createTailer("b")) {
            assertEquals("test 0", btailer.readText());
        }
    }
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build();
        ExcerptTailer atailer = cq.createTailer("a")) {
        assertEquals("test 3", atailer.readText());
        assertEquals("test 4", atailer.readText());
        assertEquals("test 5", atailer.readText());
        try (ExcerptTailer btailer = cq.createTailer("b")) {
            assertEquals("test 1", btailer.readText());
        }
    }
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build();
        ExcerptTailer atailer = cq.createTailer("a")) {
        assertEquals("test 6", atailer.readText());
        assertNull(atailer.readText());
        try (ExcerptTailer btailer = cq.createTailer("b")) {
            assertEquals("test 2", btailer.readText());
        }
    }
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build();
        ExcerptTailer atailer = cq.createTailer("a")) {
        assertNull(atailer.readText());
        try (ExcerptTailer btailer = cq.createTailer("b")) {
            assertEquals("test 3", btailer.readText());
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 39 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueBuilderTest method buildWillNotSetCreateAppenderConditionWhenQueueIsReadOnly.

@Test
public void buildWillNotSetCreateAppenderConditionWhenQueueIsReadOnly() {
    assumeFalse(OS.isWindows());
    final File tmpDir = getTmpDir();
    try (ChronicleQueue ignored = SingleChronicleQueueBuilder.single(tmpDir).build()) {
    // just create the queue
    }
    try (SingleChronicleQueue ignored = SingleChronicleQueueBuilder.single(tmpDir).createAppenderConditionCreator(q -> {
        throw new AssertionError("This should never be called");
    }).readOnly(true).build()) {
    // This will throw if we attempt to create the createAppender condition
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) File(java.io.File) Test(org.junit.Test)

Example 40 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueBuilderTest method shouldDetermineQueueDirectoryFromQueueFile.

@Test
public void shouldDetermineQueueDirectoryFromQueueFile() throws IOException {
    expectException("reading control code as text");
    ignoreException("Unable to copy TimedStoreRecovery safely");
    expectException("Queues should be configured with the queue directory, not a specific filename");
    ignoreException("Metadata file not found in readOnly mode");
    final Path path = Paths.get(OS.USER_DIR, TEST_QUEUE_FILE);
    final Path metadata = Paths.get(path.getParent().toString(), "metadata.cq4t");
    if (metadata.toFile().exists())
        Files.delete(metadata);
    try (final ChronicleQueue queue = ChronicleQueue.singleBuilder(path).testBlockSize().readOnly(true).build();
        final ExcerptTailer tailer = queue.createTailer();
        final DocumentContext dc = tailer.readingDocument()) {
        // System.out.println(queue.dump());
        assertFalse(dc.isPresent());
    } finally {
        IOTools.deleteDirWithFiles(path.toFile(), 20);
    }
    assertTrue(new File(TEST_QUEUE_FILE).length() < (1 << 20));
}
Also used : Path(java.nio.file.Path) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Aggregations

ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)80 Test (org.junit.Test)59 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)37 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)37 File (java.io.File)36 DocumentContext (net.openhft.chronicle.wire.DocumentContext)21 ArrayList (java.util.ArrayList)16 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)14 MethodReader (net.openhft.chronicle.bytes.MethodReader)12 List (java.util.List)11 IOException (java.io.IOException)9 MappedFile (net.openhft.chronicle.bytes.MappedFile)8 Path (java.nio.file.Path)7 Bytes (net.openhft.chronicle.bytes.Bytes)7 Collections (java.util.Collections)6 RollCycles (net.openhft.chronicle.queue.RollCycles)6 NotNull (org.jetbrains.annotations.NotNull)6 ByteBuffer (java.nio.ByteBuffer)5 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)5 Histogram (net.openhft.chronicle.core.util.Histogram)5