Skip to content

Instantly share code, notes, and snippets.

@plevart
Created September 16, 2021 09:26
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 plevart/3cdc7c366d03822c915a7b3ccd579421 to your computer and use it in GitHub Desktop.
Save plevart/3cdc7c366d03822c915a7b3ccd579421 to your computer and use it in GitHub Desktop.
/*
OpenJDK 18+9 - default
Benchmark Mode Cnt Score Error Units
ReflectionJacksonBenchmark.deserializeFields avgt 10 385.381 ± 8.143 ns/op
ReflectionJacksonBenchmark.deserializeMethods avgt 10 400.039 ± 4.914 ns/op
ReflectionJacksonBenchmark.serializeFields avgt 10 256.525 ± 2.686 ns/op
ReflectionJacksonBenchmark.serializeMethods avgt 10 257.759 ± 1.755 ns/op
OpenJDK 18-dev - mandy's variant (with MHInvoker & VHInvoker)
Benchmark Mode Cnt Score Error Units
ReflectionJacksonBenchmark.deserializeFields avgt 10 398.928 ± 3.766 ns/op
ReflectionJacksonBenchmark.deserializeMethods avgt 10 419.065 ± 0.509 ns/op
ReflectionJacksonBenchmark.serializeFields avgt 10 268.674 ± 0.369 ns/op
ReflectionJacksonBenchmark.serializeMethods avgt 10 268.849 ± 2.317 ns/op
OpenJDK 18-dev - peter's variant + vh2mh patch
Benchmark Mode Cnt Score Error Units
ReflectionJacksonBenchmark.deserializeFields avgt 10 399.284 ± 0.620 ns/op
ReflectionJacksonBenchmark.deserializeMethods avgt 10 417.347 ± 3.416 ns/op
ReflectionJacksonBenchmark.serializeFields avgt 10 261.795 ± 4.648 ns/op
ReflectionJacksonBenchmark.serializeMethods avgt 10 264.162 ± 3.226 ns/op
*/
package si.pele.jmh;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Fork(value = 1, warmups = 0)
public class ReflectionJacksonBenchmark {
public static class FieldRecord {
public String s1, s2, s3;
public int i1, i2, i3;
public FieldRecord() {
}
public FieldRecord(String s1, String s2, String s3, int i1, int i2, int i3) {
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.i1 = i1;
this.i2 = i2;
this.i3 = i3;
}
}
public static class PropertyRecord {
private String s1, s2, s3;
private int i1, i2, i3;
public PropertyRecord() {
}
public PropertyRecord(String s1, String s2, String s3, int i1, int i2, int i3) {
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.i1 = i1;
this.i2 = i2;
this.i3 = i3;
}
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
public String getS2() {
return s2;
}
public void setS2(String s2) {
this.s2 = s2;
}
public String getS3() {
return s3;
}
public void setS3(String s3) {
this.s3 = s3;
}
public int getI1() {
return i1;
}
public void setI1(int i1) {
this.i1 = i1;
}
public int getI2() {
return i2;
}
public void setI2(int i2) {
this.i2 = i2;
}
public int getI3() {
return i3;
}
public void setI3(int i3) {
this.i3 = i3;
}
}
static final ObjectMapper mapper = new ObjectMapper();
static final FieldRecord fieldRecord;
static final PropertyRecord propertyRecord;
static final byte[] json;
static {
fieldRecord = new FieldRecord("a", "b", "c", 123, 456, 789);
propertyRecord = new PropertyRecord("a", "b", "c", 123, 456, 789);
try {
json = mapper.writeValueAsBytes(fieldRecord);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@Benchmark
public FieldRecord deserializeFields() {
try {
return mapper.readValue(json, FieldRecord.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Benchmark
public byte[] serializeFields() {
try {
return mapper.writeValueAsBytes(fieldRecord);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@Benchmark
public PropertyRecord deserializeMethods() {
try {
return mapper.readValue(json, PropertyRecord.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Benchmark
public byte[] serializeMethods() {
try {
return mapper.writeValueAsBytes(propertyRecord);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment