Hi I have just compared 3 serialization strategies - only in terms of payload data and here are the results
entitiy includes - Few Longs, Strings, BigDecimal, Dates , Enums etc
Default Java Serialization
Notes :
1. BigDecimal - when serialized as Strings has 60% less payload when compared to bytes.
2. Date - Calendar is a very heavy to serialize when compared to its counterpart Joda Date time
3. Enums - can be write efficiently serialized. Ref :
@Test public void testJavaSerialization() throws IOException, ClassNotFoundException { Trade trade = new BuyTrade(); trade.setOid(128245); trade.setVersion(10); trade.setOrderId(13212); trade.setProductId(876); trade.setAccountId(1234L); trade.setDateMatched(org.joda.time.DateTime.now()); trade.setPrice(new BigDecimal("213.23")); trade.setVolume(10); trade.setState(TradeState.PENDING); trade.setOpenVolume(10); trade.setType(TradeType.SETTLEMENT); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(bout); oout.writeObject(trade); byte[] bytes = bout.toByteArray(); System.out.println("Java Serialization :" + bytes.length); Trade deSerializedTrade = null; //deserialze ByteArrayInputStream bin = new ByteArrayInputStream(bytes); ObjectInputStream out = new ObjectInputStream(bin); deSerializedTrade = (Trade) out.readObject(); }Default Java Serialization - 1127 bytes Jboss Serialization
@Test public void testJbossSerialization() throws IOException, ClassNotFoundException { Trade trade = new BuyTrade(); trade.setOid(128245); trade.setVersion(10); trade.setOrderId(13212); trade.setProductId(876); trade.setAccountId(1234L); trade.setDateMatched(org.joda.time.DateTime.now()); trade.setPrice(new BigDecimal("213.23")); trade.setVolume(10); trade.setState(TradeState.PENDING); trade.setOpenVolume(10); trade.setType(TradeType.SETTLEMENT); ByteArrayOutputStream bout = new ByteArrayOutputStream(); JBossObjectOutputStream oout = new JBossObjectOutputStream(bout); oout.writeObject(trade); byte[] bytes = bout.toByteArray(); System.out.println("JBoss Serialization :" + bytes.length); Trade deSerializedTrade = null; //deserialze ByteArrayInputStream bin = new ByteArrayInputStream(bytes); JBossObjectInputStream out = new JBossObjectInputStream(bin); deSerializedTrade = (Trade) out.readObject(); }Jboss Serialization – 1125 bytes Externalization
@Test public void testExternalizableSerialization() throws IOException, ClassNotFoundException { ExternalizedTrade trade = new ExternalizedTrade(); trade.setOid(128245); trade.setVersion(10); trade.setWritable(false); trade.setOrderId(13212); trade.setProductId(876); trade.setAccountId(1234L); trade.setDateMatched(org.joda.time.DateTime.now()); trade.setPrice(new BigDecimal("213.23")); trade.setVolume(10); trade.setState(TradeState.PENDING); trade.setOpenVolume(10); trade.setType(TradeType.SETTLEMENT); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(bout); oout.writeObject(trade); byte[] bytes = bout.toByteArray(); System.out.println("Externalizable Serialization :" + bytes.length); //deserialze Trade deSerializedTrade = null; ByteArrayInputStream bin = new ByteArrayInputStream(bytes); ObjectInputStream out = new ObjectInputStream(bin); deSerializedTrade = (ExternalizedTrade) out.readObject(); } @Override public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException { setBias(OrderBias.lookupOrderBiasById((new Long(input.readByte())))); setOid(input.readLong()); setVersion((new Long(input.readInt()))); setWritable(input.readBoolean()); setOrderId(input.readLong()); setProductId((new Long(input.readInt()))); setAccountId(input.readLong()); setDateMatched((org.joda.time.DateTime)input.readObject()); setPrice(new BigDecimal((String)input.readObject())); setVolume((new Long(input.readInt()))); setState(TradeState.lookupTradeStateById((new Long(input.readByte())))); setOpenVolume((new Long(input.readInt()))); setType(TradeType.lookupTradeStateById((new Long(input.readByte())))); } @Override public void writeExternal(ObjectOutput output) throws IOException { output.writeByte(((Long)getBias().getId()).intValue()); output.writeLong(getOid()); output.writeInt(((Long)getVersion()).intValue()); output.writeBoolean(isWritable()); output.writeLong(getOrderId()); output.writeInt(((Long)getProductId()).intValue()); output.writeLong(getAccountId()); output.writeObject(getDateMatched()); output.writeObject(getPrice().toString()); output.writeInt(((Long)getVolume()).intValue()); output.writeByte(((Long)getState().getId()).intValue()); output.writeInt(((Long)getOpenVolume()).intValue()); output.writeByte(((Long)getType().getId()).intValue()); }Externalizable - 999 bytes
Notes :
1. BigDecimal - when serialized as Strings has 60% less payload when compared to bytes.
2. Date - Calendar is a very heavy to serialize when compared to its counterpart Joda Date time
3. Enums - can be write efficiently serialized. Ref :
No comments:
Post a Comment