Skip to content

Instantly share code, notes, and snippets.

@paul-bjorkstrand
Last active December 5, 2021 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul-bjorkstrand/f3bb154665e7d2168b4656eb7b794496 to your computer and use it in GitHub Desktop.
Save paul-bjorkstrand/f3bb154665e7d2168b4656eb7b794496 to your computer and use it in GitHub Desktop.
Benchmark (str) Mode Cnt Score Error Units
SynchronizedBenchmark.viaField str avgt 15 7.158 ± 0.181 ns/op
SynchronizedBenchmark.viaSetterMethod str avgt 15 7.802 ± 0.193 ns/op
SynchronizedBenchmark.viaMethodHandle_setterMethod str avgt 15 17.766 ± 0.551 ns/op
SynchronizedBenchmark.viaMethodHandle_field str avgt 15 17.939 ± 0.290 ns/op
SynchronizedBenchmark.viaReflection_field str avgt 15 157.453 ± 2.678 ns/op
SynchronizedBenchmark.viaReflection_setterMethod str avgt 15 157.346 ± 2.049 ns/op
SynchronizedBenchmark.viaReflection_setterMethod_synchronized str avgt 15 542.966 ± 19.825 ns/op
SynchronizedBenchmark.viaReflection_field_synchronized str avgt 15 771.381 ± 47.922 ns/op
Benchmark (str) Mode Cnt Score Error Units
SynchronizedBenchmark.viaField str avgt 15 7.302 ± 0.248 ns/op
SynchronizedBenchmark.viaSetterMethod str avgt 15 7.551 ± 0.177 ns/op
SynchronizedBenchmark.viaMethodHandle_setterMethod str avgt 15 16.988 ± 0.103 ns/op
SynchronizedBenchmark.viaMethodHandle_field str avgt 15 18.251 ± 0.901 ns/op
SynchronizedBenchmark.viaReflection_field str avgt 15 148.221 ± 37.669 ns/op
SynchronizedBenchmark.viaReflection_setterMethod str avgt 15 163.326 ± 3.015 ns/op
SynchronizedBenchmark.viaReflection_field_synchronized str avgt 15 659.883 ± 43.468 ns/op
SynchronizedBenchmark.viaReflection_setterMethod_synchronized str avgt 15 732.092 ± 112.718 ns/op
Benchmark (str) Mode Cnt Score Error Units
SynchronizedBenchmark.viaField str avgt 15 7.181 ± 0.175 ns/op
SynchronizedBenchmark.viaSetterMethod str avgt 15 7.897 ± 0.229 ns/op
SynchronizedBenchmark.viaMethodHandle_setterMethod str avgt 15 17.230 ± 0.373 ns/op
SynchronizedBenchmark.viaMethodHandle_field str avgt 15 17.893 ± 0.277 ns/op
SynchronizedBenchmark.viaReflection_field str avgt 15 145.418 ± 10.131 ns/op
SynchronizedBenchmark.viaReflection_setterMethod str avgt 15 147.527 ± 23.975 ns/op
SynchronizedBenchmark.viaReflection_field_synchronized str avgt 15 836.309 ± 101.923 ns/op
SynchronizedBenchmark.viaReflection_setterMethod_synchronized str avgt 15 842.271 ± 22.806 ns/op
/*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
package com.bjorkstrand;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(value = 3, jvmArgsAppend = {"-XX:+UseParallelGC", "-Xms1g", "-Xmx1g"})
@BenchmarkMode({Mode.AverageTime})
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@SuppressWarnings({"java:S100", "java:S112", "java:S1171", "deprecated", "java:S3011", "java:S1874"})
public class SynchronizedBenchmark {
@Param({"str"})
String str;
Field field;
Method setterMethod;
MethodHandle fieldMethodHandle;
MethodHandle setterMethodHandle;
@Setup
public void setup() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException {
field = StringHolder.class.getDeclaredField("s");
setterMethod = StringHolder.class.getDeclaredMethod("setS", String.class);
MethodHandles.Lookup lookup =
MethodHandles.privateLookupIn(StringHolder.class, MethodHandles.lookup());
fieldMethodHandle = lookup.findSetter(StringHolder.class, "s", String.class);
MethodType setterMethodType = MethodType.methodType(void.class, String.class);
setterMethodHandle = lookup.findVirtual(StringHolder.class, "setS", setterMethodType);
}
public static class StringHolder {
public String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
}
@Benchmark
@Threads(Threads.MAX)
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void viaField(Blackhole hole) {
StringHolder sh = new StringHolder();
sh.s = str;
hole.consume(sh.getS());
}
@Benchmark
@Threads(Threads.MAX)
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void viaSetterMethod(Blackhole hole) {
StringHolder sh = new StringHolder();
sh.setS(str);
hole.consume(sh.getS());
}
@Benchmark
@Threads(Threads.MAX)
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void viaReflection_field(Blackhole hole) throws IllegalAccessException {
StringHolder sh = new StringHolder();
field.setAccessible(true);
field.set(sh, str);
hole.consume(sh.getS());
}
@Benchmark
@Threads(Threads.MAX)
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void viaReflection_field_synchronized(Blackhole hole) throws IllegalAccessException {
StringHolder sh = new StringHolder();
synchronized (field) {
field.setAccessible(true);
field.set(sh, str);
field.setAccessible(false);
hole.consume(sh.getS());
}
}
@Benchmark
@Threads(Threads.MAX)
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void viaReflection_setterMethod(Blackhole hole) throws IllegalAccessException, InvocationTargetException {
StringHolder sh = new StringHolder();
setterMethod.setAccessible(true);
setterMethod.invoke(sh, str);
hole.consume(sh.getS());
}
@Benchmark
@Threads(Threads.MAX)
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void viaReflection_setterMethod_synchronized(Blackhole hole) throws IllegalAccessException, InvocationTargetException {
StringHolder sh = new StringHolder();
synchronized (setterMethod) {
setterMethod.setAccessible(true);
setterMethod.invoke(sh, str);
setterMethod.setAccessible(false);
hole.consume(sh.getS());
}
}
@Benchmark
@Threads(Threads.MAX)
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void viaMethodHandle_field(Blackhole hole) throws Throwable {
StringHolder sh = new StringHolder();
fieldMethodHandle.invoke(sh, str);
hole.consume(sh.getS());
}
@Benchmark
@Threads(Threads.MAX)
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void viaMethodHandle_setterMethod(Blackhole hole) throws Throwable {
StringHolder sh = new StringHolder();
setterMethodHandle.invoke(sh, str);
hole.consume(sh.getS());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment