blob: 1029616d3dced6f9ece575e7ce2c2b1ae69a0179 [file] [log] [blame]
Alan Viverette3da604b2020-06-10 18:34:39 +00001/*
2 * Copyright (C) 2010 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
17package benchmarks;
18
19import com.google.caliper.BeforeExperiment;
20import com.google.caliper.Param;
21import java.io.ByteArrayInputStream;
22import java.io.ByteArrayOutputStream;
23import java.io.FileInputStream;
24import java.io.IOException;
25import java.lang.reflect.Constructor;
26import java.util.Arrays;
27import java.util.List;
28import javax.xml.parsers.DocumentBuilder;
29import javax.xml.parsers.DocumentBuilderFactory;
30import javax.xml.parsers.SAXParser;
31import javax.xml.parsers.SAXParserFactory;
32import org.w3c.dom.Document;
33import org.w3c.dom.Node;
34import org.xml.sax.Attributes;
35import org.xml.sax.SAXException;
36import org.xml.sax.helpers.DefaultHandler;
37import org.xmlpull.v1.XmlPullParser;
38
39public class XmlParseBenchmark {
40
41 @Param String xmlFile;
42 ByteArrayInputStream inputStream;
43
44 static List<String> xmlFileValues = Arrays.asList(
45 "/etc/apns-conf.xml",
46 "/etc/media_profiles.xml",
47 "/etc/permissions/features.xml"
48 );
49
50 private SAXParser saxParser;
51 private DocumentBuilder documentBuilder;
52 private Constructor<? extends XmlPullParser> kxmlConstructor;
53 private Constructor<? extends XmlPullParser> expatConstructor;
54
55 @SuppressWarnings("unchecked")
56 @BeforeExperiment
57 protected void setUp() throws Exception {
58 byte[] xmlBytes = getXmlBytes();
59 inputStream = new ByteArrayInputStream(xmlBytes);
60 inputStream.mark(xmlBytes.length);
61
62 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
63 saxParser = saxParserFactory.newSAXParser();
64
65 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
66 documentBuilder = builderFactory.newDocumentBuilder();
67
68 kxmlConstructor = (Constructor) Class.forName("com.android.org.kxml2.io.KXmlParser")
69 .getConstructor();
70 expatConstructor = (Constructor) Class.forName("org.apache.harmony.xml.ExpatPullParser")
71 .getConstructor();
72 }
73
74 private byte[] getXmlBytes() throws IOException {
75 FileInputStream fileIn = new FileInputStream(xmlFile);
76 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
77 int count;
78 byte[] buffer = new byte[1024];
79 while ((count = fileIn.read(buffer)) != -1) {
80 bytesOut.write(buffer, 0, count);
81 }
82 fileIn.close();
83 return bytesOut.toByteArray();
84 }
85
86 public int timeSax(int reps) throws IOException, SAXException {
87 int elementCount = 0;
88 for (int i = 0; i < reps; i++) {
89 inputStream.reset();
90 ElementCounterSaxHandler elementCounterSaxHandler = new ElementCounterSaxHandler();
91 saxParser.parse(inputStream, elementCounterSaxHandler);
92 elementCount += elementCounterSaxHandler.elementCount;
93 }
94 return elementCount;
95 }
96
97 private static class ElementCounterSaxHandler extends DefaultHandler {
98 int elementCount = 0;
99 @Override public void startElement(String uri, String localName,
100 String qName, Attributes attributes) {
101 elementCount++;
102 }
103 }
104
105 public int timeDom(int reps) throws IOException, SAXException {
106 int elementCount = 0;
107 for (int i = 0; i < reps; i++) {
108 inputStream.reset();
109 Document document = documentBuilder.parse(inputStream);
110 elementCount += countDomElements(document.getDocumentElement());
111 }
112 return elementCount;
113 }
114
115 private int countDomElements(Node node) {
116 int result = 0;
117 for (; node != null; node = node.getNextSibling()) {
118 if (node.getNodeType() == Node.ELEMENT_NODE) {
119 result++;
120 }
121 result += countDomElements(node.getFirstChild());
122 }
123 return result;
124 }
125
126 public int timeExpat(int reps) throws Exception {
127 return testXmlPull(expatConstructor, reps);
128 }
129
130 public int timeKxml(int reps) throws Exception {
131 return testXmlPull(kxmlConstructor, reps);
132 }
133
134 private int testXmlPull(Constructor<? extends XmlPullParser> constructor, int reps)
135 throws Exception {
136 int elementCount = 0;
137 for (int i = 0; i < reps; i++) {
138 inputStream.reset();
139 XmlPullParser xmlPullParser = constructor.newInstance();
140 xmlPullParser.setInput(inputStream, "UTF-8");
141 int type;
142 while ((type = xmlPullParser.next()) != XmlPullParser.END_DOCUMENT) {
143 if (type == XmlPullParser.START_TAG) {
144 elementCount++;
145 }
146 }
147 }
148 return elementCount;
149 }
150}