• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package benchmarks.regression;
18 
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Param;
21 import java.io.StringReader;
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import org.xml.sax.InputSource;
25 import org.xmlpull.v1.XmlPullParser;
26 import org.xmlpull.v1.XmlPullParserFactory;
27 
28 // http://code.google.com/p/android/issues/detail?id=18102
29 public final class XmlEntitiesBenchmark {
30 
31   @Param({"10", "100", "1000"}) int length;
32   @Param({"0", "0.5", "1.0"}) float entityFraction;
33 
34   private XmlPullParserFactory xmlPullParserFactory;
35   private DocumentBuilderFactory documentBuilderFactory;
36 
37   /** a string like {@code <doc>&amp;&amp;++</doc>}. */
38   private String xml;
39 
40   @BeforeExperiment
setUp()41   protected void setUp() throws Exception {
42     xmlPullParserFactory = XmlPullParserFactory.newInstance();
43     documentBuilderFactory = DocumentBuilderFactory.newInstance();
44 
45     StringBuilder xmlBuilder = new StringBuilder();
46     xmlBuilder.append("<doc>");
47     for (int i = 0; i < (length * entityFraction); i++) {
48       xmlBuilder.append("&amp;");
49     }
50     while (xmlBuilder.length() < length) {
51       xmlBuilder.append("+");
52     }
53     xmlBuilder.append("</doc>");
54     xml = xmlBuilder.toString();
55   }
56 
timeXmlParser(int reps)57   public void timeXmlParser(int reps) throws Exception {
58     for (int i = 0; i < reps; i++) {
59       XmlPullParser parser = xmlPullParserFactory.newPullParser();
60       parser.setInput(new StringReader(xml));
61       while (parser.next() != XmlPullParser.END_DOCUMENT) {
62       }
63     }
64   }
65 
timeDocumentBuilder(int reps)66   public void timeDocumentBuilder(int reps) throws Exception {
67     for (int i = 0; i < reps; i++) {
68       DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
69       documentBuilder.parse(new InputSource(new StringReader(xml)));
70     }
71   }
72 }
73