Java Code Examples for org.agrona.concurrent.IdleStrategy#reset()

The following examples show how to use org.agrona.concurrent.IdleStrategy#reset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReplayIndexTest.java    From artio with Apache License 2.0 6 votes vote down vote up
private int query(
    final long sessionId,
    final int beginSequenceNumber,
    final int beginSequenceIndex,
    final int endSequenceNumber,
    final int endSequenceIndex)
{
    final ReplayOperation operation = query.query(
        sessionId,
        beginSequenceNumber,
        beginSequenceIndex,
        endSequenceNumber,
        endSequenceIndex,
        REPLAY,
        new FixMessageTracker(REPLAY, mockHandler, sessionId));

    final IdleStrategy idleStrategy = CommonConfiguration.backoffIdleStrategy();
    while (!operation.attemptReplay())
    {
        idleStrategy.idle();
    }
    idleStrategy.reset();

    return operation.replayedMessages();
}
 
Example 2
Source File: LoadTestRig.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
void receive()
{
    final MessageTransceiver messageTransceiver = this.messageTransceiver;
    final IdleStrategy idleStrategy = configuration.receiveIdleStrategy();

    long sent = 0;
    long received = 0;
    while (true)
    {
        messageTransceiver.receive();

        final long receivedMessagesCount = receivedMessages;
        if (receivedMessagesCount != received)
        {
            received = receivedMessagesCount;
            idleStrategy.reset();
        }
        else
        {
            idleStrategy.idle();
        }

        if (0 == sent)
        {
            sent = sentMessages;
        }

        if (0 != sent && received >= sent)
        {
            break;
        }
    }
}
 
Example 3
Source File: EmbeddedExclusiveBufferClaimIpcThroughput.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void run()
{
    final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
    final AtomicBoolean running = this.running;
    final Publication publication = this.publication;
    final BufferClaim bufferClaim = new BufferClaim();
    long backPressureCount = 0;
    long totalMessageCount = 0;

    outputResults:
    while (running.get())
    {
        for (int i = 0; i < BURST_LENGTH; i++)
        {
            idleStrategy.reset();
            while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) <= 0)
            {
                ++backPressureCount;
                if (!running.get())
                {
                    break outputResults;
                }
                idleStrategy.idle();
            }

            final int offset = bufferClaim.offset();
            bufferClaim.buffer().putInt(offset, i); // Example field write
            // Real app would write whatever fields are required via a flyweight like SBE

            bufferClaim.commit();

            ++totalMessageCount;
        }
    }

    final double backPressureRatio = backPressureCount / (double)totalMessageCount;
    System.out.format("Publisher back pressure ratio: %f%n", backPressureRatio);
}
 
Example 4
Source File: EmbeddedBufferClaimIpcThroughput.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void run()
{
    final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
    final AtomicBoolean running = this.running;
    final Publication publication = this.publication;
    final BufferClaim bufferClaim = new BufferClaim();
    long backPressureCount = 0;
    long totalMessageCount = 0;

    outputResults:
    while (running.get())
    {
        for (int i = 0; i < BURST_LENGTH; i++)
        {
            idleStrategy.reset();
            while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) <= 0)
            {
                ++backPressureCount;
                if (!running.get())
                {
                    break outputResults;
                }
                idleStrategy.idle();
            }

            final int offset = bufferClaim.offset();
            bufferClaim.buffer().putInt(offset, i); // Example field write
            // Real app would write whatever fields are required via a flyweight like SBE

            bufferClaim.commit();

            ++totalMessageCount;
        }
    }

    final double backPressureRatio = backPressureCount / (double)totalMessageCount;
    System.out.format("Publisher back pressure ratio: %f%n", backPressureRatio);
}
 
Example 5
Source File: EmbeddedRecordingThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public long streamMessagesForRecording()
{
    try (ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        while (!publication.isConnected())
        {
            idleStrategy.idle();
        }

        final long startNs = System.nanoTime();
        final UnsafeBuffer buffer = this.buffer;

        for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
        {
            buffer.putLong(0, i);

            idleStrategy.reset();
            while (publication.offer(buffer, 0, MESSAGE_LENGTH) < 0)
            {
                idleStrategy.idle();
            }
        }

        final long stopPosition = publication.position();
        final CountersReader counters = aeron.countersReader();
        final int counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());

        idleStrategy.reset();
        while (counters.getCounterValue(counterId) < stopPosition)
        {
            idleStrategy.idle();
        }

        final long durationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
        final double dataRate = (stopPosition * 1000.0d / durationMs) / MEGABYTE;
        final double recordingMb = stopPosition / MEGABYTE;
        final long msgRate = (NUMBER_OF_MESSAGES / durationMs) * 1000L;

        System.out.printf(
            "Recorded %.02f MB @ %.02f MB/s - %,d msg/sec - %d byte payload + 32 byte header%n",
            recordingMb, dataRate, msgRate, MESSAGE_LENGTH);

        return RecordingPos.getRecordingId(counters, counterId);
    }
}
 
