0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This is not a regression test, but a micro-benchmark.
0N/A * To exercise swap, run with filter=LITTLE_ENDIAN on sparc,
0N/A * filter=BIG_ENDIAN on x86.
0N/A *
0N/A * I have run this as follows:
0N/A *
0N/A * for f in -client -server; do mergeBench dolphin . jr -dsa -da $f SwapMicroBenchmark.java filter=LITTLE_ENDIAN; done
0N/A *
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.nio.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport java.util.regex.Pattern;
0N/A
0N/Apublic class SwapMicroBenchmark {
0N/A abstract static class Job {
0N/A private final String name;
0N/A public Job(String name) { this.name = name; }
0N/A public String name() { return name; }
0N/A public abstract void work() throws Throwable;
0N/A }
0N/A
0N/A private static void collectAllGarbage() {
0N/A final java.util.concurrent.CountDownLatch drained
0N/A = new java.util.concurrent.CountDownLatch(1);
0N/A try {
0N/A System.gc(); // enqueue finalizable objects
0N/A new Object() { protected void finalize() {
0N/A drained.countDown(); }};
0N/A System.gc(); // enqueue detector
0N/A drained.await(); // wait for finalizer queue to drain
0N/A System.gc(); // cleanup finalized objects
0N/A } catch (InterruptedException e) { throw new Error(e); }
0N/A }
0N/A
0N/A /**
0N/A * Runs each job for long enough that all the runtime compilers
0N/A * have had plenty of time to warm up, i.e. get around to
0N/A * compiling everything worth compiling.
0N/A * Returns array of average times per job per run.
0N/A */
0N/A private static long[] time0(Job ... jobs) throws Throwable {
0N/A final long warmupNanos = 10L * 1000L * 1000L * 1000L;
0N/A long[] nanoss = new long[jobs.length];
0N/A for (int i = 0; i < jobs.length; i++) {
0N/A collectAllGarbage();
0N/A long t0 = System.nanoTime();
0N/A long t;
0N/A int j = 0;
0N/A do { jobs[i].work(); j++; }
0N/A while ((t = System.nanoTime() - t0) < warmupNanos);
0N/A nanoss[i] = t/j;
0N/A }
0N/A return nanoss;
0N/A }
0N/A
0N/A private static void time(Job ... jobs) throws Throwable {
0N/A
0N/A long[] warmup = time0(jobs); // Warm up run
0N/A long[] nanoss = time0(jobs); // Real timing run
0N/A long[] milliss = new long[jobs.length];
0N/A double[] ratios = new double[jobs.length];
0N/A
0N/A final String nameHeader = "Method";
0N/A final String millisHeader = "Millis";
0N/A final String ratioHeader = "Ratio";
0N/A
0N/A int nameWidth = nameHeader.length();
0N/A int millisWidth = millisHeader.length();
0N/A int ratioWidth = ratioHeader.length();
0N/A
0N/A for (int i = 0; i < jobs.length; i++) {
0N/A nameWidth = Math.max(nameWidth, jobs[i].name().length());
0N/A
0N/A milliss[i] = nanoss[i]/(1000L * 1000L);
0N/A millisWidth = Math.max(millisWidth,
0N/A String.format("%d", milliss[i]).length());
0N/A
0N/A ratios[i] = (double) nanoss[i] / (double) nanoss[0];
0N/A ratioWidth = Math.max(ratioWidth,
0N/A String.format("%.3f", ratios[i]).length());
0N/A }
0N/A
0N/A String format = String.format("%%-%ds %%%dd %%%d.3f%%n",
0N/A nameWidth, millisWidth, ratioWidth);
0N/A String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",
0N/A nameWidth, millisWidth, ratioWidth);
0N/A System.out.printf(headerFormat, "Method", "Millis", "Ratio");
0N/A
0N/A // Print out absolute and relative times, calibrated against first job
0N/A for (int i = 0; i < jobs.length; i++)
0N/A System.out.printf(format, jobs[i].name(), milliss[i], ratios[i]);
0N/A }
0N/A
0N/A private static String keywordValue(String[] args, String keyword) {
0N/A for (String arg : args)
0N/A if (arg.startsWith(keyword))
0N/A return arg.substring(keyword.length() + 1);
0N/A return null;
0N/A }
0N/A
0N/A private static int intArg(String[] args, String keyword, int defaultValue) {
0N/A String val = keywordValue(args, keyword);
0N/A return val == null ? defaultValue : Integer.parseInt(val);
0N/A }
0N/A
0N/A private static Pattern patternArg(String[] args, String keyword) {
0N/A String val = keywordValue(args, keyword);
0N/A return val == null ? null : Pattern.compile(val);
0N/A }
0N/A
0N/A private static Job[] filter(Pattern filter, Job[] jobs) {
0N/A if (filter == null) return jobs;
0N/A Job[] newJobs = new Job[jobs.length];
0N/A int n = 0;
0N/A for (Job job : jobs)
0N/A if (filter.matcher(job.name()).find())
0N/A newJobs[n++] = job;
0N/A // Arrays.copyOf not available in JDK 5
0N/A Job[] ret = new Job[n];
0N/A System.arraycopy(newJobs, 0, ret, 0, n);
0N/A return ret;
0N/A }
0N/A
0N/A private static void deoptimize(int sum) {
0N/A if (sum == 42)
0N/A System.out.println("the answer");
0N/A }
0N/A
0N/A /**
0N/A * Usage: [iterations=N] [size=N] [filter=REGEXP]
0N/A */
0N/A public static void main(String[] args) throws Throwable {
0N/A final int iterations = intArg(args, "iterations", 10000);
0N/A final int size = intArg(args, "size", 1024);
0N/A final Pattern filter = patternArg(args, "filter");
0N/A
0N/A final Random rnd = new Random();
0N/A
0N/A final ByteBuffer b = ByteBuffer.allocateDirect(8*size);
0N/A for (int i = 0; i < b.limit(); i++)
0N/A b.put(i, (byte) rnd.nextInt());
0N/A
0N/A Job[] jobs = {
0N/A new Job("swap char BIG_ENDIAN") {
0N/A public void work() throws Throwable {
0N/A b.order(ByteOrder.BIG_ENDIAN);
0N/A CharBuffer x = b.asCharBuffer();
0N/A for (int i = 0; i < iterations; i++) {
0N/A int sum = 0;
0N/A for (int j = 0, end = x.limit(); j < end; j++)
0N/A sum += x.get(j);
0N/A deoptimize(sum);}}},
0N/A new Job("swap char LITTLE_ENDIAN") {
0N/A public void work() throws Throwable {
0N/A b.order(ByteOrder.LITTLE_ENDIAN);
0N/A CharBuffer x = b.asCharBuffer();
0N/A for (int i = 0; i < iterations; i++) {
0N/A int sum = 0;
0N/A for (int j = 0, end = x.limit(); j < end; j++)
0N/A sum += x.get(j);
0N/A deoptimize(sum);}}},
0N/A new Job("swap short BIG_ENDIAN") {
0N/A public void work() throws Throwable {
0N/A b.order(ByteOrder.BIG_ENDIAN);
0N/A ShortBuffer x = b.asShortBuffer();
0N/A for (int i = 0; i < iterations; i++) {
0N/A int sum = 0;
0N/A for (int j = 0, end = x.limit(); j < end; j++)
0N/A sum += x.get(j);
0N/A deoptimize(sum);}}},
0N/A new Job("swap short LITTLE_ENDIAN") {
0N/A public void work() throws Throwable {
0N/A b.order(ByteOrder.LITTLE_ENDIAN);
0N/A ShortBuffer x = b.asShortBuffer();
0N/A for (int i = 0; i < iterations; i++) {
0N/A int sum = 0;
0N/A for (int j = 0, end = x.limit(); j < end; j++)
0N/A sum += x.get(j);
0N/A deoptimize(sum);}}},
0N/A new Job("swap int BIG_ENDIAN") {
0N/A public void work() throws Throwable {
0N/A b.order(ByteOrder.BIG_ENDIAN);
0N/A IntBuffer x = b.asIntBuffer();
0N/A for (int i = 0; i < iterations; i++) {
0N/A int sum = 0;
0N/A for (int j = 0, end = x.limit(); j < end; j++)
0N/A sum += x.get(j);
0N/A deoptimize(sum);}}},
0N/A new Job("swap int LITTLE_ENDIAN") {
0N/A public void work() throws Throwable {
0N/A b.order(ByteOrder.LITTLE_ENDIAN);
0N/A IntBuffer x = b.asIntBuffer();
0N/A for (int i = 0; i < iterations; i++) {
0N/A int sum = 0;
0N/A for (int j = 0, end = x.limit(); j < end; j++)
0N/A sum += x.get(j);
0N/A deoptimize(sum);}}},
0N/A new Job("swap long BIG_ENDIAN") {
0N/A public void work() throws Throwable {
0N/A b.order(ByteOrder.BIG_ENDIAN);
0N/A LongBuffer x = b.asLongBuffer();
0N/A for (int i = 0; i < iterations; i++) {
0N/A int sum = 0;
0N/A for (int j = 0, end = x.limit(); j < end; j++)
0N/A sum += x.get(j);
0N/A deoptimize(sum);}}},
0N/A new Job("swap long LITTLE_ENDIAN") {
0N/A public void work() throws Throwable {
0N/A b.order(ByteOrder.LITTLE_ENDIAN);
0N/A LongBuffer x = b.asLongBuffer();
0N/A for (int i = 0; i < iterations; i++) {
0N/A int sum = 0;
0N/A for (int j = 0, end = x.limit(); j < end; j++)
0N/A sum += x.get(j);
0N/A deoptimize(sum);}}}
0N/A };
0N/A
0N/A time(filter(filter, jobs));
0N/A }
0N/A}