Search in sources :

Example 1 with Sleeper

use of org.nustaq.fastcast.util.Sleeper in project fast-cast by RuedigerMoeller.

the class FCPing method runPingClientSync.

// CO !
public void runPingClientSync() throws InterruptedException {
    final FastCast fc = initFC("pclie", "pingponglat.kson");
    final FCPublisher pingserver = fc.onTransport("ping").publish(fc.getPublisherConf("pingtopic"));
    final Executor ex = Executors.newSingleThreadExecutor();
    final AtomicInteger await = new AtomicInteger(0);
    fc.onTransport(PING_BACK_ON_SAME_TOPIC ? "ping" : "pong").subscribe(fc.getSubscriberConf("pongtopic"), new FCSubscriber() {

        String pongName;

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            await.decrementAndGet();
        }

        @Override
        public boolean dropped() {
            // reset
            await.set(0);
            System.out.println("Drop and Reset counter !");
            return true;
        }

        @Override
        public void senderTerminated(String senderNodeId) {
            System.out.println(senderNodeId + " terminated");
        }

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
            pongName = receivesFrom;
        }
    });
    // just create a byte[] for each struct (*)
    FSTStructAllocator alloc = new FSTStructAllocator(0);
    PingRequest pr = alloc.newStruct(new PingRequest());
    Histogram histo = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    // wait for at least one heartbeat
    Thread.sleep(1000);
    System.out.println("starting ping pong " + randomId);
    System.gc();
    pr.setNanoSendTime(randomId);
    Sleeper sl = new Sleeper();
    while (true) {
        await.set(0);
        for (int i = 0; i < NUM_MSG; i++) {
            //                sl.sleepMicros(100);
            pingAndAwaitPong(pingserver, await, pr, histo, i);
        }
        histo.outputPercentileDistribution(System.out, 1000.0);
        histo.reset();
        System.gc();
    }
}
Also used : Histogram(org.HdrHistogram.Histogram) FCPublisher(org.nustaq.fastcast.api.FCPublisher) Bytez(org.nustaq.offheap.bytez.Bytez) Sleeper(org.nustaq.fastcast.util.Sleeper) FSTStructAllocator(org.nustaq.offheap.structs.FSTStructAllocator) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber) Executor(java.util.concurrent.Executor) FastCast(org.nustaq.fastcast.api.FastCast) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 2 with Sleeper

use of org.nustaq.fastcast.util.Sleeper in project fast-cast by RuedigerMoeller.

the class FCPing method runPingClientASync.

// no coordinated ommission, async
public void runPingClientASync() throws InterruptedException {
    final FastCast fc = initFC("pclie", "pingponglat.kson");
    final FCPublisher pingserver = fc.onTransport("ping").publish(fc.getPublisherConf("pingtopic"));
    final Executor ex = Executors.newSingleThreadExecutor();
    final Histogram histo = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    fc.onTransport(PING_BACK_ON_SAME_TOPIC ? "ping" : "pong").subscribe(fc.getSubscriberConf("pongtopic"), new FCSubscriber() {

        int msgCount;

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            // decode bounced back ping request
            PingRequest received = FSTStructFactory.getInstance().getStructPointer(b, off).cast();
            if (// filter out pressure of printing histogram
            msgCount > 10_000)
                histo.recordValue(System.nanoTime() - received.getNanoSendTime());
            msgCount++;
            if (msgCount > NUM_MSG + 10_000) {
                histo.outputPercentileDistribution(System.out, 1000.0);
                histo.reset();
                msgCount = 0;
            }
        }

        @Override
        public boolean dropped() {
            return true;
        }

        @Override
        public void senderTerminated(String senderNodeId) {
            System.out.println(senderNodeId + " terminated");
        }

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
        }
    });
    // just create a byte[] for each struct (*)
    FSTStructAllocator alloc = new FSTStructAllocator(0);
    PingRequest pr = alloc.newStruct(new PingRequest());
    Sleeper sl = new Sleeper();
    while (true) {
        // need rate limiting cause of async
        sl.spinMicros(200);
        pr.setNanoSendTime(System.nanoTime());
        // can be sure off is 0, see (*)
        pingserver.offer(null, pr.getBase(), true);
    }
}
Also used : Histogram(org.HdrHistogram.Histogram) FCPublisher(org.nustaq.fastcast.api.FCPublisher) Bytez(org.nustaq.offheap.bytez.Bytez) Sleeper(org.nustaq.fastcast.util.Sleeper) FSTStructAllocator(org.nustaq.offheap.structs.FSTStructAllocator) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber) Executor(java.util.concurrent.Executor) FastCast(org.nustaq.fastcast.api.FastCast)

Example 3 with Sleeper

use of org.nustaq.fastcast.util.Sleeper in project fast-cast by RuedigerMoeller.

the class SendReceive method testSleeper.

public void testSleeper() {
    RateMeasure rm = new RateMeasure("ticks ps");
    Sleeper sl = new Sleeper();
    while (true) {
        sl.spinMicros(80);
        rm.count();
    }
}
Also used : RateMeasure(org.nustaq.fastcast.util.RateMeasure) Sleeper(org.nustaq.fastcast.util.Sleeper)

Aggregations

Sleeper (org.nustaq.fastcast.util.Sleeper)3 Executor (java.util.concurrent.Executor)2 Histogram (org.HdrHistogram.Histogram)2 FCPublisher (org.nustaq.fastcast.api.FCPublisher)2 FCSubscriber (org.nustaq.fastcast.api.FCSubscriber)2 FastCast (org.nustaq.fastcast.api.FastCast)2 Bytez (org.nustaq.offheap.bytez.Bytez)2 FSTStructAllocator (org.nustaq.offheap.structs.FSTStructAllocator)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RateMeasure (org.nustaq.fastcast.util.RateMeasure)1