· 6 years ago · Aug 29, 2019, 05:32 PM
1package com.github.stokito.experiments;
2
3import static java.util.concurrent.TimeUnit.MILLISECONDS;
4import static org.openjdk.jmh.annotations.Mode.Throughput;
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.Locale;
8import org.openjdk.jmh.annotations.Benchmark;
9import org.openjdk.jmh.annotations.BenchmarkMode;
10import org.openjdk.jmh.annotations.Level;
11import org.openjdk.jmh.annotations.OutputTimeUnit;
12import org.openjdk.jmh.annotations.Scope;
13import org.openjdk.jmh.annotations.Setup;
14import org.openjdk.jmh.annotations.State;
15import org.openjdk.jmh.infra.Blackhole;
16
17/*
18Benchmark Mode Cnt Score Error Units
19StreamSortBench.testCloneSort thrpt 5 861.309 ± 71.133 ops/ms
20StreamSortBench.testStreamSort thrpt 5 768.630 ± 29.832 ops/ms
21*/
22@State(Scope.Thread)
23public class StreamSortBench {
24 private static final String[] COLUMNS = {
25 "id",
26 "merchant_id",
27 "public_key",
28 "registration_date",
29 "reserve_fee",
30 "secret_key",
31 "status_acknowledged",
32 "marketplace_id",
33 "payout_interval_id",
34 "merchant_status",
35 "newsletter_subscribed",
36 "logo",
37 "firts_name",
38 "last_name",
39 "company_id",
40 "wallet_account_code",
41 "unavailable_message",
42 "wallet_kyc_check_status"
43 };
44
45 @Setup(Level.Trial)
46 public void setup() {
47
48 }
49
50 @Benchmark
51 @BenchmarkMode(Throughput)
52 @OutputTimeUnit(MILLISECONDS)
53 public void testStreamSort(Blackhole bh) {
54 StringBuilder sb = new StringBuilder(1024);
55 Arrays.stream(COLUMNS)
56 .sorted(String::compareTo)
57 .forEachOrdered(columnName -> sb.append( "column`" ).append( columnName ).append( '`' ));
58 bh.consume(sb.toString());
59 }
60
61 @Benchmark
62 @BenchmarkMode(Throughput)
63 @OutputTimeUnit(MILLISECONDS)
64 public void testCloneSort(Blackhole bh) {
65 StringBuilder sb = new StringBuilder(1024);
66 String[] alphabeticalColumns = COLUMNS.clone();
67 Arrays.sort( alphabeticalColumns, String::compareTo);
68 for ( String column : alphabeticalColumns ) {
69 String columnName = column;
70 sb.append( "column`" ).append( columnName ).append( '`' );
71 }
72 bh.consume(sb.toString());
73 }
74
75}