Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test the performance of parallel processing with Spliterators in Java

Suppose we are given a data structure that implements a spliterator as well. What is the boilerplate code for testing whether parallel processing using that spliterator is actually better than sequential processing?

like image 881
coderodde Avatar asked Jun 09 '26 23:06

coderodde


1 Answers

To test the spliterator itself you may create a sequential and parallel stream and reduce it with simple reduction operation with minimal overhead. For example:

@Benchmark
public sequential(Blackhole bh) {
    bh.consume(StreamSupport.stream(myContainer.spliterator(), false).reduce((a, b) -> a));
}

@Benchmark
public parallel(Blackhole bh) {
    bh.consume(StreamSupport.stream(myContainer.spliterator(), true).reduce((a, b) -> a));
}

Usually it's more critical to check whether your parallel spliterator works correctly for any sequence of trySplit/tryAdvance/forEachRemaining calls. Before testing the speed it's a good idea to test thoroughly for correctness.

Also probably a good idea is to create the test which is closer to real life. Think up how your data structure will be used in production code and create the test which implements such real example in parallel and in sequential. Such results would be more relevant to users of your data structure.

like image 175
Tagir Valeev Avatar answered Jun 15 '26 06:06

Tagir Valeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!