Search in sources :

Example 6 with RateMeasure

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

the class BatchingController method main.

// 
// public void block() {
// while ( packetCounter >= maxRatePerInterval) {
// while( (System.nanoTime() - lastCheck) < interValNanos ) {
// // spin
// }
// lastCheck = System.nanoTime();
// packetCounter -= maxRatePerInterval;
// }
// }
public static void main(String[] arg) {
    // System.out.println(new BatchingController(100));
    // System.out.println(new BatchingController(200));
    // System.out.println(new BatchingController(1000));
    // System.out.println(new BatchingController(2000));
    // System.out.println(new BatchingController(5000));
    // System.out.println(new BatchingController(10000));
    // System.out.println(new BatchingController(15000));
    // System.out.println(new BatchingController(20000));
    // System.out.println(new BatchingController(30000));
    // System.out.println(new BatchingController(50000));
    // System.out.println(new BatchingController(70000));
    // System.out.println(new BatchingController(100000));
    // System.out.println(new BatchingController(500000));
    BatchingController limiter = new BatchingController(10000);
    RateMeasure m = new RateMeasure("msg rate");
    RateMeasure pm = new RateMeasure("packet rate");
    int msgPerPackAssumption = 4;
    int batchCount = 0;
    while (true) {
        // LockSupport.parkNanos(1_000);
        // Sleeper.spinMicros(300);
        Action a = limiter.getAction();
        if (a == Action.NONE) {
            limiter.countPacket();
            pm.count();
            m.count();
        } else if (a == Action.BATCH) {
            if (batchCount == msgPerPackAssumption) {
                limiter.countPacket();
                pm.count();
                batchCount = 0;
            }
            batchCount++;
            m.count();
        } else /*block*/
        {
            int x = 0;
        }
    }
}
Also used : RateMeasure(org.nustaq.fastcast.util.RateMeasure)

Example 7 with RateMeasure

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

the class SendReceive method send.

// @Test
public void send() throws InterruptedException {
    initFC();
    TestMsg template = new TestMsg();
    FSTStructAllocator allocator = new FSTStructAllocator(0);
    TestMsg toSend = allocator.newStruct(template);
    FCPublisher sender = fc.onTransport("default").publish(fc.getPublisherConf("test"));
    toSend.getString().setString("H");
    RateMeasure measure = new RateMeasure("msg send " + toSend.getByteSize());
    final Bytez base = toSend.getBase();
    final int byteSize = toSend.getByteSize();
    final long offset = toSend.getOffset();
    long tim = System.currentTimeMillis();
    while (true) {
        Sleeper.spinMicros(50);
        toSend.setTimeNanos(System.nanoTime());
        while (!sender.offer(null, base, offset, byteSize, true)) {
        // System.out.println("offer rejected !");
        }
        measure.count();
        // System.out.println("sent msg");
        if (System.currentTimeMillis() - tim > 5000) {
            break;
        }
    }
}
Also used : Bytez(org.nustaq.offheap.bytez.Bytez) RateMeasure(org.nustaq.fastcast.util.RateMeasure) FSTStructAllocator(org.nustaq.offheap.structs.FSTStructAllocator)

Example 8 with RateMeasure

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

the class StructSubscriber method main.

public static void main(String[] arg) {
    Protocol.initStructFactory();
    // 5 chars MAX !!
    FastCast.getFastCast().setNodeId("SUB");
    StructPublisher.configureFastCast();
    final RateMeasure rateMeasure = new RateMeasure("receive rate");
    FastCast.getFastCast().onTransport("default").subscribe(new SubscriberConf(1).receiveBufferPackets(33_000), new FCSubscriber() {

        PriceUpdateStruct msg = FSTStructFactory.getInstance().createEmptyStructPointer(PriceUpdateStruct.class);

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            msg.baseOn(b, (int) off);
            rateMeasure.count();
        // instanceof'ing in case of various messages
        // Class msgStruct = msg.getPointedClass(); otherStructClass = msg.detachTo(otherStructClass);
        }

        @Override
        public boolean dropped() {
            System.out.println("fatal, could not keep up. exiting");
            System.exit(0);
            return false;
        }

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

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
        }
    });
}
Also used : Bytez(org.nustaq.offheap.bytez.Bytez) RateMeasure(org.nustaq.fastcast.util.RateMeasure) SubscriberConf(org.nustaq.fastcast.config.SubscriberConf) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Example 9 with RateMeasure

use of org.nustaq.fastcast.util.RateMeasure 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)

Example 10 with RateMeasure

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

the class AsyncSubStruct method initFastCast.

public void initFastCast() throws Exception {
    fastCast = FastCast.getFastCast();
    fastCast.setNodeId("SUB");
    fastCast.loadConfig(AsyncLatPublisher.CFG_FILE_PATH);
    backPub = fastCast.onTransport("back").publish("back");
    // pointer to message
    final FSTStruct msg = FSTStructFactory.getInstance().createEmptyStructPointer(FSTStruct.class);
    final RateMeasure measure = new RateMeasure("receive rate");
    fastCast.onTransport("default").subscribe("stream", new FCSubscriber() {

        int count = 0;

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, final int len) {
            measure.count();
            if (++count == 10 || len < 30) /*"END" marker*/
            {
                count = 0;
                final byte[] copy = pool.getBA();
                if (// prevent segfault :) !
                len < copy.length) {
                    b.getArr(off, copy, 0, len);
                    count = 0;
                    // do bounce back in different thread, else blocking on send will pressure back to
                    // sender resulting in whacky behaviour+throughput
                    bounceBackExec.execute(new Runnable() {

                        @Override
                        public void run() {
                            while (!backPub.offer(null, copy, 0, len, true)) {
                            // spin
                            }
                            // give back to pool
                            pool.returnBA(copy);
                        }
                    });
                } else {
                    throw new RuntimeException("was soll das ?");
                }
            }
        }

        @Override
        public boolean dropped() {
            // fatal, exit
            System.out.println("process dropped ");
            System.exit(-1);
            return false;
        }

        @Override
        public void senderTerminated(String senderNodeId) {
        }

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
        }
    });
}
Also used : FSTStruct(org.nustaq.offheap.structs.FSTStruct) Bytez(org.nustaq.offheap.bytez.Bytez) RateMeasure(org.nustaq.fastcast.util.RateMeasure) StructString(org.nustaq.offheap.structs.structtypes.StructString) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Aggregations

RateMeasure (org.nustaq.fastcast.util.RateMeasure)13 FCPublisher (org.nustaq.fastcast.api.FCPublisher)4 Bytez (org.nustaq.offheap.bytez.Bytez)4 FCSubscriber (org.nustaq.fastcast.api.FCSubscriber)3 ObjectPublisher (org.nustaq.fastcast.api.util.ObjectPublisher)3 PublisherConf (org.nustaq.fastcast.config.PublisherConf)3 FSTStruct (org.nustaq.offheap.structs.FSTStruct)3 FSTStructAllocator (org.nustaq.offheap.structs.FSTStructAllocator)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 SubscriberConf (org.nustaq.fastcast.config.SubscriberConf)2 StructString (org.nustaq.offheap.structs.structtypes.StructString)2 File (java.io.File)1 FastCast (org.nustaq.fastcast.api.FastCast)1 ObjectSubscriber (org.nustaq.fastcast.api.util.ObjectSubscriber)1 Sleeper (org.nustaq.fastcast.util.Sleeper)1