|
1 | 1 | # scalecube-benchmarks
|
2 | 2 | Microbenchmarks framework
|
| 3 | + |
| 4 | +## Download |
| 5 | + |
| 6 | +```xml |
| 7 | +<dependency> |
| 8 | + <groupId>io.scalecube</groupId> |
| 9 | + <artifactId>scalecube-benchmarks-api</artifactId> |
| 10 | + <version>1.1.8</version> |
| 11 | +</dependency> |
| 12 | +``` |
| 13 | + |
| 14 | +## Getting started |
| 15 | + |
| 16 | +First, you need to create the settings which will be used while test is running. You can use the builder for it: |
| 17 | + |
| 18 | +```java |
| 19 | +public static void main(String[] args) { |
| 20 | + BenchmarksSettings settings = BenchmarksSettings.from(args) |
| 21 | + .nThreads(2) |
| 22 | + .durationUnit(TimeUnit.NANOSECONDS) |
| 23 | + .build(); |
| 24 | + .... |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +As you see, you could want to override some default settings with the command line, just include them during startup of the test. Or just use the hardcoded style when you write a test scenario. |
| 29 | + |
| 30 | +The second point is you need to prepare some state for your test scenario. You can define how to launch your server, launch some client or just execute some work before running test scenario. For instance, you can create a separated class to reduce writing your scenario in a test or reuse the same state in different scenarios. |
| 31 | + |
| 32 | +```java |
| 33 | +public class ExampleServiceBenchmarksState extends BenchmarksState<ExampleServiceBenchmarksState> { |
| 34 | + |
| 35 | + private ExampleService exampleService; |
| 36 | + |
| 37 | + public ExampleServiceBenchmarksState(BenchmarksSettings settings) { |
| 38 | + super(settings); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void beforeAll() { |
| 43 | + this.exampleService = new ExampleService(); |
| 44 | + } |
| 45 | + |
| 46 | + public ExampleService exampleService() { |
| 47 | + return exampleService; |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | +You can override two methods: `beforeAll` and `afterAll`, these methods will be invoked before test execution and after test termination. The state also may need some settings and it can get them from given BenchmarkSettings via constructor. Also, this state has a few necessary methods, about them below. |
| 52 | + |
| 53 | +### runForSync |
| 54 | + |
| 55 | +```java |
| 56 | +void runForSync(Function<SELF, Function<Long, Object>> func) |
| 57 | +``` |
| 58 | + |
| 59 | +This method intends for execution synchronous tasks. It receives a function, that should return the execution to be tested for the given the state. For instance: |
| 60 | + |
| 61 | +```java |
| 62 | +public static void main(String[] args) { |
| 63 | + BenchmarksSettings settings = ...; |
| 64 | + new RouterBenchmarksState(settings).runForSync(state -> { |
| 65 | + |
| 66 | + Timer timer = state.timer("timer"); |
| 67 | + Router router = state.getRouter(); |
| 68 | + |
| 69 | + return iteration -> { |
| 70 | + Timer.Context timeContext = timer.time(); |
| 71 | + ServiceReference serviceReference = router.route(request); |
| 72 | + timeContext.stop(); |
| 73 | + return serviceReference; |
| 74 | + }; |
| 75 | + }); |
| 76 | + } |
| 77 | +``` |
| 78 | + |
| 79 | +As you see, to use this method you need return some function, to generate one you can use the transferred state and iteration's number. |
| 80 | + |
| 81 | +### runForAsync |
| 82 | + |
| 83 | +```java |
| 84 | +void runForAsync(Function<SELF, Function<Long, Publisher<?>>> func) |
| 85 | +``` |
| 86 | + |
| 87 | +This method intends for execution asynchronous tasks. It receives a function, that should return the execution to be tested for the given the state. Note, the unitOfwork should return some `Publisher`. For instance: |
| 88 | + |
| 89 | +```java |
| 90 | + public static void main(String[] args) { |
| 91 | + BenchmarksSettings settings = ...; |
| 92 | + new ExampleServiceBenchmarksState(settings).runForAsync(state -> { |
| 93 | + |
| 94 | + ExampleService service = state.exampleService(); |
| 95 | + Meter meter = state.meter("meter"); |
| 96 | + |
| 97 | + return iteration -> { |
| 98 | + return service.invoke("hello") |
| 99 | + .doOnTerminate(() -> meter.mark()); |
| 100 | + }; |
| 101 | + }); |
| 102 | + } |
| 103 | +``` |
| 104 | + |
| 105 | +As you see, to use this method you need return some function, to generate one you can use the transferred state and iteration's number. |
| 106 | + |
| 107 | +### runWithRampUp |
| 108 | + |
| 109 | +```java |
| 110 | +<T> void runWithRampUp(BiFunction<Long, SELF, Publisher<T>> setUp, |
| 111 | + Function<SELF, BiFunction<Long, T, Publisher<?>>> func, |
| 112 | + BiFunction<SELF, T, Mono<Void>> cleanUp) |
| 113 | +``` |
| 114 | + |
| 115 | +This method intends for execution asynchronous tasks with consumption some resources via ramp-up strategy. It receives three functions, they are necessary to provide all resource life-cycle. The first function is like resource supplier, to implement this one you have access to the active state and a ramp-up iteration's number. And when ramp-up strategy asks for new resources it will be invoked. The second function is like unitOfWork supplier, to implement this one you receive the active state (to take some services or metric's tools), iteration's number and a resource, that was created on the former step. And the last function is like clean-up supplier, that knows how to need release given resource. For instance: |
| 116 | + |
| 117 | +```java |
| 118 | + public static void main(String[] args) { |
| 119 | + BenchmarksSettings settings = BenchmarksSettings.from(args) |
| 120 | + .rampUpDuration(Duration.ofSeconds(10)) |
| 121 | + .rampUpInterval(Duration.ofSeconds(1)) |
| 122 | + .executionTaskDuration(Duration.ofSeconds(30)) |
| 123 | + .executionTaskInterval(Duration.ofMillis(100)) |
| 124 | + .build(); |
| 125 | + |
| 126 | + new ExampleServiceBenchmarksState(settings).runWithRampUp( |
| 127 | + (rampUpIteration, state) -> Mono.just(new ServiceCaller(state.exampleService()), |
| 128 | + state -> { |
| 129 | + Meter meter = state.meter("meter"); |
| 130 | + return (iteration, serviceCaller) -> serviceCaller.call("hello").doOnTerminate(meter::mark); |
| 131 | + }, |
| 132 | + (state, serviceCaller) -> serviceCaller.close()); |
| 133 | + } |
| 134 | +``` |
| 135 | + |
| 136 | +It's time to describe the settings in more detail. First, you can see two ramp-up parameters, these are `rampUpDuration` and `rampUpInterval`. They need to specify how long will be processing ramp-up stage and how often will be invoked the resource supplier to receive a new resource. Also, we have two parameters to specify how long will be processing all `unitOfWork`s on the given resource (`executionTaskDuration`) and with another one you can specify some interval that will be applied to invoke the next `unitOfWork` on the given resource (`executionTaskInterval`). |
0 commit comments