Example 6
Source File: EmbeddedExclusiveSpiedThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    loadPropertiesFiles(args);

    final RateReporter reporter = new RateReporter(
        TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveSpiedThroughput::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);

    final MediaDriver.Context ctx = new MediaDriver.Context()
        .spiesSimulateConnection(true);

    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Subscription subscription = aeron.addSubscription(CommonContext.SPY_PREFIX + CHANNEL, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(
            rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));

        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();

        do
        {
            System.out.format(
                "%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n",
                NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);

            printingActive = true;

            long backPressureCount = 0;
            for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
            {
                OFFER_BUFFER.putLong(0, i);

                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0)
                {
                    backPressureCount++;
                    idleStrategy.idle();
                }
            }

            System.out.println(
                "Done streaming. backPressureRatio=" + ((double)backPressureCount / NUMBER_OF_MESSAGES));

            if (LINGER_TIMEOUT_MS > 0)
            {
                System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
                Thread.sleep(LINGER_TIMEOUT_MS);
            }

            printingActive = false;
        }
        while (barrier.await());

        running.set(false);
        reporter.halt();
        executor.shutdown();
    }
}
 
Example 7
Source File: EmbeddedExclusiveThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    loadPropertiesFiles(args);

    final RateReporter reporter = new RateReporter(
        TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveThroughput::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);

    try (MediaDriver ignore = MediaDriver.launch();
        Aeron aeron = Aeron.connect();
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(
            rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));

        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();

        do
        {
            System.out.format(
                "%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n",
                NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);

            printingActive = true;

            long backPressureCount = 0;
            for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
            {
                OFFER_BUFFER.putLong(0, i);

                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0)
                {
                    backPressureCount++;
                    idleStrategy.idle();
                }
            }

            System.out.println(
                "Done streaming. backPressureRatio=" + ((double)backPressureCount / NUMBER_OF_MESSAGES));

            if (LINGER_TIMEOUT_MS > 0)
            {
                System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
                Thread.sleep(LINGER_TIMEOUT_MS);
            }

            printingActive = false;
        }
        while (barrier.await());

        running.set(false);
        reporter.halt();
        executor.shutdown();
    }
}
 
Example 8
Source File: EmbeddedThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    loadPropertiesFiles(args);

    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedThroughput::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);

    try (MediaDriver ignore = MediaDriver.launch();
        Aeron aeron = Aeron.connect();
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(
            rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));

        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();

        do
        {
            System.out.format(
                "%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n",
                NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);

            printingActive = true;

            long backPressureCount = 0;
            for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
            {
                OFFER_BUFFER.putLong(0, i);

                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0)
                {
                    backPressureCount++;
                    idleStrategy.idle();
                }
            }

            System.out.println(
                "Done streaming. backPressureRatio=" + ((double)backPressureCount / NUMBER_OF_MESSAGES));

            if (LINGER_TIMEOUT_MS > 0)
            {
                System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
                Thread.sleep(LINGER_TIMEOUT_MS);
            }

            printingActive = false;
        }
        while (barrier.await());

        running.set(false);
        reporter.halt();
        executor.shutdown();
    }
}
 
Example 9
Source File: StreamingPublisher.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    if (MESSAGE_LENGTH < SIZE_OF_LONG)
    {
        throw new IllegalArgumentException("Message length must be at least " + SIZE_OF_LONG + " bytes");
    }

    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final Aeron.Context context = new Aeron.Context();

    if (EMBEDDED_MEDIA_DRIVER)
    {
        context.aeronDirectoryName(driver.aeronDirectoryName());
    }

    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), StreamingPublisher::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(1);

    executor.execute(reporter);

    // Connect to media driver and add publication to send messages on the configured channel and stream ID.
    // The Aeron and Publication classes implement AutoCloseable, and will automatically
    // clean up resources when this try block is finished.
    try (Aeron aeron = Aeron.connect(context);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();

        do
        {
            printingActive = true;

            System.out.format(
                "%nStreaming %,d messages of%s size %d bytes to %s on stream id %d%n",
                NUMBER_OF_MESSAGES,
                RANDOM_MESSAGE_LENGTH ? " random" : "",
                MESSAGE_LENGTH,
                CHANNEL,
                STREAM_ID);

            long backPressureCount = 0;

            for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
            {
                final int length = LENGTH_GENERATOR.getAsInt();

                OFFER_BUFFER.putLong(0, i);
                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, length, null) < 0L)
                {
                    // The offer failed, which is usually due to the publication
                    // being temporarily blocked.  Retry the offer after a short
                    // spin/yield/sleep, depending on the chosen IdleStrategy.
                    backPressureCount++;
                    idleStrategy.idle();
                }

                reporter.onMessage(length);
            }

            System.out.println(
                "Done streaming. Back pressure ratio " + ((double)backPressureCount / NUMBER_OF_MESSAGES));

            if (LINGER_TIMEOUT_MS > 0)
            {
                System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
                Thread.sleep(LINGER_TIMEOUT_MS);
            }

            printingActive = false;
        }
        while (barrier.await());
    }

    reporter.halt();
    executor.shutdown();
    CloseHelper.quietClose(driver);
}