use of org.nustaq.offheap.structs.FSTStructAllocator 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();
}
}
use of org.nustaq.offheap.structs.FSTStructAllocator 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);
}
}
use of org.nustaq.offheap.structs.FSTStructAllocator 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 */
}
}
}
use of org.nustaq.offheap.structs.FSTStructAllocator 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 */
}
}
}
use of org.nustaq.offheap.structs.FSTStructAllocator 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;
}
}
}
Aggregations