BinaryXmlIOComparison.java

  1. package gov.usgs.earthquake.product.io;

  2. import gov.usgs.earthquake.product.Product;
  3. import gov.usgs.util.StreamUtils;

  4. import java.io.ByteArrayOutputStream;
  5. import java.io.File;
  6. import java.util.Date;

  7. /**
  8.  * Compare io times of XML and Binary product formats.
  9.  *
  10.  * All conversion is done in memory to try to balance the tests. All writes use
  11.  * a BinaryProductSource to keep the playing field level.
  12.  */
  13. public class BinaryXmlIOComparison {

  14.     /**
  15.      * Serializes product by streaming it to a handler
  16.      * @param source a productSource
  17.      * @param handler a productHandler
  18.      * @return Time it took to stream source to handler
  19.      * @throws Exception if error occurs
  20.      */
  21.     public static long timeSerializeProduct(final ProductSource source,
  22.             final ProductHandler handler) throws Exception {
  23.         Date start = new Date();
  24.         source.streamTo(handler);
  25.         Date end = new Date();
  26.         return end.getTime() - start.getTime();
  27.     }

  28.     /**
  29.      * Testing for class
  30.      * @param args CLI args
  31.      * @throws Exception if error occurs
  32.      */
  33.     public static void main(final String[] args) throws Exception {
  34.         int numRuns = 10;
  35.         testProductIO(
  36.                 ObjectProductHandler.getProduct(new BinaryProductSource(
  37.                         StreamUtils.getInputStream(new File(
  38.                                 "etc/test_products/se082311a/us_dyfi_se082311a_1314562782198.bin")))),
  39.                 numRuns);
  40.         testProductIO(
  41.                 ObjectProductHandler.getProduct(new BinaryProductSource(
  42.                         StreamUtils.getInputStream(new File(
  43.                                 "etc/test_products/usa00040xz/us_shakemap_usa00040xz_1287260900624.bin")))),
  44.                 numRuns);
  45.         testProductIO(
  46.                 ObjectProductHandler.getProduct(new BinaryProductSource(
  47.                         StreamUtils.getInputStream(new File(
  48.                                 "etc/test_products/usa00040xz/us_losspager_usa00040xz_1287260989064.bin")))),
  49.                 numRuns);
  50.     }

  51.     /**
  52.      * Tests product IO
  53.      * @param product Produc
  54.      * @param numRuns int
  55.      * @throws Exception if error occurs
  56.      */
  57.     public static void testProductIO(final Product product, int numRuns)
  58.             throws Exception {
  59.         System.err.println(product.getId().toString());
  60.         testXmlReads(product, numRuns);
  61.         testXmlWrites(product, numRuns);
  62.         testBinaryReads(product, numRuns);
  63.         testBinaryWrites(product, numRuns);
  64.         System.err.println();
  65.     }

  66.     /**
  67.      * Tests XML reading
  68.      * @param product a product
  69.      * @param numReads int
  70.      * @throws Exception if error occurs
  71.      */
  72.     public static void testXmlReads(final Product product, int numReads)
  73.             throws Exception {
  74.         // read product into memory
  75.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  76.         new ObjectProductSource(product).streamTo(new XmlProductHandler(baos));
  77.         byte[] bytes = baos.toByteArray();

  78.         long time = 0L;

  79.         for (int i = 0; i < numReads; i++) {
  80.             Date start = new Date();

  81.             // parse from memory
  82.             ObjectProductHandler.getProduct(new XmlProductSource(StreamUtils
  83.                     .getInputStream(bytes)));

  84.             Date end = new Date();
  85.             time += end.getTime() - start.getTime();
  86.         }

  87.         System.err.println("xml\tlength=" + bytes.length + ",\tnumReads="
  88.                 + numReads + ",\tread average=" + ((double) time / numReads));
  89.     }

  90.     /**
  91.      * Tests binary reading
  92.      * @param product a product
  93.      * @param numReads int
  94.      * @throws Exception if error occurs
  95.      */
  96.     public static void testBinaryReads(final Product product, int numReads)
  97.             throws Exception {
  98.         // read product into memory
  99.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  100.         new ObjectProductSource(product)
  101.                 .streamTo(new BinaryProductHandler(baos));
  102.         byte[] bytes = baos.toByteArray();

  103.         long time = 0L;

  104.         for (int i = 0; i < numReads; i++) {
  105.             Date start = new Date();

  106.             // parse from memory
  107.             ObjectProductHandler.getProduct(new BinaryProductSource(StreamUtils
  108.                     .getInputStream(bytes)));

  109.             Date end = new Date();
  110.             time += end.getTime() - start.getTime();
  111.         }

  112.         System.err.println("binary\tlength=" + bytes.length + ",\tnumReads="
  113.                 + numReads + ",\tread average=" + ((double) time / numReads));
  114.     }

  115.     /**
  116.      * Tests binary writes
  117.      * @param product a product
  118.      * @param numWrites int
  119.      * @throws Exception if error occurs
  120.      */
  121.     public static void testBinaryWrites(final Product product, int numWrites)
  122.             throws Exception {
  123.         // read product into memory
  124.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  125.         new ObjectProductSource(product)
  126.                 .streamTo(new BinaryProductHandler(baos));
  127.         byte[] bytes = baos.toByteArray();

  128.         long time = 0L;

  129.         for (int i = 0; i < numWrites; i++) {
  130.             baos.reset();
  131.             Date start = new Date();

  132.             // parse from memory
  133.             new BinaryProductSource(StreamUtils.getInputStream(bytes))
  134.                     .streamTo(new BinaryProductHandler(baos));

  135.             Date end = new Date();
  136.             time += end.getTime() - start.getTime();
  137.         }

  138.         System.err.println("binary\tlength=" + baos.toByteArray().length
  139.                 + ",\tnumWrites=" + numWrites + ",\twrite average="
  140.                 + ((double) time / numWrites));
  141.     }

  142.     /**
  143.      * tests xml writes
  144.      * @param product a product
  145.      * @param numWrites int
  146.      * @throws Exception if error occurs
  147.      */
  148.     public static void testXmlWrites(final Product product, int numWrites)
  149.             throws Exception {
  150.         // read product into memory
  151.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  152.         new ObjectProductSource(product)
  153.                 .streamTo(new BinaryProductHandler(baos));
  154.         byte[] bytes = baos.toByteArray();

  155.         long time = 0L;

  156.         for (int i = 0; i < numWrites; i++) {
  157.             baos.reset();
  158.             Date start = new Date();

  159.             // parse from memory
  160.             new BinaryProductSource(StreamUtils.getInputStream(bytes))
  161.                     .streamTo(new XmlProductHandler(baos));

  162.             Date end = new Date();
  163.             time += end.getTime() - start.getTime();
  164.         }

  165.         System.err.println("xml\tlength=" + baos.toByteArray().length
  166.                 + ",\tnumWrites=" + numWrites + ",\twrite average="
  167.                 + ((double) time / numWrites));
  168.     }

  169. }