Search in sources :

Example 11 with EmitterProcessor

use of reactor.core.publisher.EmitterProcessor in project reactor-core by reactor.

the class FluxTests method parallelTest.

private void parallelTest(String dispatcher, int iterations) throws InterruptedException {
    System.out.println("Dispatcher: " + dispatcher);
    System.out.println("..........:  " + iterations);
    int[] data;
    CountDownLatch latch = new CountDownLatch(iterations);
    EmitterProcessor<Integer> deferred;
    switch(dispatcher) {
        case "partitioned":
            deferred = EmitterProcessor.create();
            deferred.publishOn(asyncGroup).parallel(2).groups().subscribe(stream -> stream.publishOn(asyncGroup).map(i -> i).scan(1, (acc, next) -> acc + next).subscribe(i -> latch.countDown()));
            break;
        default:
            deferred = EmitterProcessor.create();
            deferred.publishOn(asyncGroup).map(i -> i).scan(1, (acc, next) -> acc + next).subscribe(i -> latch.countDown());
    }
    data = new int[iterations];
    for (int i = 0; i < iterations; i++) {
        data[i] = i;
    }
    long start = System.currentTimeMillis();
    for (int i : data) {
        deferred.onNext(i);
    }
    if (!latch.await(15, TimeUnit.SECONDS)) {
        throw new RuntimeException("Count:" + (iterations - latch.getCount()) + " ");
    }
    long stop = System.currentTimeMillis() - start;
    stop = stop > 0 ? stop : 1;
    System.out.println("Time spent: " + stop + "ms");
    System.out.println("ev/ms: " + iterations / stop);
    System.out.println("ev/s: " + iterations / stop * 1000);
    System.out.println("");
    assertEquals(0, latch.getCount());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Processor(org.reactivestreams.Processor) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Timer(java.util.Timer) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Logger(reactor.util.Logger) Assertions(org.assertj.core.api.Assertions) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) KeyEvent(java.awt.event.KeyEvent) Instant(java.time.Instant) SignalType(reactor.core.publisher.SignalType) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) TopicProcessor(reactor.core.publisher.TopicProcessor) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) FluxProcessor(reactor.core.publisher.FluxProcessor) Scheduler(reactor.core.scheduler.Scheduler) OrderingComparison.lessThan(org.hamcrest.number.OrderingComparison.lessThan) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) MonoProcessor(reactor.core.publisher.MonoProcessor) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Phaser(java.util.concurrent.Phaser) Matcher(org.hamcrest.Matcher) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with EmitterProcessor

use of reactor.core.publisher.EmitterProcessor in project reactor-core by reactor.

the class FluxTests method multiplexUsingDispatchersAndSplit.

/**
 * <pre>
 *                 forkStream
 *                 /        \      < - - - int
 *                v          v
 * persistenceStream        computationStream
 *                 \        /      < - - - List< String >
 *                  v      v
 *                 joinStream      < - - - String
 *                 splitStream
 *             observedSplitStream
 * </pre>
 * @throws Exception for convenience
 */
