Search in sources :

Example 1 with RateMeasure

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

the class AsyncLatReceiver method initFastCast.

public void initFastCast() throws Exception {
    fastCast = FastCast.getFastCast();
    fastCast.setNodeId("SUB");
    fastCast.loadConfig(AsyncLatPublisher.CFG_FILE_PATH);
    backPub = new ObjectPublisher(fastCast.onTransport("back").publish("back"), AsyncLatMessage.class);
    final RateMeasure measure = new RateMeasure("receive rate");
    fastCast.onTransport("default").subscribe("stream", new ObjectSubscriber(AsyncLatMessage.class) {

        int count = 0;

        @Override
        protected void objectReceived(String s, long l, Object o) {
            if ("END".equals(o)) {
                backPub.sendObject(null, o, true);
                return;
            }
            AsyncLatReceiver.this.objectReceived(s, l, o);
            if (++count == 10) {
                // backtalk only 10%
                backPub.sendObject(null, o, true);
                count = 0;
            }
            measure.count();
        }

        @Override
        public boolean dropped() {
            System.exit(-2);
            return false;
        }
    });
}
Also used : ObjectPublisher(org.nustaq.fastcast.api.util.ObjectPublisher) RateMeasure(org.nustaq.fastcast.util.RateMeasure) ObjectSubscriber(org.nustaq.fastcast.api.util.ObjectSubscriber)

Example 2 with RateMeasure

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

the class AsyncPubStruct method run.

public void run(int pauseNanos, int numEvents) throws Throwable {
    RateMeasure report = new RateMeasure("send rate");
    // alloc + init a single instance which is reused then. if no allocator is used, default is OnHeap Alloc (struct is byte[] backed)
    AsyncLatMessageStruct event = (AsyncLatMessageStruct) new AsyncLatMessageStruct(System.nanoTime(), 0, 0 + 1, 10, 110).toOffHeap();
    byte[] underlying = event.getBase().asByteArray();
    byte[] end = new StructString("END").toOffHeap().getBase().asByteArray();
    for (int i = 0; i < numEvents; i++) {
        double bidPrc = Math.random() * 10;
        event.setBidPrc(bidPrc);
        event.setAskPrc(bidPrc + 1);
        event.setSendTimeStampNanos(System.nanoTime());
        while (!pub.offer(null, underlying, 0, underlying.length, true)) {
        // spin
        }
        report.count();
        long time = System.nanoTime();
        while (System.nanoTime() - time < pauseNanos) {
        // spin
        }
    }
    while (!pub.offer(null, end, 0, end.length, true)) {
    // spin
    }
}
Also used : StructString(org.nustaq.offheap.structs.structtypes.StructString) RateMeasure(org.nustaq.fastcast.util.RateMeasure)

Example 3 with RateMeasure

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

the class MPPublisher method main.

public static void main(String[] arg) {
    // 5 chars MAX !!
    FastCast.getFastCast().setNodeId("MPUB");
    configureFastCast();
    FCPublisher pub = FastCast.getFastCast().onTransport("default").publish(// unique-per-transport topic id
    new PublisherConf(1).numPacketHistory(// how many packets are kept for retransmission requests
    33_000).pps(// packets per second rate limit.
    10_000));
    MultipleProtocol.initStructFactory();
    FSTStructAllocator onHeapAlloc = new FSTStructAllocator(10_000);
    AMessage aMsg = onHeapAlloc.newStruct(new AMessage());
    OtherMessage other = onHeapAlloc.newStruct(new OtherMessage());
    ComposedMessage composed = onHeapAlloc.newStruct(new ComposedMessage());
    ThreadLocalRandom random = ThreadLocalRandom.current();
    // could directly send raw on publisher
    RateMeasure measure = new RateMeasure("msg/s");
    int count = 0;
    FSTStruct msg = null;
    while (true) {
        measure.count();
        // fill in data
        switch(random.nextInt(3)) {
            case 0:
                for (int i = 0; i < aMsg.stringArrayLen(); i++) {
                    aMsg.stringArray(i).setString("Hello " + i);
                }
                aMsg.setL(count++);
                msg = aMsg;
                break;
            case 1:
                other.setQuantity(count++);
                other.setValue(random.nextDouble());
                msg = other;
                break;
            case 2:
                aMsg.stringArray(0).setString("Hello !");
                aMsg.stringArray(1).setString("Hello !!");
                aMsg.setL(count++);
                other.setQuantity(count++);
                other.setValue(random.nextDouble());
                // does a copy !
                composed.setMegA(aMsg);
                // does a copy !
                composed.setMsgB(other);
                msg = composed;
                break;
        }
        // send message
        while (!pub.offer(null, msg.getBase(), msg.getOffset(), msg.getByteSize(), false)) {
        /* spin */
        }
    }
}
Also used : FSTStruct(org.nustaq.offheap.structs.FSTStruct) FCPublisher(org.nustaq.fastcast.api.FCPublisher) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RateMeasure(org.nustaq.fastcast.util.RateMeasure) PublisherConf(org.nustaq.fastcast.config.PublisherConf) FSTStructAllocator(org.nustaq.offheap.structs.FSTStructAllocator)

Example 4 with RateMeasure

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

the class StructPublisher method main.

public static void main(String[] arg) {
    // 5 chars MAX !!
    FastCast.getFastCast().setNodeId("PUB");
    configureFastCast();
    FCPublisher pub = FastCast.getFastCast().onTransport("default").publish(// unique-per-transport topic id
    new PublisherConf(1).numPacketHistory(// how many packets are kept for retransmission requests
    33_000).pps(// packets per second rate limit.
    10_000));
    Protocol.initStructFactory();
    Protocol.PriceUpdateStruct template = new Protocol.PriceUpdateStruct();
    FSTStructAllocator onHeapAlloc = new FSTStructAllocator(0);
    // speed up instantiation
    Protocol.PriceUpdateStruct msg = onHeapAlloc.newStruct(template);
    ThreadLocalRandom current = ThreadLocalRandom.current();
    // could directly send raw on publisher
    RateMeasure measure = new RateMeasure("msg/s");
    while (true) {
        measure.count();
        // fill in data
        Protocol.InstrumentStruct instrument = msg.getInstrument();
        instrument.getMnemonic().setString("BMW");
        instrument.setInstrumentId(13);
        msg.setPrc(99.0 + current.nextDouble(10.0) - 5);
        msg.setQty(100 + current.nextInt(10));
        // send message
        while (!pub.offer(null, msg.getBase(), msg.getOffset(), msg.getByteSize(), false)) {
        /* spin */
        }
    }
}
Also used : FCPublisher(org.nustaq.fastcast.api.FCPublisher) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RateMeasure(org.nustaq.fastcast.util.RateMeasure) PublisherConf(org.nustaq.fastcast.config.PublisherConf) FSTStructAllocator(org.nustaq.offheap.structs.FSTStructAllocator)

Example 5 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)

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