Path: blob/master/test/jdk/java/nio/Buffer/SwapMicroBenchmark.java
41149 views
/*1* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* This is not a regression test, but a micro-benchmark.25* To exercise swap, run with filter=LITTLE_ENDIAN on sparc,26* filter=BIG_ENDIAN on x86.27*28* I have run this as follows:29*30* for f in -client -server; do mergeBench dolphin . jr -dsa -da $f SwapMicroBenchmark.java filter=LITTLE_ENDIAN; done31*32* @author Martin Buchholz33*/3435import java.util.*;36import java.nio.*;37import java.util.regex.Pattern;3839public class SwapMicroBenchmark {40abstract static class Job {41private final String name;42public Job(String name) { this.name = name; }43public String name() { return name; }44public abstract void work() throws Throwable;45}4647private static void collectAllGarbage() {48final java.util.concurrent.CountDownLatch drained49= new java.util.concurrent.CountDownLatch(1);50try {51System.gc(); // enqueue finalizable objects52new Object() { protected void finalize() {53drained.countDown(); }};54System.gc(); // enqueue detector55drained.await(); // wait for finalizer queue to drain56System.gc(); // cleanup finalized objects57} catch (InterruptedException e) { throw new Error(e); }58}5960/**61* Runs each job for long enough that all the runtime compilers62* have had plenty of time to warm up, i.e. get around to63* compiling everything worth compiling.64* Returns array of average times per job per run.65*/66private static long[] time0(Job ... jobs) throws Throwable {67final long warmupNanos = 10L * 1000L * 1000L * 1000L;68long[] nanoss = new long[jobs.length];69for (int i = 0; i < jobs.length; i++) {70collectAllGarbage();71long t0 = System.nanoTime();72long t;73int j = 0;74do { jobs[i].work(); j++; }75while ((t = System.nanoTime() - t0) < warmupNanos);76nanoss[i] = t/j;77}78return nanoss;79}8081private static void time(Job ... jobs) throws Throwable {8283long[] warmup = time0(jobs); // Warm up run84long[] nanoss = time0(jobs); // Real timing run85long[] milliss = new long[jobs.length];86double[] ratios = new double[jobs.length];8788final String nameHeader = "Method";89final String millisHeader = "Millis";90final String ratioHeader = "Ratio";9192int nameWidth = nameHeader.length();93int millisWidth = millisHeader.length();94int ratioWidth = ratioHeader.length();9596for (int i = 0; i < jobs.length; i++) {97nameWidth = Math.max(nameWidth, jobs[i].name().length());9899milliss[i] = nanoss[i]/(1000L * 1000L);100millisWidth = Math.max(millisWidth,101String.format("%d", milliss[i]).length());102103ratios[i] = (double) nanoss[i] / (double) nanoss[0];104ratioWidth = Math.max(ratioWidth,105String.format("%.3f", ratios[i]).length());106}107108String format = String.format("%%-%ds %%%dd %%%d.3f%%n",109nameWidth, millisWidth, ratioWidth);110String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",111nameWidth, millisWidth, ratioWidth);112System.out.printf(headerFormat, "Method", "Millis", "Ratio");113114// Print out absolute and relative times, calibrated against first job115for (int i = 0; i < jobs.length; i++)116System.out.printf(format, jobs[i].name(), milliss[i], ratios[i]);117}118119private static String keywordValue(String[] args, String keyword) {120for (String arg : args)121if (arg.startsWith(keyword))122return arg.substring(keyword.length() + 1);123return null;124}125126private static int intArg(String[] args, String keyword, int defaultValue) {127String val = keywordValue(args, keyword);128return val == null ? defaultValue : Integer.parseInt(val);129}130131private static Pattern patternArg(String[] args, String keyword) {132String val = keywordValue(args, keyword);133return val == null ? null : Pattern.compile(val);134}135136private static Job[] filter(Pattern filter, Job[] jobs) {137if (filter == null) return jobs;138Job[] newJobs = new Job[jobs.length];139int n = 0;140for (Job job : jobs)141if (filter.matcher(job.name()).find())142newJobs[n++] = job;143// Arrays.copyOf not available in JDK 5144Job[] ret = new Job[n];145System.arraycopy(newJobs, 0, ret, 0, n);146return ret;147}148149private static void deoptimize(int sum) {150if (sum == 42)151System.out.println("the answer");152}153154/**155* Usage: [iterations=N] [size=N] [filter=REGEXP]156*/157public static void main(String[] args) throws Throwable {158final int iterations = intArg(args, "iterations", 10000);159final int size = intArg(args, "size", 1024);160final Pattern filter = patternArg(args, "filter");161162final Random rnd = new Random();163164final ByteBuffer b = ByteBuffer.allocateDirect(8*size);165for (int i = 0; i < b.limit(); i++)166b.put(i, (byte) rnd.nextInt());167168Job[] jobs = {169new Job("swap char BIG_ENDIAN") {170public void work() throws Throwable {171b.order(ByteOrder.BIG_ENDIAN);172CharBuffer x = b.asCharBuffer();173for (int i = 0; i < iterations; i++) {174int sum = 0;175for (int j = 0, end = x.limit(); j < end; j++)176sum += x.get(j);177deoptimize(sum);}}},178new Job("swap char LITTLE_ENDIAN") {179public void work() throws Throwable {180b.order(ByteOrder.LITTLE_ENDIAN);181CharBuffer x = b.asCharBuffer();182for (int i = 0; i < iterations; i++) {183int sum = 0;184for (int j = 0, end = x.limit(); j < end; j++)185sum += x.get(j);186deoptimize(sum);}}},187new Job("swap short BIG_ENDIAN") {188public void work() throws Throwable {189b.order(ByteOrder.BIG_ENDIAN);190ShortBuffer x = b.asShortBuffer();191for (int i = 0; i < iterations; i++) {192int sum = 0;193for (int j = 0, end = x.limit(); j < end; j++)194sum += x.get(j);195deoptimize(sum);}}},196new Job("swap short LITTLE_ENDIAN") {197public void work() throws Throwable {198b.order(ByteOrder.LITTLE_ENDIAN);199ShortBuffer x = b.asShortBuffer();200for (int i = 0; i < iterations; i++) {201int sum = 0;202for (int j = 0, end = x.limit(); j < end; j++)203sum += x.get(j);204deoptimize(sum);}}},205new Job("swap int BIG_ENDIAN") {206public void work() throws Throwable {207b.order(ByteOrder.BIG_ENDIAN);208IntBuffer x = b.asIntBuffer();209for (int i = 0; i < iterations; i++) {210int sum = 0;211for (int j = 0, end = x.limit(); j < end; j++)212sum += x.get(j);213deoptimize(sum);}}},214new Job("swap int LITTLE_ENDIAN") {215public void work() throws Throwable {216b.order(ByteOrder.LITTLE_ENDIAN);217IntBuffer x = b.asIntBuffer();218for (int i = 0; i < iterations; i++) {219int sum = 0;220for (int j = 0, end = x.limit(); j < end; j++)221sum += x.get(j);222deoptimize(sum);}}},223new Job("swap long BIG_ENDIAN") {224public void work() throws Throwable {225b.order(ByteOrder.BIG_ENDIAN);226LongBuffer x = b.asLongBuffer();227for (int i = 0; i < iterations; i++) {228int sum = 0;229for (int j = 0, end = x.limit(); j < end; j++)230sum += x.get(j);231deoptimize(sum);}}},232new Job("swap long LITTLE_ENDIAN") {233public void work() throws Throwable {234b.order(ByteOrder.LITTLE_ENDIAN);235LongBuffer x = b.asLongBuffer();236for (int i = 0; i < iterations; i++) {237int sum = 0;238for (int j = 0, end = x.limit(); j < end; j++)239sum += x.get(j);240deoptimize(sum);}}}241};242243time(filter(filter, jobs));244}245}246247248