Search in sources :

Example 6 with Histogram

use of net.openhft.chronicle.core.util.Histogram in project Chronicle-Queue by OpenHFT.

the class StagedPerformanceMain method runBenchmark.

private static void runBenchmark(int count, int interval, boolean warmup) {
    String run = Long.toString(System.nanoTime(), 36);
    WARMUP = warmup;
    DIR = PATH + "/run-" + run;
    new File(DIR).mkdirs();
    // every 60 seconds.
    REPORT_INTERVAL = (int) (60e9 / interval);
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(DIR + "/data").build()) {
        q0 = queue;
        List<Runnable> runnables = new ArrayList<>();
        runnables.add(() -> producer(count, interval));
        runnables.add(() -> firstStage());
        for (int s = 2; s <= STAGES; s++) {
            int finalS = s;
            runnables.add(() -> runStage(finalS));
        }
        runnables.stream().parallel().forEach(Runnable::run);
        Histogram latencies = new Histogram();
        reportLatenciesForStage(STAGES, latencies);
    }
    IOTools.deleteDirWithFiles(DIR, 3);
}
Also used : Histogram(net.openhft.chronicle.core.util.Histogram) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ArrayList(java.util.ArrayList) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile)

Example 7 with Histogram

use of net.openhft.chronicle.core.util.Histogram in project Chronicle-Queue by OpenHFT.

the class SingleChroniclePerfMainTest method doPerfTest.

static void doPerfTest(TestWriter<Bytes> writer, TestReader<Bytes> reader, int count, boolean print) throws IOException {
    Histogram writeHdr = new Histogram(30, 7);
    Histogram readHdr = new Histogram(30, 7);
    String file = OS.getTarget() + "/deleteme-" + Time.uniqueId();
    try (ChronicleQueue chronicle = single(file).blockSize(64 << 20).build()) {
        ExcerptAppender appender = chronicle.acquireAppender();
        UncheckedBytes bytes = new UncheckedBytes(NoBytesStore.NO_BYTES);
        for (int i = 0; i < count; i++) {
            long start = System.nanoTime();
            try (DocumentContext dc = appender.writingDocument()) {
                Bytes<?> bytes0 = dc.wire().bytes();
                bytes0.ensureCapacity(size);
                bytes.setBytes(bytes0);
                bytes.readPosition(bytes.writePosition());
                writer.writeTo(bytes);
                bytes0.writePosition(bytes.writePosition());
            }
            long time = System.nanoTime() - start;
            writeHdr.sample(time);
        }
        ExcerptTailer tailer = chronicle.createTailer();
        for (int i = 0; i < count; i++) {
            long start2 = System.nanoTime();
            try (DocumentContext dc = tailer.readingDocument()) {
                assertTrue(dc.isPresent());
                Bytes<?> bytes0 = dc.wire().bytes();
                bytes.setBytes(bytes0);
                reader.readFrom(bytes);
            }
            long time2 = System.nanoTime() - start2;
            readHdr.sample(time2);
        }
    }
    if (print) {
        System.out.println("Write latencies " + writeHdr.toMicrosFormat());
        System.out.println("Read latencies " + readHdr.toMicrosFormat());
    }
    IOTools.deleteDirWithFiles(file, 3);
}
Also used : Histogram(net.openhft.chronicle.core.util.Histogram) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 8 with Histogram

use of net.openhft.chronicle.core.util.Histogram in project Chronicle-Queue by OpenHFT.

the class ChronicleHistoryReaderTest method checkWithQueueHistoryRecordHistoryInitial.

