Skip to content

Instantly share code, notes, and snippets.

@CodingFabian
Created December 1, 2019 09:11
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 CodingFabian/4d3612a256628fd25521f4946389f133 to your computer and use it in GitHub Desktop.
Save CodingFabian/4d3612a256628fd25521f4946389f133 to your computer and use it in GitHub Desktop.
package com.fasterxml.jackson.benchmark;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Threads;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import com.instana.DoubleUtil;
public class ContentionBenchmark {
static final MyPojo pojo;
static final ObjectWriter objectWriter;
static {
pojo = new MyPojo(Arrays.asList(new SubTypeA("1", "1"), new SubTypeB("2", "2"), new SubTypeA("3", "3")));
objectWriter = configureObjectMapper(new ObjectMapper()).writerFor(MyPojo.class);
}
public static ObjectMapper configureObjectMapper(ObjectMapper mapper) {
// These are the properties we use. I doubt they have any effect, but included for completeness
mapper.configure(DeserializationFeature.USE_LONG_FOR_INTS, true);
mapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.getFactory().enable(JsonParser.Feature.ALLOW_MISSING_VALUES);
mapper.getFactory().disable(JsonFactory.Feature.INTERN_FIELD_NAMES);
mapper.getFactory().disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES);
SimpleModule module = new SimpleModule();
module.addAbstractTypeMapping(Map.class, HashMap.class);
module.addSerializer(Double.class, new DoubleSerializer());
mapper.registerModule(module);
mapper.registerModule(new AfterburnerModule());
return mapper;
}
public static class DoubleSerializer extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeNumber(DoubleUtil.toString(value.doubleValue(), 4));
}
}
@Benchmark
@Threads(4)
public byte[] benchContention() throws JsonProcessingException {
return objectWriter.writeValueAsBytes(pojo);
}
}
package com.fasterxml.jackson.benchmark;
import java.util.List;
public class MyPojo {
final List<PolyType> list;
public MyPojo(List<PolyType> list) {
this.list = list;
}
public List<PolyType> getList() {
return list;
}
}
package com.fasterxml.jackson.benchmark;
public abstract class PolyType {
public String name;
public PolyType(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package com.fasterxml.jackson.benchmark;
public class SubTypeA extends PolyType {
private String attrib;
public SubTypeA(String name, String attrib) {
super(name);
this.attrib = attrib;
}
public String getAttrib() {
return attrib;
}
}
package com.fasterxml.jackson.benchmark;
public class SubTypeB extends PolyType {
private String attrib;
public SubTypeB(String name, String attrib) {
super(name);
this.attrib = attrib;
}
public String getAttrib() {
return attrib;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment