Skip to content

Instantly share code, notes, and snippets.

@rjdkolb
Created July 22, 2015 17:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rjdkolb/65bf7b49e97d373958d8 to your computer and use it in GitHub Desktop.
public static AtomicInteger counter = new AtomicInteger(1);
public static final String user1JsonStart = "{\n"
+ " \"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack";
public static final String user1JsonEnd = "\" },\n"
+ " \"gender\" : \"MALE\",\n"
+ " \"verified\" : false,\n"
+ " \"userImage\" : \"Rm9vYmFyIQ==\"\n"
+ "}";
@Benchmark
public User jsonUnmashal() throws Exception {
ObjectMapper mapper = new ObjectMapper();
StringBuilder jsonUser = new StringBuilder();
//build new Json String
jsonUser.append(user1JsonStart).append(counter.incrementAndGet()).append(user1JsonEnd);
User user = mapper.readValue(jsonUser.toString(), User.class);
return user;
}
@lajospajtek
Copy link

@Setup
public void jsonUser(final StateContainer sc) {
    sc.jsonUser = user1JsonStart + counter.getAndIncrement() + user1JsonEnd;
}

@State(Scope.Benchmark)
public static class StateContainer {
    String jsonUser;
}

@Benchmark
public User jsonUnmashal(final StateContainer sc) throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    final User user = mapper.readValue(sc.jsonUser, User.class);
    return user;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment