Last active
July 18, 2022 10:12
-
-
Save ShakalakaB/cf2e6916034ca817187d32eb011b5dfa to your computer and use it in GitHub Desktop.
objectmapper-first-initializaiton-execution-slow
This file contains hidden or 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
String json = "{\"name\":\"car2\",\"keyA\":1,\"keyB\":2,\"wheels\":null,\"atomicCounter\":0,\"counter\":0,\"brand\":\"car brand\"}\n"; | |
// static objectMapper | |
Stopwatch stopwatch = Stopwatch.createStarted(); | |
List<CompletableFuture<Car>> futureList = new ArrayList<>(); | |
for (int i =0; i < 9; i++) { | |
CompletableFuture<Car> future = CompletableFuture.supplyAsync(() -> { | |
// JsonUtil.toObject is a method to get static objectMapper and execute 'readvalue' | |
return JsonUtil.toObject(json, new TypeReference<Car>() {}); | |
}); | |
futureList.add(future); | |
} | |
CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join(); | |
System.out.println("static objectMapper " + stopwatch.toString()); | |
// instance objectMapper | |
Stopwatch stopwatch1 = Stopwatch.createStarted(); | |
List<CompletableFuture<Car>> futureList2 = new ArrayList<>(); | |
for (int i =0; i < 9; i++) { | |
CompletableFuture<Car> future = CompletableFuture.supplyAsync(() -> { | |
Car car = null; | |
try { | |
car = new ObjectMapper().readValue(json, new TypeReference<Car>() {}); | |
} catch (JsonProcessingException e) { | |
e.printStackTrace(); | |
} | |
return car; | |
}).exceptionally(e -> {throw new RuntimeException(e.getMessage());}); | |
futureList2.add(future); | |
} | |
CompletableFuture.allOf(futureList2.toArray(new CompletableFuture[]{})).join(); | |
System.out.println("instance objectMapper " + stopwatch1.toString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment