Skip to content

Instantly share code, notes, and snippets.

@mike-ensor
Created December 29, 2016 19:34
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 mike-ensor/33ed0d2d54d25af10be67276ae6bf896 to your computer and use it in GitHub Desktop.
Save mike-ensor/33ed0d2d54d25af10be67276ae6bf896 to your computer and use it in GitHub Desktop.
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class SerializationBenchmark {
private RecipeService service;
private Recipe recipe;
private byte[] protoRecipe;
private String recipeAsJSON;
@Setup(Level.Trial)
public void setup() {
IngredientUsed jalepenoUsed = new IngredientUsed(new Ingredient("Jalepeno", "Spicy Pepper"), MeasurementType.ITEM, 1);
IngredientUsed cheeseUsed = new IngredientUsed(new Ingredient("Cheese", "Creamy Cheese"), MeasurementType.OUNCE, 4);
recipe = RecipeTestUtil.createRecipe("My Recipe", "Some spicy recipe using a few items", ImmutableList.of(jalepenoUsed, cheeseUsed));
service = new RecipeService(new ObjectMapper());
protoRecipe = service.recipeAsProto(recipe).toByteArray();
recipeAsJSON = service.recipeAsJSON(recipe);
}
@Benchmark
public Messages.Recipe serialize_recipe_object_to_protobuf() {
return service.recipeAsProto(recipe);
}
@Benchmark
public String serialize_recipe_object_to_JSON() {
return service.recipeAsJSON(recipe);
}
@Benchmark
public Recipe deserialize_protobuf_to_recipe_object() {
return service.getRecipe(protoRecipe);
}
@Benchmark
public Recipe deserialize_json_to_recipe_object() {
return service.getRecipe(recipeAsJSON);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment