-
-
Save vepo/c879bc8ca65719b46e1638f9bbb754a2 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
import java.util.List; | |
import java.util.Map; | |
import java.util.Objects; | |
import java.util.Random; | |
import java.util.stream.IntStream; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
import com.fasterxml.jackson.databind.DeserializationFeature; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.datatype.guava.GuavaModule; | |
import com.google.common.collect.HashMultimap; | |
public class SerializerBenchmark { | |
public record GuavaRecord(String name, int age, HashMultimap<String, byte[]> headers) { | |
} | |
public record PlainRecord(String name, int age, Map<String, List<byte[]>> headers) { | |
} | |
private static ObjectMapper guavaMapper = new ObjectMapper().setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL) | |
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | |
.registerModule(new GuavaModule()); | |
private static ObjectMapper plainMapper = new ObjectMapper().setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL) | |
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | |
private static List<GuavaRecord> guavaRecords = List.of(new GuavaRecord("name-1", 1, guavaHeader(1)), | |
new GuavaRecord("name-2", 2, guavaHeader(2)), | |
new GuavaRecord("name-3", 3, guavaHeader(3))); | |
private static List<PlainRecord> plainRecords = List.of(new PlainRecord("name-1", 1, plainHeader(1)), | |
new PlainRecord("name-2", 2, plainHeader(2)), | |
new PlainRecord("name-3", 3, plainHeader(3))); | |
private static Random random = new Random(); | |
public static void main(String[] args) throws Exception { | |
// var recordGuava3 = new GuavaRecord("name-3", 3, guavaHeader(3)); | |
// var recordPlain3 = new PlainRecord("name-3", 3, plainHeader(3)); | |
// System.out.println(guavaMapper.writeValueAsString(recordGuava3)); | |
// System.out.println(plainMapper.writeValueAsString(recordPlain3)); | |
org.openjdk.jmh.Main.main(args); | |
} | |
private static Map<String, List<byte[]>> plainHeader(int counter) { | |
return Map.of("key", IntStream.range(0, counter).mapToObj(i -> randomBytes()).toList()); | |
} | |
private static byte[] randomBytes() { | |
byte[] b = new byte[10]; | |
if (random == null) { | |
random = new Random(); | |
} | |
random.nextBytes(b); | |
return b; | |
} | |
private static HashMultimap<String, byte[]> guavaHeader(int counter) { | |
HashMultimap<String, byte[]> headers = HashMultimap.create(); | |
IntStream.range(0, counter).forEach(i -> headers.put("key", randomBytes())); | |
return headers; | |
} | |
@Benchmark | |
public void guavaSerializer() { | |
guavaRecords.forEach(r -> { | |
try { | |
var result = guavaMapper.writeValueAsString(r); | |
Objects.requireNonNull(result, "Result is null"); | |
var obj = guavaMapper.readValue(result, GuavaRecord.class); | |
Objects.requireNonNull(obj, "Object is null"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(-1); | |
} | |
}); | |
} | |
@Benchmark | |
public void plainSerializer() { | |
plainRecords.forEach(r -> { | |
try { | |
var result = plainMapper.writeValueAsString(r); | |
Objects.requireNonNull(result, "Result is null"); | |
var obj = plainMapper.readValue(result, PlainRecord.class); | |
Objects.requireNonNull(obj, "Object is null"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(-1); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment