Created
October 30, 2015 17:28
-
-
Save cberner/c141e651cb45288fba24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OperationsPerInvocation; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.Warmup; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import org.openjdk.jmh.runner.options.VerboseMode; | |
import java.lang.invoke.MethodHandle; | |
import java.lang.invoke.MethodHandles; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.ThreadLocalRandom; | |
import java.util.concurrent.TimeUnit; | |
@SuppressWarnings("MethodMayBeStatic") | |
@State(Scope.Thread) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@Fork(1) | |
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) | |
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) | |
@BenchmarkMode(Mode.AverageTime) | |
public class BenchmarkInvoke | |
{ | |
private static final int TOTAL_ELEMENTS = 10_000_000; | |
private static final int NUM_TYPES = 3; | |
@Benchmark | |
@OperationsPerInvocation(TOTAL_ELEMENTS * NUM_TYPES) | |
public Object boundInvokeExact(BenchmarkData data) | |
throws Throwable | |
{ | |
long result = 0; | |
MethodHandle a = data.getLoopingMethodHandles().get(0); | |
MethodHandle b = data.getLoopingMethodHandles().get(1); | |
MethodHandle c = data.getLoopingMethodHandles().get(2); | |
for (long[] values : data.getData()) { | |
result += (long) a.invokeExact(values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) b.invokeExact(values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) c.invokeExact(values); | |
} | |
return result; | |
} | |
@Benchmark | |
@OperationsPerInvocation(TOTAL_ELEMENTS * NUM_TYPES) | |
public Object localBoundInvokeExact(BenchmarkData data) | |
throws Throwable | |
{ | |
long result = 0; | |
MethodHandle loop = data.getLoopMethodHandle(); | |
MethodHandle a = loop.bindTo(data.getInnerMethodHandles().get(0)); | |
MethodHandle b = loop.bindTo(data.getInnerMethodHandles().get(1)); | |
MethodHandle c = loop.bindTo(data.getInnerMethodHandles().get(2)); | |
for (long[] values : data.getData()) { | |
result += (long) a.invokeExact(values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) b.invokeExact(values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) c.invokeExact(values); | |
} | |
return result; | |
} | |
@Benchmark | |
@OperationsPerInvocation(TOTAL_ELEMENTS * NUM_TYPES) | |
public Object forceLambdaFormInvokeExact(BenchmarkData data) | |
throws Throwable | |
{ | |
long result = 0; | |
MethodHandle loopA = data.getLambdaFormLoopMethodHandles().get(0); | |
MethodHandle loopB = data.getLambdaFormLoopMethodHandles().get(1); | |
MethodHandle loopC = data.getLambdaFormLoopMethodHandles().get(2); | |
MethodHandle a = data.getInnerMethodHandles().get(0); | |
MethodHandle b = data.getInnerMethodHandles().get(1); | |
MethodHandle c = data.getInnerMethodHandles().get(2); | |
for (long[] values : data.getData()) { | |
result += (long) loopA.invokeExact(a, values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) loopB.invokeExact(b, values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) loopC.invokeExact(c, values); | |
} | |
return result; | |
} | |
@Benchmark | |
@OperationsPerInvocation(TOTAL_ELEMENTS * NUM_TYPES) | |
public Object multipleInvokeExact(BenchmarkData data) | |
throws Throwable | |
{ | |
long result = 0; | |
MethodHandle loopA = data.getLoopMethodHandles().get(0); | |
MethodHandle loopB = data.getLoopMethodHandles().get(1); | |
MethodHandle loopC = data.getLoopMethodHandles().get(2); | |
MethodHandle a = data.getInnerMethodHandles().get(0); | |
MethodHandle b = data.getInnerMethodHandles().get(1); | |
MethodHandle c = data.getInnerMethodHandles().get(2); | |
for (long[] values : data.getData()) { | |
result += (long) loopA.invokeExact(a, values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) loopB.invokeExact(b, values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) loopC.invokeExact(c, values); | |
} | |
return result; | |
} | |
@Benchmark | |
@OperationsPerInvocation(TOTAL_ELEMENTS * NUM_TYPES) | |
public Object invokeExact(BenchmarkData data) | |
throws Throwable | |
{ | |
long result = 0; | |
MethodHandle loop = data.getLoopMethodHandle(); | |
MethodHandle a = data.getInnerMethodHandles().get(0); | |
MethodHandle b = data.getInnerMethodHandles().get(1); | |
MethodHandle c = data.getInnerMethodHandles().get(2); | |
for (long[] values : data.getData()) { | |
result += (long) loop.invokeExact(a, values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) loop.invokeExact(b, values); | |
} | |
for (long[] values : data.getData()) { | |
result += (long) loop.invokeExact(c, values); | |
} | |
return result; | |
} | |
@Benchmark | |
@OperationsPerInvocation(TOTAL_ELEMENTS * NUM_TYPES) | |
public Object multipleInvokeStatic(BenchmarkData data) | |
throws Throwable | |
{ | |
long result = 0; | |
MethodHandle a = data.getInnerMethodHandles().get(0); | |
MethodHandle b = data.getInnerMethodHandles().get(1); | |
MethodHandle c = data.getInnerMethodHandles().get(2); | |
for (long[] values : data.getData()) { | |
result += loopA(a, values); | |
} | |
for (long[] values : data.getData()) { | |
result += loopB(b, values); | |
} | |
for (long[] values : data.getData()) { | |
result += loopC(c, values); | |
} | |
return result; | |
} | |
@Benchmark | |
@OperationsPerInvocation(TOTAL_ELEMENTS * NUM_TYPES) | |
public Object invokeStatic(BenchmarkData data) | |
throws Throwable | |
{ | |
long result = 0; | |
MethodHandle a = data.getInnerMethodHandles().get(0); | |
MethodHandle b = data.getInnerMethodHandles().get(1); | |
MethodHandle c = data.getInnerMethodHandles().get(2); | |
for (long[] values : data.getData()) { | |
result += loop(a, values); | |
} | |
for (long[] values : data.getData()) { | |
result += loop(b, values); | |
} | |
for (long[] values : data.getData()) { | |
result += loop(c, values); | |
} | |
return result; | |
} | |
@Benchmark | |
@OperationsPerInvocation(TOTAL_ELEMENTS * NUM_TYPES) | |
public Object fast(BenchmarkData data) | |
throws Throwable | |
{ | |
long result = 0; | |
for (long[] values : data.getData()) { | |
result += fastLoopA(values); | |
} | |
for (long[] values : data.getData()) { | |
result += fastLoopB(values); | |
} | |
for (long[] values : data.getData()) { | |
result += fastLoopC(values); | |
} | |
return result; | |
} | |
@SuppressWarnings("FieldMayBeFinal") | |
@State(Scope.Thread) | |
public static class BenchmarkData | |
{ | |
@Param({"10000", "100000"}) | |
private int arrays = 1000; | |
private long[][] data; | |
private List<MethodHandle> loopingMethodHandles; | |
private List<MethodHandle> innerMethodHandles; | |
private MethodHandle loopMethodHandle; | |
private List<MethodHandle> loopMethodHandles; | |
private List<MethodHandle> lambdaFormLoopMethodHandles; | |
@Setup | |
public void setup() | |
throws NoSuchMethodException, IllegalAccessException | |
{ | |
MethodHandle a = methodHandle(BenchmarkInvoke.class, "methodA", long.class); | |
MethodHandle b = methodHandle(BenchmarkInvoke.class, "methodB", long.class); | |
MethodHandle c = methodHandle(BenchmarkInvoke.class, "methodC", long.class); | |
innerMethodHandles = new ArrayList<>(); | |
innerMethodHandles.add(a); | |
innerMethodHandles.add(b); | |
innerMethodHandles.add(c); | |
loopMethodHandle = methodHandle(BenchmarkInvoke.class, "loop", MethodHandle.class, long[].class); | |
loopingMethodHandles = new ArrayList<>(); | |
loopingMethodHandles.add(loopMethodHandle.bindTo(a)); | |
loopingMethodHandles.add(loopMethodHandle.bindTo(b)); | |
loopingMethodHandles.add(loopMethodHandle.bindTo(c)); | |
loopMethodHandles = new ArrayList<>(); | |
loopMethodHandles.add(methodHandle(BenchmarkInvoke.class, "loopA", MethodHandle.class, long[].class)); | |
loopMethodHandles.add(methodHandle(BenchmarkInvoke.class, "loopB", MethodHandle.class, long[].class)); | |
loopMethodHandles.add(methodHandle(BenchmarkInvoke.class, "loopC", MethodHandle.class, long[].class)); | |
lambdaFormLoopMethodHandles = new ArrayList<>(); | |
MethodHandle lambdaFormLoopMethodHandle = methodHandle(BenchmarkInvoke.class, "forceLambdaFormLoop", Object.class, MethodHandle.class, long[].class); | |
lambdaFormLoopMethodHandles.add(lambdaFormLoopMethodHandle.bindTo(0)); | |
lambdaFormLoopMethodHandles.add(lambdaFormLoopMethodHandle.bindTo(1)); | |
lambdaFormLoopMethodHandles.add(lambdaFormLoopMethodHandle.bindTo(2)); | |
int elementsPerArray = TOTAL_ELEMENTS / arrays; | |
data = new long[arrays][]; | |
for (int i = 0; i < arrays; i++) { | |
data[i] = new long[elementsPerArray]; | |
for (int j = 0; j < elementsPerArray; j++) { | |
data[i][j] = ThreadLocalRandom.current().nextLong(); | |
} | |
} | |
} | |
public long[][] getData() | |
{ | |
return data; | |
} | |
public List<MethodHandle> getLoopingMethodHandles() | |
{ | |
return loopingMethodHandles; | |
} | |
public List<MethodHandle> getInnerMethodHandles() | |
{ | |
return innerMethodHandles; | |
} | |
public MethodHandle getLoopMethodHandle() | |
{ | |
return loopMethodHandle; | |
} | |
public List<MethodHandle> getLambdaFormLoopMethodHandles() | |
{ | |
return lambdaFormLoopMethodHandles; | |
} | |
public List<MethodHandle> getLoopMethodHandles() | |
{ | |
return loopMethodHandles; | |
} | |
} | |
public static void main(String[] args) | |
throws Throwable | |
{ | |
Options options = new OptionsBuilder() | |
.verbosity(VerboseMode.NORMAL) | |
.include(".*" + BenchmarkInvoke.class.getSimpleName() + ".*") | |
.build(); | |
new Runner(options).run(); | |
} | |
public static long forceLambdaFormLoop(Object forceLamdbaForm, MethodHandle methodHandle, long[] values) | |
throws Throwable | |
{ | |
long result = 0; | |
for (long value : values) { | |
result += (long) methodHandle.invokeExact(value); | |
} | |
return result; | |
} | |
public static long loop(MethodHandle methodHandle, long[] values) | |
throws Throwable | |
{ | |
long result = 0; | |
for (long value : values) { | |
result += (long) methodHandle.invokeExact(value); | |
} | |
return result; | |
} | |
public static long loopA(MethodHandle methodHandle, long[] values) | |
throws Throwable | |
{ | |
long result = 0; | |
for (long value : values) { | |
result += (long) methodHandle.invokeExact(value); | |
} | |
return result; | |
} | |
public static long loopB(MethodHandle methodHandle, long[] values) | |
throws Throwable | |
{ | |
long result = 0; | |
for (long value : values) { | |
result += (long) methodHandle.invokeExact(value); | |
} | |
return result; | |
} | |
public static long loopC(MethodHandle methodHandle, long[] values) | |
throws Throwable | |
{ | |
long result = 0; | |
for (long value : values) { | |
result += (long) methodHandle.invokeExact(value); | |
} | |
return result; | |
} | |
public static long methodA(long value) | |
{ | |
return value + 1; | |
} | |
public static long methodB(long value) | |
{ | |
return value + 2; | |
} | |
public static long methodC(long value) | |
{ | |
return value + 3; | |
} | |
public static long fastLoopA(long[] values) | |
{ | |
long result = 0; | |
for (long value : values) { | |
result += methodA(value); | |
} | |
return result; | |
} | |
public static long fastLoopB(long[] values) | |
{ | |
long result = 0; | |
for (long value : values) { | |
result += methodB(value); | |
} | |
return result; | |
} | |
public static long fastLoopC(long[] values) | |
{ | |
long result = 0; | |
for (long value : values) { | |
result += methodC(value); | |
} | |
return result; | |
} | |
private static MethodHandle methodHandle(Class<?> clazz, String name, Class<?>... parameterTypes) | |
throws NoSuchMethodException, IllegalAccessException | |
{ | |
return MethodHandles.lookup().unreflect(clazz.getMethod(name, parameterTypes)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment