jdk/test/java/lang/Class/TypeCheckMicroBenchmark.java
author jlahoda
Tue, 02 Dec 2014 15:12:40 +0100
changeset 27785 29d4cb4a8f5e
parent 5506 202f599c92aa
permissions -rw-r--r--
8065998: Avoid use of _ as a one-character identifier Reviewed-by: alanb, chegar, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * This is not a regression test, but a micro-benchmark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * I have run this as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * repeat 5 for f in -client -server; do mergeBench dolphin . jr -dsa -da $f TypeCheckMicroBenchmark.java; done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class TypeCheckMicroBenchmark {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    abstract static class Job {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        Job(String name) { this.name = name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        String name() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        abstract void work() throws Throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static void collectAllGarbage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        final java.util.concurrent.CountDownLatch drained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            = new java.util.concurrent.CountDownLatch(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            System.gc();        // enqueue finalizable objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            new Object() { protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                drained.countDown(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            System.gc();        // enqueue detector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            drained.await();    // wait for finalizer queue to drain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            System.gc();        // cleanup finalized objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        } catch (InterruptedException e) { throw new Error(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Runs each job for long enough that all the runtime compilers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * have had plenty of time to warm up, i.e. get around to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * compiling everything worth compiling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Returns array of average times per job per run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static long[] time0(Job ... jobs) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        final long warmupNanos = 10L * 1000L * 1000L * 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        long[] nanoss = new long[jobs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        for (int i = 0; i < jobs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            collectAllGarbage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            long t0 = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            long t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            do { jobs[i].work(); j++; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            while ((t = System.nanoTime() - t0) < warmupNanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            nanoss[i] = t/j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return nanoss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static void time(Job ... jobs) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        long[] warmup = time0(jobs); // Warm up run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        long[] nanoss = time0(jobs); // Real timing run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        long[] milliss = new long[jobs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        double[] ratios = new double[jobs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        final String nameHeader   = "Method";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        final String millisHeader = "Millis";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        final String ratioHeader  = "Ratio";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int nameWidth   = nameHeader.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int millisWidth = millisHeader.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int ratioWidth  = ratioHeader.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        for (int i = 0; i < jobs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            nameWidth = Math.max(nameWidth, jobs[i].name().length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            milliss[i] = nanoss[i]/(1000L * 1000L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            millisWidth = Math.max(millisWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                   String.format("%d", milliss[i]).length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            ratios[i] = (double) nanoss[i] / (double) nanoss[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            ratioWidth = Math.max(ratioWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                  String.format("%.3f", ratios[i]).length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        String format = String.format("%%-%ds %%%dd %%%d.3f%%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                      nameWidth, millisWidth, ratioWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                            nameWidth, millisWidth, ratioWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.printf(headerFormat, "Method", "Millis", "Ratio");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // Print out absolute and relative times, calibrated against first job
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (int i = 0; i < jobs.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.out.printf(format, jobs[i].name(), milliss[i], ratios[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static String keywordValue(String[] args, String keyword) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (String arg : args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (arg.startsWith(keyword))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                return arg.substring(keyword.length() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static int intArg(String[] args, String keyword, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        String val = keywordValue(args, keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return val == null ? defaultValue : Integer.parseInt(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static java.util.regex.Pattern patternArg(String[] args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                                      String keyword) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        String val = keywordValue(args, keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return val == null ? null : java.util.regex.Pattern.compile(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static Job[] filter(java.util.regex.Pattern filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                Job[] jobs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (filter == null) return jobs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Job[] newJobs = new Job[jobs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (Job job : jobs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (filter.matcher(job.name()).find())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                newJobs[n++] = job;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // Arrays.copyOf not available in JDK 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Job[] ret = new Job[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        System.arraycopy(newJobs, 0, ret, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Usage: [iterations=N] [size=N] [filter=REGEXP]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        final int iterations = intArg(args, "iterations", 30000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        final int size       = intArg(args, "size", 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        final java.util.regex.Pattern filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            = patternArg(args, "filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        final List<Integer> list = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        final Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for (int i = 0; i < size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            list.add(rnd.nextInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        final Class klazz = Integer.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        final Job[] jobs = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            new Job("toArray(T[])") { void work() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                Object[] a = new Integer[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                for (int i = 0; i < iterations; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    try { list.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    catch (ArrayStoreException ase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        throw new ClassCastException(); }}}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            new Job("isInstance") { void work() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                for (int i = 0; i < iterations; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    for (Object x : list.toArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        if (! (x != null && klazz.isInstance(x)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                            throw new ClassCastException(); }}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            new Job("Class.cast") { void work() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                for (int i = 0; i < iterations; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    for (Object x : list.toArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        klazz.cast(x); }}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            new Job("write into array") { void work() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                Object[] a = new Integer[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                for (int i = 0; i < iterations; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    for (Object x : list.toArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        try { a[0] = x; }
27785
29d4cb4a8f5e 8065998: Avoid use of _ as a one-character identifier
jlahoda
parents: 5506
diff changeset
   185
                        catch (ArrayStoreException unused) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            throw new ClassCastException(); }}}}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            new Job("write into dynamic array") { void work() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                for (int i = 0; i < iterations; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    for (Object x : list.toArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        Object[] a = (Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                            java.lang.reflect.Array.newInstance(klazz, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        try { a[0] = x; }
27785
29d4cb4a8f5e 8065998: Avoid use of _ as a one-character identifier
jlahoda
parents: 5506
diff changeset
   193
                        catch (ArrayStoreException unused) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                            throw new ClassCastException(); }}}}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        time(filter(filter, jobs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}