use of org.nustaq.kontraktor.util.RateMeasure in project kontraktor by RuedigerMoeller.
the class RxJava method rxToJ8StreamTest.
@Test
public void rxToJ8StreamTest() {
Observable<Integer> range = Observable.range(0, NUM_MSG / 4);
Publisher<Integer> pub = RxReactiveStreams.toPublisher(range);
AtomicInteger count = new AtomicInteger(0);
RateMeasure rm = new RateMeasure("events");
Promise<Integer> finished = new Promise<>();
KxReactiveStreams.get().asKxPublisher(pub).async().stream(stream -> {
stream.forEach(i -> {
rm.count();
count.incrementAndGet();
});
finished.resolve(count.get());
});
Assert.assertTrue(finished.await(50000).intValue() == NUM_MSG / 4);
}
use of org.nustaq.kontraktor.util.RateMeasure in project kontraktor by RuedigerMoeller.
the class Basics method testStopThreadsError.
@Test
public void testStopThreadsError() throws InterruptedException {
resetThreadCount();
EventSink<Integer> eventSink = new EventSink<Integer>();
AtomicInteger sum = new AtomicInteger(0);
AtomicInteger elems = new AtomicInteger(0);
RateMeasure ms = new RateMeasure("rate");
eventSink.syncMap(i -> i * i).map(i -> "" + i).syncMap(s -> s.substring(0, s.length() / 2)).map(s -> s.length()).subscribe((len, err) -> {
ms.count();
if (len != null) {
// complete signal
elems.incrementAndGet();
sum.addAndGet(len);
}
});
for (int i = 0; i < MASMSG_NUM; i++) {
while (!eventSink.offer(i)) {
Thread.yield();
}
}
eventSink.error(new RuntimeException("error"));
System.out.println("count:" + DispatcherThread.activeDispatchers.get());
int cnt = 0;
while (sum.get() != 94803201 && cnt < 10) {
Thread.sleep(1000);
cnt++;
}
System.out.println("count:" + DispatcherThread.activeDispatchers.get());
Thread.sleep(1000);
System.out.println("elems:" + elems.get());
Thread.sleep(5000);
Assert.assertTrue(elems.get() == MASMSG_NUM);
System.out.println("disp:" + DispatcherThread.activeDispatchers.get());
Assert.assertTrue(DispatcherThread.activeDispatchers.get() == 0);
}
use of org.nustaq.kontraktor.util.RateMeasure in project kontraktor by RuedigerMoeller.
the class Basics method testStopThreadsWithIter.
@Test
public void testStopThreadsWithIter() throws InterruptedException {
resetThreadCount();
AtomicInteger sum = new AtomicInteger(0);
AtomicInteger elems = new AtomicInteger(0);
RateMeasure ms = new RateMeasure("rate");
KxReactiveStreams.get().produce(IntStream.range(0, MASMSG_NUM)).syncMap(i -> i * i).map(i -> "" + i).syncMap(s -> s.substring(0, s.length() / 2)).map(s -> s.length()).subscribe((len, err) -> {
ms.count();
if (len != null) {
// complete signal
sum.addAndGet(len);
elems.incrementAndGet();
}
});
System.out.println("count:" + DispatcherThread.activeDispatchers.get());
int cnt = 0;
while (sum.get() != 94803201 && cnt < 10) {
Thread.sleep(1000);
cnt++;
}
System.out.println("count:" + DispatcherThread.activeDispatchers.get());
Thread.sleep(5000);
Assert.assertTrue(elems.get() == MASMSG_NUM);
Assert.assertTrue(DispatcherThread.activeDispatchers.get() == 0);
}
use of org.nustaq.kontraktor.util.RateMeasure in project kontraktor by RuedigerMoeller.
the class Basics method testStopThreads.
@Test
public void testStopThreads() throws InterruptedException {
resetThreadCount();
EventSink<Integer> eventSink = new EventSink<Integer>();
AtomicInteger sum = new AtomicInteger(0);
AtomicInteger cnt = new AtomicInteger(0);
RateMeasure ms = new RateMeasure("rate");
eventSink.syncMap(i -> i * i).map(i -> "" + i).syncMap(s -> s.substring(0, s.length() / 2)).map(s -> s.length()).subscribe((len, err) -> {
ms.count();
if (len != null) {
// complete signal
cnt.incrementAndGet();
sum.addAndGet(len);
}
});
for (int i = 0; i < MASMSG_NUM; i++) {
while (!eventSink.offer(i)) {
Thread.yield();
}
}
eventSink.complete();
int count = 0;
while (cnt.get() != MASMSG_NUM && count < 10) {
Thread.sleep(1000);
count++;
}
Thread.sleep(5000);
System.out.println("threads:" + DispatcherThread.activeDispatchers.get());
Assert.assertTrue(DispatcherThread.activeDispatchers.get() == 0);
}
use of org.nustaq.kontraktor.util.RateMeasure in project kontraktor by RuedigerMoeller.
the class Java8Streams method remoteJava8Streams.
/**
* stream a java 8 stream via network
*
* @throws InterruptedException
*/
public <T> int remoteJava8Streams(Function<MyEvent, T> doStuff, int port) throws InterruptedException {
AtomicInteger validCount = new AtomicInteger(-1);
KxReactiveStreams kxReactiveStreams = KxReactiveStreams.get();
kxReactiveStreams.produce(IntStream.range(0, 5_000_000).mapToObj(i -> new MyEvent(i, Math.random(), "Hello" + i))).serve(new TCPPublisher().port(port));
CountDownLatch latch = new CountDownLatch(1);
RateMeasure measure = new RateMeasure("rate");
KxReactiveStreams.get().connect(MyEvent.class, new TCPConnectable().host("localhost").port(port)).stream(stream -> {
long count = 0;
try {
count = stream.map(event -> {
measure.count();
return doStuff.apply(event);
}).count();
} finally {
System.out.println("Count:" + count);
validCount.set((int) count);
latch.countDown();
}
});
latch.await();
return validCount.get();
}
Aggregations