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);
}
}
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);
}
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);
}
});
}
Aggregations