1 /*
   2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.javax.xml;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.Level;
  27 import org.openjdk.jmh.annotations.Setup;
  28 import org.openjdk.jmh.annotations.TearDown;
  29 
  30 import javax.xml.stream.XMLInputFactory;
  31 import javax.xml.stream.XMLStreamConstants;
  32 import javax.xml.stream.XMLStreamReader;
  33 import java.io.ByteArrayInputStream;
  34 import java.util.HashMap;
  35 import java.util.LinkedList;
  36 import java.util.Map;
  37 
  38 public class STAX extends AbstractXMLMicro {
  39 
  40     /** Live data */
  41     public Map<String, LinkedList<String>> liveData;
  42 
  43     @Setup(Level.Iteration)
  44     public void setupLiveData() {
  45         Map<String, LinkedList<String>> map = new HashMap<>();
  46         // Somewhere around 100 MB live, but fragmented to start with.
  47         for (int i = 0; i < 1000; i++) {
  48             LinkedList<String> list = new LinkedList<>();
  49             String key = "Dummy linked list " + i;
  50             list.add(key);
  51             for (int j = 0; j < 1000; j++) {
  52                 list.add("Dummy string " + i + "." + j);
  53             }
  54             map.put(key, list);
  55         }
  56         // thread safe, will only be one
  57         liveData = map;
  58     }
  59 
  60     @TearDown(Level.Iteration)
  61     public void teardownLiveData() {
  62         liveData = null;
  63     }
  64 
  65     @Benchmark
  66     public int testParse() throws Exception {
  67         int intDummy = 0;
  68         byte[] bytes = getFileBytesFromResource(doc);
  69         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  70         XMLInputFactory factory = XMLInputFactory.newInstance();
  71 
  72         XMLStreamReader parser = factory.createXMLStreamReader(bais);
  73         int acc;
  74         do {
  75             acc = parser.next();
  76             intDummy += acc;
  77         } while (acc != XMLStreamConstants.END_DOCUMENT);
  78         return intDummy;
  79     }
  80 
  81 }