test/micro/org/openjdk/bench/java/util/regex/PatternBench.java
author redestad
Sat, 01 Jun 2019 03:18:23 +0200
changeset 55141 db105c4c5776
child 55220 28c93b5fb056
permissions -rw-r--r--
8225061: Performance regression in Regex Reviewed-by: naoto, alanb Contributed-by: claes.redestad@oracle.com, naoto.sato@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55141
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
     1
/*
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
     4
 *
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
     8
 *
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    13
 * accompanied this code).
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    14
 *
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    18
 *
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    21
 * questions.
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    22
 */
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    23
package org.openjdk.bench.java.util.regex;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    24
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    25
import org.openjdk.jmh.annotations.Benchmark;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    26
import org.openjdk.jmh.annotations.BenchmarkMode;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    27
import org.openjdk.jmh.annotations.Measurement;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    28
import org.openjdk.jmh.annotations.Mode;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    29
import org.openjdk.jmh.annotations.OutputTimeUnit;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    30
import org.openjdk.jmh.annotations.Param;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    31
import org.openjdk.jmh.annotations.Scope;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    32
import org.openjdk.jmh.annotations.Setup;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    33
import org.openjdk.jmh.annotations.State;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    34
import org.openjdk.jmh.annotations.Warmup;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    35
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    36
import java.util.concurrent.TimeUnit;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    37
import java.util.regex.Pattern;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    38
import java.util.regex.PatternSyntaxException;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    39
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    40
@BenchmarkMode(Mode.AverageTime)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    41
@OutputTimeUnit(TimeUnit.NANOSECONDS)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    42
@State(Scope.Thread)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    43
public class PatternBench {
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    44
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    45
    public String fileTestString;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    46
    public String flagsString;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    47
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    48
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    49
    public Pattern graphemePattern;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    50
    public Pattern jmodPattern;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    51
    public Pattern jmodCanonicalPattern;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    52
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    53
    public Pattern pattern;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    54
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    55
    @Setup
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    56
    public void setup() {
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    57
        flagsString = "\ud83c\udde6\ud83c\uddec\ud83c\uddec\ud83c\udde6\ud83c\uddfa\ud83c\uddf8\ud83c\uddeb\ud83c\uddf7";
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    58
        fileTestString = "META-INF/providers/org.openjdk.foo_hotspot_nodes_PluginFactory_EndLockScopeNode";
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    59
        graphemePattern = Pattern.compile("\\b{g}");
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    60
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    61
        String jmodRegex = "^.*(?:(?:_the\\.[^/]*)|(?:_[^/]*\\.marker)|(?:[^/]*\\.diz)|(?:[^/]*\\.debuginfo)|(?:[^/]*\\.dSYM/.*)|(?:[^/]*\\.dSYM)|(?:[^/]*\\.pdb)|(?:[^/]*\\.map))$";
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    62
        jmodCanonicalPattern = Pattern.compile(jmodRegex, Pattern.CANON_EQ);
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    63
        jmodPattern = Pattern.compile(jmodRegex);
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    64
    }
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    65
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    66
    @Benchmark
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    67
    @Warmup(iterations = 3)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    68
    @Measurement(iterations = 3)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    69
    public int splitFlags() {
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    70
        return graphemePattern.split(flagsString).length;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    71
    }
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    72
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    73
    @Benchmark
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    74
    @Warmup(iterations = 3)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    75
    @Measurement(iterations = 3)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    76
    public boolean canonicalJmodMatch() {
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    77
        return jmodCanonicalPattern.matcher(fileTestString).matches();
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    78
    }
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    79
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    80
    @Benchmark
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    81
    @Warmup(iterations = 3)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    82
    @Measurement(iterations = 3)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    83
    public boolean normalJmodMatch() {
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    84
        return jmodPattern.matcher(fileTestString).matches();
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    85
    }
db105c4c5776 8225061: Performance regression in Regex
redestad
parents:
diff changeset
    86
}