private void checkWithQueueHistoryRecordHistoryInitial(Class<? extends DummyListener> dummyClass) {
    expectException("Overriding sourceId from existing metadata, was 0, overriding to");
    MessageHistory.set(null);
    int extraTiming = 1;
    File queuePath1 = IOTools.createTempFile("testWithQueueHistory1-");
    File queuePath2 = IOTools.createTempFile("testWithQueueHistory2-");
    File queuePath3 = IOTools.createTempFile("testWithQueueHistory3-");
    try {
        try (ChronicleQueue out = queue(queuePath1, 1)) {
            DummyListener writer = out.acquireAppender().methodWriterBuilder(dummyClass).useMethodIds(true).get();
            // this will write the 1st timestamps
            writer.say("hello");
        }
        try (ChronicleQueue in = queue(queuePath1, 1);
            ChronicleQueue out = queue(queuePath2, 2)) {
            DummyListener writer = out.acquireAppender().methodWriterBuilder(dummyClass).get();
            final AtomicInteger numberRead = new AtomicInteger();
            // if this listener is a DummyListener then messages with methodId won't be routed to it
            DummyListenerId dummy = msg -> {
                numberRead.incrementAndGet();
                MessageHistory history = MessageHistory.get();
                Assert.assertEquals(1, history.sources());
                // written 1st then received by me
                Assert.assertEquals(1 + extraTiming, history.timings());
                // this writes 2 more timestamps
                writer.say(msg);
            };
            MethodReader reader = in.createTailer().methodReader(dummy);
            assertTrue(reader.readOne());
            assertEquals("check routed to correct dest", 1, numberRead.get());
            assertFalse(reader.readOne());
        }
        try (ChronicleQueue in = queue(queuePath2, 2);
            ChronicleQueue out = queue(queuePath3, 3)) {
            DummyListener writer = out.acquireAppender().methodWriterBuilder(dummyClass).get();
            final AtomicInteger numberRead = new AtomicInteger();
            DummyListenerId dummy = msg -> {
                numberRead.incrementAndGet();
                MessageHistory history = MessageHistory.get();
                Assert.assertEquals(2, history.sources());
                Assert.assertEquals(3 + extraTiming, history.timings());
                // this writes 2 more timestamps
                writer.say(msg);
            };
            MethodReader reader = in.createTailer().methodReader(dummy);
            assertTrue(reader.readOne());
            assertEquals("check routed to correct dest", 1, numberRead.get());
            assertFalse(reader.readOne());
        }
        ChronicleHistoryReader chronicleHistoryReader = new ChronicleHistoryReader().withBasePath(queuePath3.toPath()).withTimeUnit(TimeUnit.MICROSECONDS).withMessageSink(System.out::println);
        Map<String, Histogram> histos = chronicleHistoryReader.readChronicle();
        chronicleHistoryReader.outputData();
        Assert.assertEquals(5, histos.size());
        Assert.assertEquals("[1, startTo1, 2, 1to2, endToEnd]", histos.keySet().toString());
    } finally {
        IOTools.deleteDirWithFiles(queuePath1.toString(), queuePath2.toString(), queuePath3.toString());
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) MessageHistory(net.openhft.chronicle.wire.MessageHistory) Histogram(net.openhft.chronicle.core.util.Histogram) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Test(org.junit.Test) ChronicleHistoryReader(net.openhft.chronicle.queue.reader.ChronicleHistoryReader) File(java.io.File) MethodId(net.openhft.chronicle.bytes.MethodId) TimeUnit(java.util.concurrent.TimeUnit) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOTools(net.openhft.chronicle.core.io.IOTools) Map(java.util.Map) QueueTestCommon(net.openhft.chronicle.queue.QueueTestCommon) OS(net.openhft.chronicle.core.OS) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) Histogram(net.openhft.chronicle.core.util.Histogram) ChronicleHistoryReader(net.openhft.chronicle.queue.reader.ChronicleHistoryReader) MessageHistory(net.openhft.chronicle.wire.MessageHistory) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader)

Example 9 with Histogram

use of net.openhft.chronicle.core.util.Histogram in project Chronicle-Queue by OpenHFT.

the class InternalBenchmarkMain method benchmark.

