From 2a8fcd23643874478dea0b2c1a4ef4a2d1a2bba1 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Mon, 5 Aug 2019 10:56:31 -0400 Subject: [PATCH] Change test file name to Protobuf Data analysis --- .../demo/serialize/ProtobufDataAnalysis.java | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/test/java/com/insight/demo/serialize/ProtobufDataAnalysis.java diff --git a/src/test/java/com/insight/demo/serialize/ProtobufDataAnalysis.java b/src/test/java/com/insight/demo/serialize/ProtobufDataAnalysis.java new file mode 100644 index 0000000..d5d7cfc --- /dev/null +++ b/src/test/java/com/insight/demo/serialize/ProtobufDataAnalysis.java @@ -0,0 +1,126 @@ +package com.insight.demo.serialize; + +import com.google.protobuf.ByteString; +import com.insight.demo.serialize.mode.Tot.TotMessage; +import com.insight.demo.serialize.mode.TotList.TotListMessage; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.RandomUtils; +import org.joda.time.DateTime; +import org.junit.Test; +import org.xerial.snappy.Snappy; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.File; +import java.io.IOException; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class ProtobufDataAnalysis { + + @Test + public void testTotCache() { + + long sTime = DateTime.now().getMillis(); + + TotMessage.Builder builder = TotMessage.newBuilder(); + TotListMessage.Builder builderList = TotListMessage.newBuilder(); + + + for (int i = 0; i < 30000; i++) { + // simple fields + builder.setPortfolioId(RandomUtils.nextDouble(12, 2000)) + .setTradableEntityId(RandomUtils.nextDouble(12, 2000)) + .setQuantity(RandomUtils.nextDouble(0, 2000000)) + .setTestPrice((RandomUtils.nextDouble(0, 2000000))) + .setTotalMarketValueUSDAmount(builder.getQuantity() * builder.getTestPrice()) + .setRuleApplicationTimeCode("BOD") + .setTradeSide("BUY") + .setPositionCode("LONG") + .setHoldingTransactionId(RandomUtils.nextDouble(12, 2000)) + .setLastExecutionUSDPrice(RandomUtils.nextDouble(0, 2000000)) + .setPortfolioTreatmentCode("TEST-DATA") + .setPortfolioRelationId(RandomUtils.nextDouble(12, 2000)) + .setEffectiveTmstmp(LocalDateTime.now().toString()) + .setTestType("UPDATE") + .setHoldingsId(RandomUtils.nextLong(10000, 1000000)) + .setRequestId(UUID.randomUUID().toString()) + .setTradeEventCode("AMEND") + .setTradePrice(RandomUtils.nextDouble(0, 2000000)) + .setOriginTradeId(UUID.randomUUID().toString()) + .setCurrencyCode("USD") + .setHoldingSourceCode(RandomStringUtils.randomAlphanumeric(6)) + .setHoldingViewCode(RandomStringUtils.randomAlphanumeric(12)) + .setOriginalContinuousHoldingId(RandomUtils.nextLong(10000, 1000000)) + .setPortfolioFactorVersionNumber(RandomUtils.nextLong(10000, 1000000)) + .setPortfolioFactorMethodCode(RandomStringUtils.randomAlphanumeric(12)) + .setTotalAmortizedBookCostUSDAmount(RandomUtils.nextDouble(0, 2000000)) + .setHoldingEffectiveTmstmp(LocalDateTime.now().toString()) + .setLatestPrice(false); + + + // Build Message + TotMessage totMessage = builder.build(); + + // repeated field + builderList.addTotList(totMessage.toByteString()); + } + + TotListMessage lotListMessage = builderList.build(); + +// System.out.println(builderList.toString()); + + + // write the protocol buffers binary to a file + try { + + byte[] compressed = Snappy.compress(lotListMessage.toByteArray()); + + FileUtils.writeByteArrayToFile(new File("C:\\Users\\yhu\\Documents\\TestData\\tot-msg\\lotListMessage.bin"), lotListMessage.toByteArray()); + + FileUtils.writeByteArrayToFile(new File("C:\\Users\\yhu\\Documents\\TestData\\tot-msg\\lotListMessage.bin.snappy"), compressed); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + long eTime = DateTime.now().getMillis(); + + System.out.println(">>S>>"+ sTime); + System.out.println(">>E>>"+ eTime); + System.out.println(">>DIFF >>"+ (eTime - sTime )); + + // send as byte array +// byte[] bytes = message.toByteArray(); + + try { +// System.out.println("Reading from file... "); + FileInputStream fileInputStream = new FileInputStream("C:\\Users\\yhu\\Documents\\TestData\\tot-msg\\lotListMessage.bin"); + TotListMessage messageFromFile = TotListMessage.parseFrom(fileInputStream); + + List messageList = new ArrayList(); + messageList = messageFromFile.getTotListList(); + +// System.out.println("Message Count >>" + messageList.size()); + + for (ByteString byteString : messageList) { + TotMessage totMessageFromFile = TotMessage.parseFrom(byteString); +// System.out.println("setPortfolioId >>" + totMessageFromFile.getPortfolioId()); + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); +// } + + } + + } +}