Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/Buffer/SwapMicroBenchmark.java
38813 views
/*1* Copyright (c) 2007, 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.concurrent.*;38import java.util.regex.Pattern;3940public class SwapMicroBenchmark {41abstract static class Job {42private final String name;43public Job(String name) { this.name = name; }44public String name() { return name; }45public abstract void work() throws Throwable;46}4748private static void collectAllGarbage() {49final java.util.concurrent.CountDownLatch drained50= new java.util.concurrent.CountDownLatch(1);51try {52System.gc(); // enqueue finalizable objects53new Object() { protected void finalize() {54drained.countDown(); }};55System.gc(); // enqueue detector56drained.await(); // wait for finalizer queue to drain57System.gc(); // cleanup finalized objects58} catch (InterruptedException e) { throw new Error(e); }59}6061/**62* Runs each job for long enough that all the runtime compilers63* have had plenty of time to warm up, i.e. get around to64* compiling everything worth compiling.65* Returns array of average times per job per run.66*/67private static long[] time0(Job ... jobs) throws Throwable {68final long warmupNanos = 10L * 1000L * 1000L * 1000L;69long[] nanoss = new long[jobs.length];70for (int i = 0; i < jobs.length; i++) {71collectAllGarbage();72long t0 = System.nanoTime();73long t;74int j = 0;75do { jobs[i].work(); j++; }76while ((t = System.nanoTime() - t0) < warmupNanos);77nanoss[i] = t/j;78}79return nanoss;80}8182private static void time(Job ... jobs) throws Throwable {8384long[] warmup = time0(jobs); // Warm up run85long[] nanoss = time0(jobs); // Real timing run86long[] milliss = new long[jobs.length];87double[] ratios = new double[jobs.length];8889final String nameHeader = "Method";90final String millisHeader = "Millis";91final String ratioHeader = "Ratio";9293int nameWidth = nameHeader.length();94int millisWidth = millisHeader.length();95int ratioWidth = ratioHeader.length();9697for (int i = 0; i < jobs.length; i++) {98nameWidth = Math.max(nameWidth, jobs[i].name().length());99100milliss[i] = nanoss[i]/(1000L * 1000L);101millisWidth = Math.max(millisWidth,102String.format("%d", milliss[i]).length());103104ratios[i] = (double) nanoss[i] / (double) nanoss[0];105ratioWidth = Math.max(ratioWidth,106String.format("%.3f", ratios[i]).length());107}108109String format = String.format("%%-%ds %%%dd %%%d.3f%%n",110nameWidth, millisWidth, ratioWidth);111String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",112nameWidth, millisWidth, ratioWidth);113System.out.printf(headerFormat, "Method", "Millis", "Ratio");114115// Print out absolute and relative times, calibrated against first job116for (int i = 0; i < jobs.length; i++)117System.out.printf(format, jobs[i].name(), milliss[i], ratios[i]);118}119120private static String keywordValue(String[] args, String keyword) {121for (String arg : args)122if (arg.startsWith(keyword))123return arg.substring(keyword.length() + 1);124return null;125}126127private static int intArg(String[] args, String keyword, int defaultValue) {128String val = keywordValue(args, keyword);129return val == null ? defaultValue : Integer.parseInt(val);130}131132private static Pattern patternArg(String[] args, String keyword) {133String val = keywordValue(args, keyword);134return val == null ? null : Pattern.compile(val);135}136137private static Job[] filter(Pattern filter, Job[] jobs) {138if (filter == null) return jobs;139Job[] newJobs = new Job[jobs.length];140int n = 0;141for (Job job : jobs)142if (filter.matcher(job.name()).find())143newJobs[n++] = job;144// Arrays.copyOf not available in JDK 5145Job[] ret = new Job[n];146System.arraycopy(newJobs, 0, ret, 0, n);147return ret;148}149150private static void deoptimize(int sum) {151if (sum == 42)152System.out.println("the answer");153}154155/**156* Usage: [iterations=N] [size=N] [filter=REGEXP]157*/158public static void main(String[] args) throws Throwable {159final int iterations = intArg(args, "iterations", 10000);160final int size = intArg(args, "size", 1024);161final Pattern filter = patternArg(args, "filter");162163final Random rnd = new Random();164165final ByteBuffer b = ByteBuffer.allocateDirect(8*size);166for (int i = 0; i < b.limit(); i++)167b.put(i, (byte) rnd.nextInt());168169Job[] jobs = {170new Job("swap char BIG_ENDIAN") {171public void work() throws Throwable {172b.order(ByteOrder.BIG_ENDIAN);173CharBuffer x = b.asCharBuffer();174for (int i = 0; i < iterations; i++) {175int sum = 0;176for (int j = 0, end = x.limit(); j < end; j++)177sum += x.get(j);178deoptimize(sum);}}},179new Job("swap char LITTLE_ENDIAN") {180public void work() throws Throwable {181b.order(ByteOrder.LITTLE_ENDIAN);182CharBuffer x = b.asCharBuffer();183for (int i = 0; i < iterations; i++) {184int sum = 0;185for (int j = 0, end = x.limit(); j < end; j++)186sum += x.get(j);187deoptimize(sum);}}},188new Job("swap short BIG_ENDIAN") {189public void work() throws Throwable {190b.order(ByteOrder.BIG_ENDIAN);191ShortBuffer x = b.asShortBuffer();192for (int i = 0; i < iterations; i++) {193int sum = 0;194for (int j = 0, end = x.limit(); j < end; j++)195sum += x.get(j);196deoptimize(sum);}}},197new Job("swap short LITTLE_ENDIAN") {198public void work() throws Throwable {199b.order(ByteOrder.LITTLE_ENDIAN);200ShortBuffer x = b.asShortBuffer();201for (int i = 0; i < iterations; i++) {202int sum = 0;203for (int j = 0, end = x.limit(); j < end; j++)204sum += x.get(j);205deoptimize(sum);}}},206new Job("swap int BIG_ENDIAN") {207public void work() throws Throwable {208b.order(ByteOrder.BIG_ENDIAN);209IntBuffer x = b.asIntBuffer();210for (int i = 0; i < iterations; i++) {211int sum = 0;212for (int j = 0, end = x.limit(); j < end; j++)213sum += x.get(j);214deoptimize(sum);}}},215new Job("swap int LITTLE_ENDIAN") {216public void work() throws Throwable {217b.order(ByteOrder.LITTLE_ENDIAN);218IntBuffer x = b.asIntBuffer();219for (int i = 0; i < iterations; i++) {220int sum = 0;221for (int j = 0, end = x.limit(); j < end; j++)222sum += x.get(j);223deoptimize(sum);}}},224new Job("swap long BIG_ENDIAN") {225public void work() throws Throwable {226b.order(ByteOrder.BIG_ENDIAN);227LongBuffer x = b.asLongBuffer();228for (int i = 0; i < iterations; i++) {229int sum = 0;230for (int j = 0, end = x.limit(); j < end; j++)231sum += x.get(j);232deoptimize(sum);}}},233new Job("swap long LITTLE_ENDIAN") {234public void work() throws Throwable {235b.order(ByteOrder.LITTLE_ENDIAN);236LongBuffer x = b.asLongBuffer();237for (int i = 0; i < iterations; i++) {238int sum = 0;239for (int j = 0, end = x.limit(); j < end; j++)240sum += x.get(j);241deoptimize(sum);}}}242};243244time(filter(filter, jobs));245}246}247248249