title: Test code to compare jackson Stream vs Map - Is this working right?
body: <p>I have a server side application that I am profiling using <strong><code>VisualVM</code></strong> that makes use of Streaming API.</p>
  
  <p>However, since there are a lot of factors in that code I also made a toy example to compare streaming vs mapping. 
  I have a feeling that something may be off in that there is a lot of randomness in the results. </p>
  
  <p>Is it the measuring? Would using other types of typers make a difference? Is it that there is something that is multi-threaded I don't know about? </p>
  
  <p>Currently I am writing to <code>NUL</code> file object the windows equivalent of <code>dev/null</code>. I am running this on <code>high</code> priority in case the operating system may affect it. </p>
  
  <p><strong>Toy Example Code:</strong></p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.io.StringWriter;
  import java.util.ArrayList;
  import java.util.Map.Entry;
  import java.util.Scanner;
  import java.util.TreeMap;
  
  import com.fasterxml.jackson.core.JsonEncoding;
  import com.fasterxml.jackson.core.JsonFactory;
  import com.fasterxml.jackson.core.JsonGenerator;
  import com.fasterxml.jackson.databind.ObjectMapper;
  
  
  public class TestStreamingMapping {
  
      public final static int NUM_SIMULATED_CATALOGS = 10000;
      public final static int CATALOG_SIZE = 1000;                //1000 Items in CATALOG, 500 requests per second
      public final static boolean WRITE_TO_FILE = false;          //Write to file, or write to string
      public final static boolean DEBUG_PRINT_100_CHAR = false;   //Print out part of string to see all ok
      public static final String mappingFile = "mapping.txt";     //If writing to file, where?
      public static final String streamingFile = "streaming.txt"; //If streaming to file, where?
      public static final boolean PRINT_INTERMEDIATE_RESULTS = false;
  
      public static TreeMap&lt;Long,Double&gt; iterationPercentages = new TreeMap&lt;Long,Double&gt;();
  
      ObjectMapper  mapper= new ObjectMapper();
      JsonFactory f = new JsonFactory();
      JsonGenerator g;
  
      public static long totalCountStream = 0, totalCountMap = 0;
  
      public static void main(String args[])
      {       
  
          System.out.println("Press enter when profiler is connected...");
          new Scanner(System.in).nextLine(); 
          System.out.println("Starting iterations of JSON generation.");
          double percentage;
  
          for(long i=0; i&lt;NUM_SIMULATED_CATALOGS; i++)
          {
              performTest();  
              percentage = (totalCountStream*100.0d / totalCountMap);
              iterationPercentages.put(i, percentage);
  
              if(!PRINT_INTERMEDIATE_RESULTS &amp;&amp; i%100 == 0)System.out.print(i+"-");
          }
  
          System.out.println("Total Streaming API:        " + totalCountStream + "    ns.");
          System.out.println("Total Mapping API:      " + totalCountMap + "   ns.");
          System.out.println("Total Stream(as % of map):  " +  totalCountStream*100.0d / totalCountMap  + "   %\r\n" );
  
          System.out.println("Iteration\tStreamPercent");
          for(Entry&lt;Long, Double&gt; entry : iterationPercentages.entrySet())
              if(entry.getKey() % 20 ==0)
                  System.out.println(entry.getKey() + "\t\t" + Math.round(entry.getValue())  + "%" );
  
      }
  
      public static void performTest()
      {
          TestStreamingMapping test = new TestStreamingMapping(); 
          long time1, time2;
          double percentage = 0;
  
          try {       
              long starttime1 = System.nanoTime();
              test.streamingToFile();
              totalCountStream+=time1=System.nanoTime() - starttime1;
  
              long starttime2 = System.nanoTime();
              test.objectMapping();
              totalCountMap+=time2=System.nanoTime() - starttime2;
  
              percentage = (time1*100.0d / time2);
  
              if(PRINT_INTERMEDIATE_RESULTS)
              {
                  System.out.println("Streaming API:      " + time1 + "   ns.");
                  System.out.println("Mapping API:        " + time2 + "   ns.");
                  System.out.println("Stream(as % of map):    " + percentage  + " %" );
                  System.out.println("----------------------------------------------\r\n");       
  
              }           
  
          } catch (IOException e) {
              e.printStackTrace();
          }               
      }
  
      public String[] numbers;
      public ArrayList&lt;String&gt; arrayList = new ArrayList&lt;String&gt;();
  
      public TestStreamingMapping()
      {
          numbers=new String[62];
          for(int i=0; i&lt;60; i++) numbers[i] = String.valueOf(Math.random()*i);
  
          for(int i=0; i&lt;60; i++) arrayList.add(String.valueOf(Math.random()*i));
      }
  
  
      public void initializeGenerator(StringWriter writer) throws IOException
      {
          if(WRITE_TO_FILE)
              g = f.createGenerator(new File(mappingFile), JsonEncoding.UTF8);
          else
              g = f. createGenerator(writer);
      }
  
  
      public void objectMapping() throws IOException
      {       
          StringWriter writer = new StringWriter();
          initializeGenerator(writer);
  
          for(int j=0; j&lt;CATALOG_SIZE; j++)
              mapper.writeValue(g, this); 
  
          g.close();
          writer.close();     
  
          if(DEBUG_PRINT_100_CHAR)
          System.out.println(writer.toString().substring(0,100));
      }
  
  
  
  
      public void streamingToFile() throws IOException
      {
  
          StringWriter writer = new StringWriter();
          initializeGenerator(writer);
  
          for(int j=0; j&lt;CATALOG_SIZE; j++)
          {
               g.writeStartObject();  
               g.writeFieldName("numbers_streaming");  
               g.writeStartArray(); 
               for(int i=0; i&lt;numbers.length; i++) g.writeString(numbers[i]); 
               g.writeEndArray();
  
               g.writeFieldName("arrayList");  g.writeStartArray(); 
               for(String num: arrayList) g.writeString(num); 
               g.writeEndArray();
  
               g.writeEndObject(); 
          }
  
          g.close();
          writer.close();     
  
          if(DEBUG_PRINT_100_CHAR)
          System.out.println(writer.toString().substring(0,100));
  
      }
  
  }
  </code></pre>
  
  <p>The below code is simulating a service that would generate a JSON catalog document with 1000 Prouct Objects. The hotspot obviously is the serialization of the products (<code>streamToFile()</code> vs <code>objectMapping()</code>).</p>