Search in sources :

Example 11 with RateMeasure

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Promise(org.nustaq.kontraktor.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) Test(org.junit.Test)

Example 12 with RateMeasure

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) IntStream(java.util.stream.IntStream) TCPServerConnector(org.nustaq.kontraktor.remoting.tcp.TCPServerConnector) ActorPublisher(org.nustaq.kontraktor.remoting.base.ActorPublisher) TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) DispatcherThread(org.nustaq.kontraktor.impl.DispatcherThread) Test(org.junit.Test) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assert(junit.framework.Assert) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorServer(org.nustaq.kontraktor.remoting.base.ActorServer) Log(org.nustaq.kontraktor.util.Log) Actors(org.nustaq.kontraktor.Actors) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) Test(org.junit.Test)

Example 13 with RateMeasure

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);
}
Also used : TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) IntStream(java.util.stream.IntStream) TCPServerConnector(org.nustaq.kontraktor.remoting.tcp.TCPServerConnector) ActorPublisher(org.nustaq.kontraktor.remoting.base.ActorPublisher) TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) DispatcherThread(org.nustaq.kontraktor.impl.DispatcherThread) Test(org.junit.Test) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assert(junit.framework.Assert) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorServer(org.nustaq.kontraktor.remoting.base.ActorServer) Log(org.nustaq.kontraktor.util.Log) Actors(org.nustaq.kontraktor.Actors) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) Test(org.junit.Test)

Example 14 with RateMeasure

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) IntStream(java.util.stream.IntStream) TCPServerConnector(org.nustaq.kontraktor.remoting.tcp.TCPServerConnector) ActorPublisher(org.nustaq.kontraktor.remoting.base.ActorPublisher) TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) DispatcherThread(org.nustaq.kontraktor.impl.DispatcherThread) Test(org.junit.Test) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assert(junit.framework.Assert) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorServer(org.nustaq.kontraktor.remoting.base.ActorServer) Log(org.nustaq.kontraktor.util.Log) Actors(org.nustaq.kontraktor.Actors) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) Test(org.junit.Test)

Example 15 with RateMeasure

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

RateMeasure (org.nustaq.kontraktor.util.RateMeasure)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 Test (org.junit.Test)20 KxReactiveStreams (org.nustaq.kontraktor.reactivestreams.KxReactiveStreams)13 WebSocketPublisher (org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher)13 WebSocketConnectable (org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 Actors (org.nustaq.kontraktor.Actors)12 IntStream (java.util.stream.IntStream)10 Log (org.nustaq.kontraktor.util.Log)10 Assert (org.junit.Assert)9 KxPublisher (org.nustaq.kontraktor.reactivestreams.KxPublisher)9 Publisher (org.reactivestreams.Publisher)9 RxReactiveStreams (rx.RxReactiveStreams)9 TCPConnectable (org.nustaq.kontraktor.remoting.tcp.TCPConnectable)8 ActorSystem (akka.actor.ActorSystem)7 ActorMaterializer (akka.stream.ActorMaterializer)7 ActorMaterializerSettings (akka.stream.ActorMaterializerSettings)7 Materializer (akka.stream.Materializer)7 PublisherSink (akka.stream.impl.PublisherSink)7