Search in sources :

Example 1 with MessageHistory

use of net.openhft.chronicle.wire.MessageHistory in project Chronicle-Queue by OpenHFT.

the class OrderManagerTest method testWithQueueHistory.

/*
    When all the tests are run gets
[main] WARN net.openhft.chronicle.queue.micros.OrderManagerTest$$Lambda$478/807322507 - Failure to dispatch message: onOrder [!Order {
  symbol: EURUSD,
  side: Buy,
  limitPrice: 1.1167,
  quantity: 1E6
}
]
java.lang.AssertionError: expected:<4> but was:<7>
	at org.junit.Assert.fail(Assert.java:88)
	at org.junit.Assert.failNotEquals(Assert.java:834)
	at org.junit.Assert.assertEquals(Assert.java:645)
	at org.junit.Assert.assertEquals(Assert.java:631)
	at net.openhft.chronicle.queue.micros.OrderManagerTest.lambda$testWithQueueHistory$0(OrderManagerTest.java:163)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.openhft.chronicle.wire.MethodReader.invoke(MethodReader.java:284)
	at net.openhft.chronicle.wire.MethodReader.lambda$addParseletForMethod$3(MethodReader.java:164)
	at net.openhft.chronicle.wire.WireParser.parseOne(WireParser.java:41)
	at net.openhft.chronicle.wire.WireParser.accept(WireParser.java:49)
	at net.openhft.chronicle.wire.MethodReader.readOne(MethodReader.java:311)
	at net.openhft.chronicle.queue.micros.OrderManagerTest.testWithQueueHistory(OrderManagerTest.java:165)
     */
@Ignore("TODO FIX")
@Test
public void testWithQueueHistory() {
    File queuePath = new File(OS.TARGET, "testWithQueueHistory-" + System.nanoTime());
    File queuePath2 = new File(OS.TARGET, "testWithQueueHistory-down-" + System.nanoTime());
    try {
        try (SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(queuePath).testBlockSize().build()) {
            OrderIdeaListener orderManager = out.acquireAppender().methodWriterBuilder(OrderIdeaListener.class).addInterface(MarketDataListener.class).recordHistory(true).get();
            SidedMarketDataCombiner combiner = new SidedMarketDataCombiner((MarketDataListener) orderManager);
            // events in
            // not expected to trigger
            orderManager.onOrderIdea(new OrderIdea("EURUSD", Side.Buy, 1.1180, 2e6));
            combiner.onSidedPrice(new SidedPrice("EURUSD", 123456789000L, Side.Sell, 1.1172, 2e6));
            combiner.onSidedPrice(new SidedPrice("EURUSD", 123456789100L, Side.Buy, 1.1160, 2e6));
            combiner.onSidedPrice(new SidedPrice("EURUSD", 123456789100L, Side.Buy, 1.1167, 2e6));
            // expected to trigger
            orderManager.onOrderIdea(new OrderIdea("EURUSD", Side.Buy, 1.1165, 1e6));
        }
        try (SingleChronicleQueue in = SingleChronicleQueueBuilder.binary(queuePath).testBlockSize().sourceId(1).build();
            SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(queuePath2).testBlockSize().build()) {
            OrderListener listener = out.acquireAppender().methodWriterBuilder(OrderListener.class).recordHistory(true).get();
            // build our scenario
            OrderManager orderManager = new OrderManager(listener);
            MethodReader reader = in.createTailer().methodReader(orderManager);
            for (int i = 0; i < 5; i++) assertTrue(reader.readOne());
            assertFalse(reader.readOne());
        // System.out.println(out.dump());
        }
        try (SingleChronicleQueue in = SingleChronicleQueueBuilder.binary(queuePath2).testBlockSize().sourceId(2).build()) {
            MethodReader reader = in.createTailer().methodReader((OrderListener) order -> {
                MessageHistory x = MessageHistory.get();
                System.out.println(x);
                assertEquals(1, x.sourceId(0));
                assertEquals(2, x.sourceId(1));
                assertEquals(4, x.timings());
            });
            assertTrue(reader.readOne());
            assertFalse(reader.readOne());
        }
    } finally {
        try {
            IOTools.shallowDeleteDirWithFiles(queuePath);
            IOTools.shallowDeleteDirWithFiles(queuePath2);
        } catch (Exception e) {
        }
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) MessageHistory(net.openhft.chronicle.wire.MessageHistory) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) File(java.io.File) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) Ignore(org.junit.Ignore) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) IOTools(net.openhft.chronicle.core.io.IOTools) RollCycles(net.openhft.chronicle.queue.RollCycles) OS(net.openhft.chronicle.core.OS) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Assert(org.junit.Assert) MessageHistory(net.openhft.chronicle.wire.MessageHistory) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with MessageHistory

