test/micro/org/openjdk/bench/java/lang/StringIndexOf.java
author redestad
Fri, 16 Nov 2018 23:39:51 +0100
changeset 52595 16609197022c
child 54043 0324b3756aa2
permissions -rw-r--r--
8061281: Microbenchmark suite build support, directory layout and sample benchmarks 8061282: Migrate jmh-jdk-microbenchmarks into the JDK Reviewed-by: ecaspole, mchung, erikj, ihse Contributed-by: magnus.ihse.bursie@oracle.com, erik.joelsson@oracle.com, claes.redestad@oracle.com, sfriberg@kth.se
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     1
/*
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     4
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     8
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    13
 * accompanied this code).
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    14
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    18
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    21
 * questions.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    22
 */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    23
package org.openjdk.bench.java.lang;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    24
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    25
import org.openjdk.jmh.annotations.Benchmark;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    26
import org.openjdk.jmh.annotations.BenchmarkMode;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    27
import org.openjdk.jmh.annotations.Mode;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    28
import org.openjdk.jmh.annotations.OutputTimeUnit;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    29
import org.openjdk.jmh.annotations.Scope;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    30
import org.openjdk.jmh.annotations.Setup;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    31
import org.openjdk.jmh.annotations.State;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    32
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    33
import java.util.concurrent.TimeUnit;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    34
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    35
@BenchmarkMode(Mode.AverageTime)
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    36
@OutputTimeUnit(TimeUnit.NANOSECONDS)
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    37
@State(Scope.Thread)
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    38
public class StringIndexOf {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    39
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    40
    private String dataString;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    41
    private String searchString;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    42
    private String dataStringBig;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    43
    private String searchStringBig;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    44
    private String data;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    45
    private String sub;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    46
    private String shortSub1;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    47
    private String data2;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    48
    private String shortSub2;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    49
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    50
    @Setup
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    51
    public void setup() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    52
        dataString = "ngdfilsoscargfdgf";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    53
        searchString = "oscar";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    54
        dataStringBig = "2937489745890797905764956790452976742965790437698498409583479067ngdcapaapapapasdkajdlkajskldjaslkjdlkasjdsalkjas";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    55
        searchStringBig = "capaapapapasdkajdlkajskldjaslkjdlkasjdsalk";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    56
        data = "0000100101010010110101010010101110101001110110101010010101010010000010111010101010101010100010010101110111010101101010100010010100001010111111100001010101001010100001010101001010101010111010010101010101010101010101010";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    57
        sub = "10101010";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    58
        shortSub1 = "1";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    59
        data2 = "00001001010100a10110101010010101110101001110110101010010101010010000010111010101010101010a100010010101110111010101101010100010010a100a0010101111111000010101010010101000010101010010101010101110a10010101010101010101010101010";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    60
        shortSub2 = "a";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    61
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    62
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    63
    /** IndexOf Micros Strings */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    64
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    65
    /**
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    66
     * Benchmarks String.indexOf with a rather small String to search and a rather small String to search for. The
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    67
     * searched string contains the string that is searched for.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    68
     */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    69
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    70
    public int success() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    71
        return dataString.indexOf(searchString, 2);
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    72
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    73
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    74
    /**
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    75
     * Benchmarks String.indexOf with a rather big String to search and a rather big String to search for. The searched
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    76
     * string contains the string that is searched for.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    77
     */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    78
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    79
    public int successBig() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    80
        return dataStringBig.indexOf(searchStringBig, 2);
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    81
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    82
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    83
    /**
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    84
     * Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 8 chars and most
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    85
     * oftenly will require a inner lopp match in String.indexOf with sse42.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    86
     */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    87
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    88
    public int advancedWithMediumSub() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    89
        int index = 0;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    90
        int dummy = 0;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    91
        while ((index = data.indexOf(sub, index)) > -1) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    92
            index++;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    93
            dummy += index;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    94
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    95
        return dummy;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    96
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    97
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    98
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    99
    /**
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   100
     * Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars will find a
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   101
     * huge amount of matches
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   102
     */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   103
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   104
    public int advancedWithShortSub1() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   105
        int dummy = 0;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   106
        int index = 0;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   107
        while ((index = data.indexOf(shortSub1, index)) > -1) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   108
            index++;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   109
            dummy += index;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   110
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   111
        return dummy;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   112
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   113
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   114
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   115
    /**
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   116
     * Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars but only with
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   117
     * a few matches.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   118
     */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   119
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   120
    public int advancedWithShortSub2() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   121
        int dummy = 0;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   122
        int index = 0;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   123
        while ((index = data2.indexOf(shortSub2, index)) > -1) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   124
            index++;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   125
            dummy += index;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   126
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   127
        return dummy;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   128
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   129
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   130
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   131
    public void constantPattern() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   132
        String tmp = "simple-hash:SHA-1/UTF-8";
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   133
        if (!tmp.contains("SHA-1")) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   134
            throw new RuntimeException("indexOf failed");
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   135
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   136
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   137
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   138
}