jdk/test/java/nio/Buffer/SwapMicroBenchmark.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright (c) 2007 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * This is not a regression test, but a micro-benchmark.
       
    26  * To exercise swap, run with filter=LITTLE_ENDIAN on sparc,
       
    27  * filter=BIG_ENDIAN on x86.
       
    28  *
       
    29  * I have run this as follows:
       
    30  *
       
    31  * for f in -client -server; do mergeBench dolphin . jr -dsa -da $f SwapMicroBenchmark.java filter=LITTLE_ENDIAN; done
       
    32  *
       
    33  * @author Martin Buchholz
       
    34  */
       
    35 
       
    36 import java.util.*;
       
    37 import java.nio.*;
       
    38 import java.util.concurrent.*;
       
    39 import java.util.regex.Pattern;
       
    40 
       
    41 public class SwapMicroBenchmark {
       
    42     abstract static class Job {
       
    43         private final String name;
       
    44         public Job(String name) { this.name = name; }
       
    45         public String name() { return name; }
       
    46         public abstract void work() throws Throwable;
       
    47     }
       
    48 
       
    49     private static void collectAllGarbage() {
       
    50         final java.util.concurrent.CountDownLatch drained
       
    51             = new java.util.concurrent.CountDownLatch(1);
       
    52         try {
       
    53             System.gc();        // enqueue finalizable objects
       
    54             new Object() { protected void finalize() {
       
    55                 drained.countDown(); }};
       
    56             System.gc();        // enqueue detector
       
    57             drained.await();    // wait for finalizer queue to drain
       
    58             System.gc();        // cleanup finalized objects
       
    59         } catch (InterruptedException e) { throw new Error(e); }
       
    60     }
       
    61 
       
    62     /**
       
    63      * Runs each job for long enough that all the runtime compilers
       
    64      * have had plenty of time to warm up, i.e. get around to
       
    65      * compiling everything worth compiling.
       
    66      * Returns array of average times per job per run.
       
    67      */
       
    68     private static long[] time0(Job ... jobs) throws Throwable {
       
    69         final long warmupNanos = 10L * 1000L * 1000L * 1000L;
       
    70         long[] nanoss = new long[jobs.length];
       
    71         for (int i = 0; i < jobs.length; i++) {
       
    72             collectAllGarbage();
       
    73             long t0 = System.nanoTime();
       
    74             long t;
       
    75             int j = 0;
       
    76             do { jobs[i].work(); j++; }
       
    77             while ((t = System.nanoTime() - t0) < warmupNanos);
       
    78             nanoss[i] = t/j;
       
    79         }
       
    80         return nanoss;
       
    81     }
       
    82 
       
    83     private static void time(Job ... jobs) throws Throwable {
       
    84 
       
    85         long[] warmup = time0(jobs); // Warm up run
       
    86         long[] nanoss = time0(jobs); // Real timing run
       
    87         long[] milliss = new long[jobs.length];
       
    88         double[] ratios = new double[jobs.length];
       
    89 
       
    90         final String nameHeader   = "Method";
       
    91         final String millisHeader = "Millis";
       
    92         final String ratioHeader  = "Ratio";
       
    93 
       
    94         int nameWidth   = nameHeader.length();
       
    95         int millisWidth = millisHeader.length();
       
    96         int ratioWidth  = ratioHeader.length();
       
    97 
       
    98         for (int i = 0; i < jobs.length; i++) {
       
    99             nameWidth = Math.max(nameWidth, jobs[i].name().length());
       
   100 
       
   101             milliss[i] = nanoss[i]/(1000L * 1000L);
       
   102             millisWidth = Math.max(millisWidth,
       
   103                                    String.format("%d", milliss[i]).length());
       
   104 
       
   105             ratios[i] = (double) nanoss[i] / (double) nanoss[0];
       
   106             ratioWidth = Math.max(ratioWidth,
       
   107                                   String.format("%.3f", ratios[i]).length());
       
   108         }
       
   109 
       
   110         String format = String.format("%%-%ds %%%dd %%%d.3f%%n",
       
   111                                       nameWidth, millisWidth, ratioWidth);
       
   112         String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",
       
   113                                             nameWidth, millisWidth, ratioWidth);
       
   114         System.out.printf(headerFormat, "Method", "Millis", "Ratio");
       
   115 
       
   116         // Print out absolute and relative times, calibrated against first job
       
   117         for (int i = 0; i < jobs.length; i++)
       
   118             System.out.printf(format, jobs[i].name(), milliss[i], ratios[i]);
       
   119     }
       
   120 
       
   121     private static String keywordValue(String[] args, String keyword) {
       
   122         for (String arg : args)
       
   123             if (arg.startsWith(keyword))
       
   124                 return arg.substring(keyword.length() + 1);
       
   125         return null;
       
   126     }
       
   127 
       
   128     private static int intArg(String[] args, String keyword, int defaultValue) {
       
   129         String val = keywordValue(args, keyword);
       
   130         return val == null ? defaultValue : Integer.parseInt(val);
       
   131     }
       
   132 
       
   133     private static Pattern patternArg(String[] args, String keyword) {
       
   134         String val = keywordValue(args, keyword);
       
   135         return val == null ? null : Pattern.compile(val);
       
   136     }
       
   137 
       
   138     private static Job[] filter(Pattern filter, Job[] jobs) {
       
   139         if (filter == null) return jobs;
       
   140         Job[] newJobs = new Job[jobs.length];
       
   141         int n = 0;
       
   142         for (Job job : jobs)
       
   143             if (filter.matcher(job.name()).find())
       
   144                 newJobs[n++] = job;
       
   145         // Arrays.copyOf not available in JDK 5
       
   146         Job[] ret = new Job[n];
       
   147         System.arraycopy(newJobs, 0, ret, 0, n);
       
   148         return ret;
       
   149     }
       
   150 
       
   151     private static void deoptimize(int sum) {
       
   152         if (sum == 42)
       
   153             System.out.println("the answer");
       
   154     }
       
   155 
       
   156     /**
       
   157      * Usage: [iterations=N] [size=N] [filter=REGEXP]
       
   158      */
       
   159     public static void main(String[] args) throws Throwable {
       
   160         final int iterations = intArg(args, "iterations", 10000);
       
   161         final int size       = intArg(args, "size", 1024);
       
   162         final Pattern filter = patternArg(args, "filter");
       
   163 
       
   164         final Random rnd = new Random();
       
   165 
       
   166         final ByteBuffer b = ByteBuffer.allocateDirect(8*size);
       
   167         for (int i = 0; i < b.limit(); i++)
       
   168             b.put(i, (byte) rnd.nextInt());
       
   169 
       
   170         Job[] jobs = {
       
   171             new Job("swap char BIG_ENDIAN") {
       
   172                 public void work() throws Throwable {
       
   173                     b.order(ByteOrder.BIG_ENDIAN);
       
   174                     CharBuffer x = b.asCharBuffer();
       
   175                     for (int i = 0; i < iterations; i++) {
       
   176                         int sum = 0;
       
   177                         for (int j = 0, end = x.limit(); j < end; j++)
       
   178                             sum += x.get(j);
       
   179                         deoptimize(sum);}}},
       
   180             new Job("swap char LITTLE_ENDIAN") {
       
   181                 public void work() throws Throwable {
       
   182                     b.order(ByteOrder.LITTLE_ENDIAN);
       
   183                     CharBuffer x = b.asCharBuffer();
       
   184                     for (int i = 0; i < iterations; i++) {
       
   185                         int sum = 0;
       
   186                         for (int j = 0, end = x.limit(); j < end; j++)
       
   187                             sum += x.get(j);
       
   188                         deoptimize(sum);}}},
       
   189             new Job("swap short BIG_ENDIAN") {
       
   190                 public void work() throws Throwable {
       
   191                     b.order(ByteOrder.BIG_ENDIAN);
       
   192                     ShortBuffer x = b.asShortBuffer();
       
   193                     for (int i = 0; i < iterations; i++) {
       
   194                         int sum = 0;
       
   195                         for (int j = 0, end = x.limit(); j < end; j++)
       
   196                             sum += x.get(j);
       
   197                         deoptimize(sum);}}},
       
   198             new Job("swap short LITTLE_ENDIAN") {
       
   199                 public void work() throws Throwable {
       
   200                     b.order(ByteOrder.LITTLE_ENDIAN);
       
   201                     ShortBuffer x = b.asShortBuffer();
       
   202                     for (int i = 0; i < iterations; i++) {
       
   203                         int sum = 0;
       
   204                         for (int j = 0, end = x.limit(); j < end; j++)
       
   205                             sum += x.get(j);
       
   206                         deoptimize(sum);}}},
       
   207             new Job("swap int BIG_ENDIAN") {
       
   208                 public void work() throws Throwable {
       
   209                     b.order(ByteOrder.BIG_ENDIAN);
       
   210                     IntBuffer x = b.asIntBuffer();
       
   211                     for (int i = 0; i < iterations; i++) {
       
   212                         int sum = 0;
       
   213                         for (int j = 0, end = x.limit(); j < end; j++)
       
   214                             sum += x.get(j);
       
   215                         deoptimize(sum);}}},
       
   216             new Job("swap int LITTLE_ENDIAN") {
       
   217                 public void work() throws Throwable {
       
   218                     b.order(ByteOrder.LITTLE_ENDIAN);
       
   219                     IntBuffer x = b.asIntBuffer();
       
   220                     for (int i = 0; i < iterations; i++) {
       
   221                         int sum = 0;
       
   222                         for (int j = 0, end = x.limit(); j < end; j++)
       
   223                             sum += x.get(j);
       
   224                         deoptimize(sum);}}},
       
   225             new Job("swap long BIG_ENDIAN") {
       
   226                 public void work() throws Throwable {
       
   227                     b.order(ByteOrder.BIG_ENDIAN);
       
   228                     LongBuffer x = b.asLongBuffer();
       
   229                     for (int i = 0; i < iterations; i++) {
       
   230                         int sum = 0;
       
   231                         for (int j = 0, end = x.limit(); j < end; j++)
       
   232                             sum += x.get(j);
       
   233                         deoptimize(sum);}}},
       
   234             new Job("swap long LITTLE_ENDIAN") {
       
   235                 public void work() throws Throwable {
       
   236                     b.order(ByteOrder.LITTLE_ENDIAN);
       
   237                     LongBuffer x = b.asLongBuffer();
       
   238                     for (int i = 0; i < iterations; i++) {
       
   239                         int sum = 0;
       
   240                         for (int j = 0, end = x.limit(); j < end; j++)
       
   241                             sum += x.get(j);
       
   242                         deoptimize(sum);}}}
       
   243         };
       
   244 
       
   245         time(filter(filter, jobs));
       
   246     }
       
   247 }