use of net.openhft.chronicle.wire.MessageHistory in project Chronicle-Queue by OpenHFT.

the class ChronicleHistoryReaderTest method doTest.

private void doTest(boolean recordHistoryFirst) {
    VanillaMessageHistory veh = new VanillaMessageHistory();
    veh.addSourceDetails(true);
    MessageHistory.set(veh);
    int extraTiming = recordHistoryFirst ? 1 : 0;
    long nanoTime = System.nanoTime();
    File queuePath = new File(OS.TARGET, "testWithQueueHistory-" + nanoTime);
    File queuePath2 = new File(OS.TARGET, "testWithQueueHistory2-" + nanoTime);
    File queuePath3 = new File(OS.TARGET, "testWithQueueHistory3-" + nanoTime);
    try {
        try (SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(queuePath).testBlockSize().sourceId(1).build()) {
            DummyListener writer = out.acquireAppender().methodWriterBuilder(DummyListener.class).recordHistory(recordHistoryFirst).get();
            writer.say("hello");
        }
        try (SingleChronicleQueue in = SingleChronicleQueueBuilder.binary(queuePath).testBlockSize().sourceId(1).build();
            SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(queuePath2).testBlockSize().build()) {
            DummyListener writer = out.acquireAppender().methodWriterBuilder(DummyListener.class).recordHistory(true).get();
            DummyListener dummy = msg -> {
                MessageHistory history = MessageHistory.get();
                Assert.assertEquals(1, history.sources());
                // written 1st then received by me
                Assert.assertEquals(1 + extraTiming, history.timings());
                writer.say(msg);
            };
            MethodReader reader = in.createTailer().methodReader(dummy);
            assertTrue(reader.readOne());
            assertFalse(reader.readOne());
        }
        try (SingleChronicleQueue in = SingleChronicleQueueBuilder.binary(queuePath2).testBlockSize().sourceId(2).build();
            SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(queuePath3).testBlockSize().build()) {
            DummyListener writer = out.acquireAppender().methodWriterBuilder(DummyListener.class).recordHistory(true).get();
            DummyListener dummy = msg -> {
                MessageHistory history = MessageHistory.get();
                Assert.assertEquals(2, history.sources());
                Assert.assertEquals(3 + extraTiming, history.timings());
                writer.say(msg);
            };
            MethodReader reader = in.createTailer().methodReader(dummy);
            assertTrue(reader.readOne());
            assertFalse(reader.readOne());
        }
        ChronicleHistoryReader chronicleHistoryReader = new ChronicleHistoryReader().withBasePath(queuePath3.toPath()).withTimeUnit(TimeUnit.MICROSECONDS);
        Map<String, Histogram> histos = chronicleHistoryReader.readChronicle();
        chronicleHistoryReader.withMessageSink(System.out::println);
        chronicleHistoryReader.outputData();
        if (recordHistoryFirst) {
            Assert.assertEquals(5, histos.size());
            Assert.assertEquals("[1, startTo1, 2, 1to2, endToEnd]", histos.keySet().toString());
        } else {
            Assert.assertEquals(4, histos.size());
            Assert.assertEquals("[1, 2, 1to2, endToEnd]", histos.keySet().toString());
        }
    } finally {
        try {
            IOTools.shallowDeleteDirWithFiles(queuePath);
            IOTools.shallowDeleteDirWithFiles(queuePath2);
            IOTools.shallowDeleteDirWithFiles(queuePath3);
        } catch (Exception e) {
        }
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) MessageHistory(net.openhft.chronicle.wire.MessageHistory) Histogram(net.openhft.chronicle.core.util.Histogram) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) VanillaMessageHistory(net.openhft.chronicle.wire.VanillaMessageHistory) IOTools(net.openhft.chronicle.core.io.IOTools) Assert.assertFalse(org.junit.Assert.assertFalse) Map(java.util.Map) Assume(org.junit.Assume) OS(net.openhft.chronicle.core.OS) Assert(org.junit.Assert) Histogram(net.openhft.chronicle.core.util.Histogram) MessageHistory(net.openhft.chronicle.wire.MessageHistory) VanillaMessageHistory(net.openhft.chronicle.wire.VanillaMessageHistory) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) VanillaMessageHistory(net.openhft.chronicle.wire.VanillaMessageHistory) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader)

