BAEL-3326, "Optimizing JSON Schema for production use":

Added percentages, fixed formatting.
This commit is contained in:
Karsten Silz 2020-09-06 13:48:39 +01:00
parent 9d326b55bc
commit 2c78247c95
4 changed files with 16 additions and 10 deletions

View File

@ -43,7 +43,7 @@ public class CustomerDeserializer extends StdDeserializer<Customer> {
feedback.setPhoneNumber(phoneNumber.isNull() ? null : phoneNumber.asText()); feedback.setPhoneNumber(phoneNumber.isNull() ? null : phoneNumber.asText());
JsonNode email = node.get(8); JsonNode email = node.get(8);
feedback.setEmail(email.isNull() ? null : email.asText()); feedback.setEmail(email.isNull() ? null : email.asText());
return feedback; return feedback;
} }
} }

View File

@ -31,7 +31,7 @@ public class CustomerSlimDeserializer extends StdDeserializer<CustomerSlim> {
.asText()); .asText());
feedback.setAddress(node.get(2) feedback.setAddress(node.get(2)
.asText()); .asText());
return feedback; return feedback;
} }
} }

View File

@ -5,13 +5,13 @@ import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
public class CustomerSlimShortNames { public class CustomerSlimShortNames {
@JsonProperty("i") @JsonProperty("i")
private long id; private long id;
@JsonProperty("n") @JsonProperty("n")
private String name; private String name;
@JsonProperty("a") @JsonProperty("a")
private String address; private String address;

View File

@ -32,10 +32,14 @@ class JsonOptimizationUnitTest {
private static DecimalFormat LENGTH_FORMATTER = new DecimalFormat("###,###,###"); private static DecimalFormat LENGTH_FORMATTER = new DecimalFormat("###,###,###");
private static Customer[] customers; private static Customer[] customers;
private ObjectMapper mapper; private ObjectMapper mapper;
private static int defaultJsonLength;
@BeforeAll @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
customers = Customer.fromMockFile(); customers = Customer.fromMockFile();
ObjectMapper oneTimeMapper = new ObjectMapper();
byte[] feedback = oneTimeMapper.writeValueAsBytes(customers);
defaultJsonLength = feedback.length;
} }
@BeforeEach @BeforeEach
@ -98,16 +102,16 @@ class JsonOptimizationUnitTest {
@Test @Test
void testCustomSerializer() throws IOException { void testCustomSerializer() throws IOException {
printBanner(TEST_LABEL_CUSTOM_SERIALIZER); printBanner(TEST_LABEL_CUSTOM_SERIALIZER);
SimpleModule serializer = new SimpleModule("CustomDeSerializer", new Version(1, 0, 0, null, null, null)); SimpleModule serializer = new SimpleModule("CustomDeSerializer", new Version(1, 0, 0, null, null, null));
serializer.addSerializer(Customer.class, new CustomerSerializer()); serializer.addSerializer(Customer.class, new CustomerSerializer());
serializer.addDeserializer(Customer.class, new CustomerDeserializer()); serializer.addDeserializer(Customer.class, new CustomerDeserializer());
mapper.registerModule(serializer); mapper.registerModule(serializer);
byte[] plainJson = createPlainJson(TEST_LABEL_CUSTOM_SERIALIZER, customers); byte[] plainJson = createPlainJson(TEST_LABEL_CUSTOM_SERIALIZER, customers);
compressJson(TEST_LABEL_CUSTOM_SERIALIZER, plainJson); compressJson(TEST_LABEL_CUSTOM_SERIALIZER, plainJson);
} }
@Test @Test
void testSlimCustomSerializer() throws IOException { void testSlimCustomSerializer() throws IOException {
printBanner(TEST_LABEL_SLIM_CUSTOM_SERIALIZER); printBanner(TEST_LABEL_SLIM_CUSTOM_SERIALIZER);
@ -135,7 +139,8 @@ class JsonOptimizationUnitTest {
gzipStream.write(plainJson); gzipStream.write(plainJson);
gzipStream.close(); gzipStream.close();
byte[] gzippedJson = outpuStream.toByteArray(); byte[] gzippedJson = outpuStream.toByteArray();
System.out.println(label + " GZIPped length: " + LENGTH_FORMATTER.format(gzippedJson.length)); int percent = gzippedJson.length * 100 / defaultJsonLength;
System.out.println(label + " GZIPped length: " + LENGTH_FORMATTER.format(gzippedJson.length / 1024) + "kB (" + percent + "%)");
assertTrue(plainJson.length > gzippedJson.length, label + " should be longer than GZIPped data"); assertTrue(plainJson.length > gzippedJson.length, label + " should be longer than GZIPped data");
} }
@ -145,7 +150,8 @@ class JsonOptimizationUnitTest {
System.out.println(prettyWritter.writeValueAsString(customers[0])); System.out.println(prettyWritter.writeValueAsString(customers[0]));
byte[] feedback = mapper.writeValueAsBytes(customers); byte[] feedback = mapper.writeValueAsBytes(customers);
System.out.println(label + " length: " + LENGTH_FORMATTER.format(feedback.length)); int percent = feedback.length * 100 / defaultJsonLength;
System.out.println(label + " length: " + LENGTH_FORMATTER.format(feedback.length / 1024) + "kB (" + percent + "%)");
assertTrue(feedback.length > 1, label + " should be there"); assertTrue(feedback.length > 1, label + " should be there");
String prefix = label.replaceAll(" ", "-") String prefix = label.replaceAll(" ", "-")