Search in sources :

Example 11 with Fork

use of org.openjdk.jmh.annotations.Fork in project cyclops by aol.

the class ShakespearePlaysScrabbleWithStreams method measureThroughput.

@SuppressWarnings("unused")
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@Fork(1)
public List<Entry<Integer, List<String>>> measureThroughput() {
    // Function to compute the score of a given word
    IntUnaryOperator scoreOfALetter = letter -> letterScores[letter - 'a'];
    // score of the same letters in a word
    ToIntFunction<Entry<Integer, Long>> letterScore = entry -> letterScores[entry.getKey() - 'a'] * Integer.min(entry.getValue().intValue(), scrabbleAvailableLetters[entry.getKey() - 'a']);
    // Histogram of the letters in a given word
    Function<String, Map<Integer, Long>> histoOfLetters = word -> word.chars().boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    // number of blanks for a given letter
    ToLongFunction<Entry<Integer, Long>> blank = entry -> Long.max(0L, entry.getValue() - scrabbleAvailableLetters[entry.getKey() - 'a']);
    // number of blanks for a given word
    Function<String, Long> nBlanks = word -> histoOfLetters.apply(word).entrySet().stream().mapToLong(blank).sum();
    // can a word be written with 2 blanks?
    Predicate<String> checkBlanks = word -> nBlanks.apply(word) <= 2;
    // score taking blanks into account
    Function<String, Integer> score2 = word -> histoOfLetters.apply(word).entrySet().stream().mapToInt(letterScore).sum();
    // Placing the word on the board
    // Building the streams of first and last letters
    Function<String, IntStream> first3 = word -> word.chars().limit(3);
    Function<String, IntStream> last3 = word -> word.chars().skip(Integer.max(0, word.length() - 4));
    // Stream to be maxed
    Function<String, IntStream> toBeMaxed = word -> Stream.of(first3.apply(word), last3.apply(word)).flatMapToInt(Function.identity());
    // Bonus for double letter
    ToIntFunction<String> bonusForDoubleLetter = word -> toBeMaxed.apply(word).map(scoreOfALetter).max().orElse(0);
    // score of the word put on the board
    Function<String, Integer> score3 = word -> (score2.apply(word) + bonusForDoubleLetter.applyAsInt(word)) + (score2.apply(word) + bonusForDoubleLetter.applyAsInt(word)) + (word.length() == 7 ? 50 : 0);
    Function<Function<String, Integer>, Stream<Map<Integer, List<String>>>> buildHistoOnScore = score -> Stream.of(buildShakerspeareWordsStream().filter(scrabbleWords::contains).filter(// filter out the words that needs more than 2 blanks
    checkBlanks).collect(Collectors.groupingBy(score, () -> new TreeMap<Integer, List<String>>(Comparator.reverseOrder()), Collectors.toList())));
    // best key / value pairs
    List<Entry<Integer, List<String>>> finalList = buildHistoOnScore.apply(score3).map(e -> e.entrySet().stream().limit(3).collect(Collectors.toList())).findAny().get();
    return finalList;
}
Also used : IntStream(java.util.stream.IntStream) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Measurement(org.openjdk.jmh.annotations.Measurement) IntUnaryOperator(java.util.function.IntUnaryOperator) Mode(org.openjdk.jmh.annotations.Mode) Predicate(java.util.function.Predicate) ToIntFunction(java.util.function.ToIntFunction) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Warmup(org.openjdk.jmh.annotations.Warmup) Benchmark(org.openjdk.jmh.annotations.Benchmark) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) Map(java.util.Map) Entry(java.util.Map.Entry) Fork(org.openjdk.jmh.annotations.Fork) Comparator(java.util.Comparator) ToLongFunction(java.util.function.ToLongFunction) IntUnaryOperator(java.util.function.IntUnaryOperator) ToIntFunction(java.util.function.ToIntFunction) Function(java.util.function.Function) ToLongFunction(java.util.function.ToLongFunction) Entry(java.util.Map.Entry) IntStream(java.util.stream.IntStream) Stream(java.util.stream.Stream) List(java.util.List) TreeMap(java.util.TreeMap) Map(java.util.Map) IntStream(java.util.stream.IntStream) Measurement(org.openjdk.jmh.annotations.Measurement) Warmup(org.openjdk.jmh.annotations.Warmup) Fork(org.openjdk.jmh.annotations.Fork) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit)

Aggregations

Benchmark (org.openjdk.jmh.annotations.Benchmark)11 Fork (org.openjdk.jmh.annotations.Fork)11 Measurement (org.openjdk.jmh.annotations.Measurement)8 Warmup (org.openjdk.jmh.annotations.Warmup)6 BenchmarkMode (org.openjdk.jmh.annotations.BenchmarkMode)4 OutputTimeUnit (org.openjdk.jmh.annotations.OutputTimeUnit)3 ByteBuf (io.netty.buffer.ByteBuf)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 TreeMap (java.util.TreeMap)2 TimeUnit (java.util.concurrent.TimeUnit)2 Function (java.util.function.Function)2 IntUnaryOperator (java.util.function.IntUnaryOperator)2 Predicate (java.util.function.Predicate)2 ToIntFunction (java.util.function.ToIntFunction)2 ToLongFunction (java.util.function.ToLongFunction)2 Collectors (java.util.stream.Collectors)2