Skip to content
This repository was archived by the owner on Jun 20, 2025. It is now read-only.

Improved sync/async benchmarks #48

Merged
merged 5 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.codahale.metrics.Timer;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;

import reactor.core.Exceptions;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Mono;
Expand All @@ -18,7 +19,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -207,12 +210,18 @@ public final void runForSync(Function<SELF, Function<Long, Object>> func) {

Function<Long, Object> unitOfWork = func.apply(self);

Flux<Long> fromStream = Flux.fromStream(LongStream.range(0, settings.numOfIterations()).boxed());
CountDownLatch latch = new CountDownLatch(1);

Flux.merge(fromStream
.publishOn(scheduler()).map(unitOfWork))
.take(settings.executionTaskDuration())
.blockLast();
Flux.fromStream(LongStream.range(0, settings.numOfIterations()).boxed())
.parallel()
.runOn(scheduler())
.map(unitOfWork)
.doOnTerminate(latch::countDown)
.subscribe();

latch.await(settings.executionTaskDuration().toMillis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw Exceptions.propagate(e);
} finally {
self.shutdown();
}
Expand Down Expand Up @@ -280,7 +289,7 @@ public final <T> void runWithRampUp(

BiFunction<Long, T, Publisher<?>> unitOfWork = func.apply(self);

Flux.interval(settings.rampUpInterval())
Flux.interval(Duration.ZERO, settings.rampUpInterval())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

.take(settings.rampUpDuration())
.flatMap(rampUpIteration -> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
public class ExampleService {

public Mono<String> invoke(String request) {
return Mono.defer(() -> Mono.just(request + hardTask()));
return Mono.defer(() -> Mono.just(syncInvoke(request)));
}

public String syncInvoke(String request) {
return request + hardTask();
}

private Double hardTask() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.scalecube.benchmarks.examples;

import io.scalecube.benchmarks.BenchmarksSettings;

import com.codahale.metrics.Meter;
import com.codahale.metrics.Timer;

import java.util.concurrent.TimeUnit;

public class SyncExampleBenchmarksRunner {

/**
* Runs example benchmark.
*
* @param args command line args
*/
public static void main(String[] args) {
BenchmarksSettings settings = BenchmarksSettings.from(args).durationUnit(TimeUnit.NANOSECONDS).build();
new ExampleServiceBenchmarksState(settings).runForSync(state -> {

ExampleService service = state.exampleService();
Timer timer = state.timer("timer");
Meter meter = state.meter("meter");

return i -> {
Timer.Context timeContext = timer.time();
String result = service.syncInvoke("hello");
timeContext.stop();
meter.mark();
return result;
};
});
}
}