@Test(timeout = TIMEOUT)
public void multiplexUsingDispatchersAndSplit() throws Exception {
    final EmitterProcessor<Integer> forkEmitterProcessor = EmitterProcessor.create();
    final EmitterProcessor<Integer> computationEmitterProcessor = EmitterProcessor.create(false);
    Scheduler computation = Schedulers.newSingle("computation");
    Scheduler persistence = Schedulers.newSingle("persistence");
    Scheduler forkJoin = Schedulers.newParallel("forkJoin", 2);
    final Flux<List<String>> computationStream = computationEmitterProcessor.publishOn(computation).map(i -> {
        final List<String> list = new ArrayList<>(i);
        for (int j = 0; j < i; j++) {
            list.add("i" + j);
        }
        return list;
    }).doOnNext(ls -> println("Computed: ", ls)).log("computation");
    final EmitterProcessor<Integer> persistenceEmitterProcessor = EmitterProcessor.create(false);
    final Flux<List<String>> persistenceStream = persistenceEmitterProcessor.publishOn(persistence).doOnNext(i -> println("Persisted: ", i)).map(i -> Collections.singletonList("done" + i)).log("persistence");
    Flux<Integer> forkStream = forkEmitterProcessor.publishOn(forkJoin).log("fork");
    forkStream.subscribe(computationEmitterProcessor);
    forkStream.subscribe(persistenceEmitterProcessor);
    final Flux<List<String>> joinStream = Flux.zip(computationStream, persistenceStream, (a, b) -> Arrays.asList(a, b)).publishOn(forkJoin).map(listOfLists -> {
        listOfLists.get(0).addAll(listOfLists.get(1));
        return listOfLists.get(0);
    }).log("join");
    final Semaphore doneSemaphore = new Semaphore(0);
    final MonoProcessor<List<String>> listPromise = joinStream.flatMap(Flux::fromIterable).log("resultStream").collectList().doOnTerminate(doneSemaphore::release).toProcessor();
    listPromise.subscribe();
    forkEmitterProcessor.onNext(1);
    forkEmitterProcessor.onNext(2);
    forkEmitterProcessor.onNext(3);
    forkEmitterProcessor.onComplete();
    List<String> res = listPromise.block(Duration.ofSeconds(5));
    assertEquals(Arrays.asList("i0", "done1", "i0", "i1", "done2", "i0", "i1", "i2", "done3"), res);
    forkJoin.dispose();
    persistence.dispose();
    computation.dispose();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Processor(org.reactivestreams.Processor) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Timer(java.util.Timer) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Logger(reactor.util.Logger) Assertions(org.assertj.core.api.Assertions) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) KeyEvent(java.awt.event.KeyEvent) Instant(java.time.Instant) SignalType(reactor.core.publisher.SignalType) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) TopicProcessor(reactor.core.publisher.TopicProcessor) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) FluxProcessor(reactor.core.publisher.FluxProcessor) Scheduler(reactor.core.scheduler.Scheduler) OrderingComparison.lessThan(org.hamcrest.number.OrderingComparison.lessThan) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) MonoProcessor(reactor.core.publisher.MonoProcessor) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Phaser(java.util.concurrent.Phaser) Matcher(org.hamcrest.Matcher) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) Flux(reactor.core.publisher.Flux) Semaphore(java.util.concurrent.Semaphore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with EmitterProcessor

use of reactor.core.publisher.EmitterProcessor in project reactor-core by reactor.

the class FluxTests method parallelMapManyTest.

private void parallelMapManyTest(String dispatcher, int iterations) throws InterruptedException {
    System.out.println("MM Dispatcher: " + dispatcher);
    System.out.println("..........:  " + iterations);
    int[] data;
    CountDownLatch latch = new CountDownLatch(iterations);
    EmitterProcessor<Integer> mapManydeferred;
    switch(dispatcher) {
        case "partitioned":
            mapManydeferred = EmitterProcessor.create();
            mapManydeferred.parallel(4).groups().subscribe(substream -> substream.publishOn(asyncGroup).subscribe(i -> latch.countDown()));
            break;
        default:
            mapManydeferred = EmitterProcessor.create();
            ("sync".equals(dispatcher) ? mapManydeferred : mapManydeferred.publishOn(asyncGroup)).flatMap(Flux::just).subscribe(i -> latch.countDown());
    }
    data = new int[iterations];
    for (int i = 0; i < iterations; i++) {
        data[i] = i;
    }
    long start = System.currentTimeMillis();
    for (int i : data) {
        mapManydeferred.onNext(i);
    }
    if (!latch.await(20, TimeUnit.SECONDS)) {
        throw new RuntimeException(latch.getCount() + "");
    } else {
        System.out.println(latch.getCount());
    }
    assertEquals(0, latch.getCount());
    long stop = System.currentTimeMillis() - start;
    stop = stop > 0 ? stop : 1;
    System.out.println("MM Dispatcher: " + dispatcher);
    System.out.println("Time spent: " + stop + "ms");
    System.out.println("ev/ms: " + iterations / stop);
    System.out.println("ev/s: " + iterations / stop * 1000);
    System.out.println("");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Processor(org.reactivestreams.Processor) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Timer(java.util.Timer) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Logger(reactor.util.Logger) Assertions(org.assertj.core.api.Assertions) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) KeyEvent(java.awt.event.KeyEvent) Instant(java.time.Instant) SignalType(reactor.core.publisher.SignalType) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) TopicProcessor(reactor.core.publisher.TopicProcessor) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) FluxProcessor(reactor.core.publisher.FluxProcessor) Scheduler(reactor.core.scheduler.Scheduler) OrderingComparison.lessThan(org.hamcrest.number.OrderingComparison.lessThan) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) MonoProcessor(reactor.core.publisher.MonoProcessor) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Phaser(java.util.concurrent.Phaser) Matcher(org.hamcrest.Matcher) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Duration (java.time.Duration)13 List (java.util.List)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 TimeUnit (java.util.concurrent.TimeUnit)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 Test (org.junit.Test)13 EmitterProcessor (reactor.core.publisher.EmitterProcessor)13 Flux (reactor.core.publisher.Flux)13 Mono (reactor.core.publisher.Mono)13 Schedulers (reactor.core.scheduler.Schedulers)13 StepVerifier (reactor.test.StepVerifier)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 MonoProcessor (reactor.core.publisher.MonoProcessor)11 ArrayList (java.util.ArrayList)10 Arrays (java.util.Arrays)10 Collections (java.util.Collections)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Consumer (java.util.function.Consumer)10 Assert (org.junit.Assert)10 FluxProcessor (reactor.core.publisher.FluxProcessor)10