Search in sources :

Example 11 with RateMeasure

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

the class ProgrammaticConfiguredPublisher 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 long packets are kept for retransmission requests
    40_000).pps(// packets per second rate limit. So max traffic for topic = 5000*2500 = 12.5 MB/second
    5000));
    // could directly send raw on publisher
    // while( ! pub.offer(..) ) { /* spin */ }
    // or use a helper for fast-serialized messages
    ObjectPublisher opub = new ObjectPublisher(pub);
    RateMeasure measure = new RateMeasure("msg/s");
    while (true) {
        measure.count();
        opub.sendObject(// all listeners should receive (by specifying a nodeId, a specific subscriber can be targeted)
        null, // serializable object
        "Hello " + System.currentTimeMillis(), // allow for 'batching' several messages into one (will create slight latency)
        false);
    }
}
Also used : FCPublisher(org.nustaq.fastcast.api.FCPublisher) ObjectPublisher(org.nustaq.fastcast.api.util.ObjectPublisher) RateMeasure(org.nustaq.fastcast.util.RateMeasure) PublisherConf(org.nustaq.fastcast.config.PublisherConf)

Example 12 with RateMeasure

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

the class AsyncLatPublisher method run.

public void run(int pauseNanos, int numEvents) throws Throwable {
    RateMeasure report = new RateMeasure("send rate");
    AsyncLatMessage event = new AsyncLatMessage(System.nanoTime(), 0, 0 + 1, 10, 110);
    // int msgCount = 0;
    for (int i = 0; i < numEvents; i++) {
        double bidPrc = Math.random() * 10;
        event.setBidPrc(bidPrc);
        event.setAskPrc(bidPrc + 1);
        event.setSendTimeStampNanos(System.nanoTime());
        pub.sendObject(null, event, true);
        report.count();
        long time = System.nanoTime();
        while (System.nanoTime() - time < pauseNanos) {
        // spin
        }
    // msgCount++;
    // if ( (msgCount%10000) == 0 ) {
    // System.out.println("count "+msgCount);
    // }
    }
    pub.sendObject(null, "END", true);
}
Also used : RateMeasure(org.nustaq.fastcast.util.RateMeasure)

Example 13 with RateMeasure

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

the class MPSubscriber method main.

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

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

        int count = 0;

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            rateMeasure.count();
            msg.baseOn(b, (int) off);
            Class type = msg.getPointedClass();
            if (type == AMessage.class) {
                AMessage am = msg.cast();
            // am is valid until another pointer is used. use "am = am.detach" in order to get a non-cached pointer
            // note msg data is valid only inside this method (reused). You need to copy data in case processing should
            // be done in a different thread (usually do basic filtering in-thread, then do processing on a dedicated thread)
            } else if (type == OtherMessage.class) {
                OtherMessage om = msg.cast();
            // ..
            } else if (type == ComposedMessage.class) {
                ComposedMessage cm = msg.cast();
                if (count++ == 500_000) {
                    System.out.println("Other" + cm);
                    count = 0;
                }
            }
        }

        @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 : FSTStruct(org.nustaq.offheap.structs.FSTStruct) Bytez(org.nustaq.offheap.bytez.Bytez) RateMeasure(org.nustaq.fastcast.util.RateMeasure) SubscriberConf(org.nustaq.fastcast.config.SubscriberConf) 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