static void benchmark(int messageSize) {
    Histogram writeTime = new Histogram(32, 7);
    Histogram transportTime = new Histogram(32, 7);
    Histogram readTime = new Histogram(32, 7);
    String path = basePath + "/test-q-" + messageSize;
    ChronicleQueue queue = createQueue(path);
    Thread pretoucher = new Thread(() -> {
        ExcerptAppender appender = queue.acquireAppender();
        Thread thread = Thread.currentThread();
        while (!thread.isInterrupted()) {
            appender.pretouch();
            Jvm.pause(10);
        }
    });
    pretoucher.setDaemon(true);
    pretoucher.start();
    Histogram loopTime = new Histogram();
    Thread reader = new Thread(() -> {
        // try (ChronicleQueue queue2 = createQueue(path))
        ExcerptTailer tailer = queue.createTailer().toEnd();
        long endLoop = System.nanoTime();
        while (running) {
            loopTime.sample((double) (System.nanoTime() - endLoop));
            Jvm.safepoint();
            // readerLoopTime = System.nanoTime();
            // if (readerLoopTime - readerEndLoopTime > 1000)
            // System.out.println("r " + (readerLoopTime - readerEndLoopTime));
            // try {
            runInner(transportTime, readTime, tailer);
            runInner(transportTime, readTime, tailer);
            runInner(transportTime, readTime, tailer);
            runInner(transportTime, readTime, tailer);
            // } finally {
            // readerEndLoopTime = System.nanoTime();
            // }
            Jvm.safepoint();
            endLoop = System.nanoTime();
        }
    });
    reader.start();
    // give the reader time to start
    Jvm.pause(250);
    long next = System.nanoTime();
    long end = (long) (next + runtime * 1e9);
    ExcerptAppender appender = queue.acquireAppender();
    while (end > System.nanoTime()) {
        long start = System.nanoTime();
        try (DocumentContext dc = appender.writingDocument(false)) {
            writeMessage(dc.wire(), messageSize);
        }
        long written = System.nanoTime();
        long time = written - start;
        // System.out.println(time);
        writeTime.sample(time);
        long diff = writeTime.totalCount() - readTime.totalCount();
        Thread.yield();
        if (diff >= 200) {
            // long rlt = readerLoopTime;
            // long delay = System.nanoTime() - rlt;
            System.out.println("diff=" + diff);
            StringBuilder sb = new StringBuilder();
            sb.append("Reader: profile of the thread");
            Jvm.trimStackTrace(sb, reader.getStackTrace());
            System.out.println(sb);
        }
        next += messageSize * 1e9 / (throughput * 1e6);
        long delay = next - System.nanoTime();
        if (delay > 0)
            LockSupport.parkNanos(delay);
    }
    while (readTime.totalCount() < writeTime.totalCount()) Jvm.pause(50);
    pretoucher.interrupt();
    reader.interrupt();
    running = false;
    // monitor.interrupt();
    System.out.println("Loop times " + loopTime.toMicrosFormat());
    System.out.println("messageSize " + messageSize);
    System.out.println("messages " + writeTime.totalCount());
    System.out.println("write histogram: " + writeTime.toMicrosFormat());
    System.out.println("transport histogram: " + transportTime.toMicrosFormat());
    System.out.println("read histogram: " + readTime.toMicrosFormat());
    IOTools.deleteDirWithFiles(path, 2);
    Jvm.pause(1000);
}
Also used : Histogram(net.openhft.chronicle.core.util.Histogram) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 10 with Histogram

use of net.openhft.chronicle.core.util.Histogram in project Chronicle-Queue by OpenHFT.

the class InternalPingPongMain method pingPong.

static void pingPong(int size) {
    String path = InternalPingPongMain.basePath + "/test-q-" + Time.uniqueId();
    Histogram readDelay = new Histogram();
    Histogram readDelay2 = new Histogram();
    try (ChronicleQueue queue = createQueue(path)) {
        Thread reader = new Thread(() -> {
            ExcerptTailer tailer = queue.createTailer();
            while (running.get()) {
                // noinspection StatementWithEmptyBody
                while (readCount.get() == writeCount.get()) ;
                long wakeTime = System.nanoTime();
                while (running.get()) {
                    try (DocumentContext dc = tailer.readingDocument(true)) {
                        if (!dc.isPresent())
                            continue;
                    }
                    break;
                }
                final long delay = wakeTime - writeTime.get();
                final long time = System.nanoTime() - wakeTime;
                readDelay2.sample(time);
                readDelay.sample(delay);
                if (time + delay > 20_000)
                    System.out.println("td " + delay + " + " + time);
                if (readCount.get() == 100000) {
                    System.out.println("reset");
                    readDelay.reset();
                    readDelay2.reset();
                }
                readCount.incrementAndGet();
            }
        });
        reader.setDaemon(true);
        reader.start();
        Jvm.pause(100);
        final long finish = System.currentTimeMillis() + runtime * 1000L;
        final ExcerptAppender appender = queue.acquireAppender();
        while (System.currentTimeMillis() < finish) {
            if (readCount.get() < writeCount.get()) {
                Thread.yield();
                continue;
            }
            try (DocumentContext dc = appender.writingDocument(false)) {
                dc.wire().bytes().writeSkip(size);
            }
            writeCount.incrementAndGet();
            writeTime.set(System.nanoTime());
        }
        running.set(false);
    }
    System.out.println("read delay: " + readDelay.toMicrosFormat());
    System.out.println("read delay2: " + readDelay2.toMicrosFormat());
    IOTools.deleteDirWithFiles(path, 2);
}
Also used : Histogram(net.openhft.chronicle.core.util.Histogram) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Aggregations

Histogram (net.openhft.chronicle.core.util.Histogram)12 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)7 NotNull (org.jetbrains.annotations.NotNull)6 Map (java.util.Map)5 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)5 DocumentContext (net.openhft.chronicle.wire.DocumentContext)5 File (java.io.File)4 LinkedHashMap (java.util.LinkedHashMap)4 TimeUnit (java.util.concurrent.TimeUnit)4 MethodReader (net.openhft.chronicle.bytes.MethodReader)4 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)4 Test (org.junit.Test)4 Jvm (net.openhft.chronicle.core.Jvm)3 IOTools (net.openhft.chronicle.core.io.IOTools)3 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)3 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 AffinityLock (net.openhft.affinity.AffinityLock)2