Example 3 with MessageHistory

use of net.openhft.chronicle.wire.MessageHistory in project Chronicle-Queue by OpenHFT.

the class ChronicleHistoryReader method processMessage.

protected void processMessage(CharSequence methodName, MessageHistory history) {
    CharSequence extraHistoId = histosByMethod ? ("_" + methodName) : "";
    long lastTime = 0;
    // if the tailer has recordHistory(true) then the MessageHistory will be
    // written with a single timing and nothing else. This is then carried through
    int firstWriteOffset = history.timings() - (history.sources() * 2);
    if (!(firstWriteOffset == 0 || firstWriteOffset == 1))
        // don't know how this can happen, but there is at least one CQ that exhibits it
        return;
    for (int sourceIndex = 0; sourceIndex < history.sources(); sourceIndex++) {
        String histoId = Integer.toString(history.sourceId(sourceIndex)) + extraHistoId;
        Histogram histo = histos.computeIfAbsent(histoId, s -> histogram());
        long receivedByThisComponent = history.timing((2 * sourceIndex) + firstWriteOffset);
        long processedByThisComponent = history.timing((2 * sourceIndex) + firstWriteOffset + 1);
        histo.sample(processedByThisComponent - receivedByThisComponent);
        if (lastTime == 0 && firstWriteOffset > 0) {
            Histogram histo1 = histos.computeIfAbsent("startTo" + histoId, s -> histogram());
            histo1.sample(receivedByThisComponent - history.timing(0));
        } else if (lastTime != 0) {
            Histogram histo1 = histos.computeIfAbsent(Integer.toString(history.sourceId(sourceIndex - 1)) + "to" + histoId, s -> histogram());
            // here we are comparing System.nanoTime across processes. YMMV
            histo1.sample(receivedByThisComponent - lastTime);
        }
        lastTime = processedByThisComponent;
    }
    if (history.sources() > 1) {
        Histogram histoE2E = histos.computeIfAbsent("endToEnd", s -> histogram());
        histoE2E.sample(history.timing(history.timings() - 1) - history.timing(0));
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) MessageHistory(net.openhft.chronicle.wire.MessageHistory) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Histogram(net.openhft.chronicle.core.util.Histogram) Files(java.nio.file.Files) Collectors(java.util.stream.Collectors) LinkedHashMap(java.util.LinkedHashMap) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) VanillaMessageHistory(net.openhft.chronicle.wire.VanillaMessageHistory) Map(java.util.Map) WireParselet(net.openhft.chronicle.wire.WireParselet) NotNull(org.jetbrains.annotations.NotNull) Path(java.nio.file.Path) VanillaMethodReader(net.openhft.chronicle.wire.VanillaMethodReader) Histogram(net.openhft.chronicle.core.util.Histogram)

Aggregations

MethodReader (net.openhft.chronicle.bytes.MethodReader)3 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)3 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)3 MessageHistory (net.openhft.chronicle.wire.MessageHistory)3 File (java.io.File)2 Map (java.util.Map)2 TimeUnit (java.util.concurrent.TimeUnit)2 OS (net.openhft.chronicle.core.OS)2 IOTools (net.openhft.chronicle.core.io.IOTools)2 Histogram (net.openhft.chronicle.core.util.Histogram)2 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)2 VanillaMessageHistory (net.openhft.chronicle.wire.VanillaMessageHistory)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 LinkedHashMap (java.util.LinkedHashMap)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)1