1/* 2 * Copyright (C) 2007 The Android Open Source Project 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 android.sax; 18 19import android.test.AndroidTestCase; 20import android.test.suitebuilder.annotation.LargeTest; 21import android.util.Log; 22import android.util.Xml; 23import org.kxml2.io.KXmlParser; 24import org.xml.sax.SAXException; 25import org.xml.sax.helpers.DefaultHandler; 26import org.xmlpull.v1.XmlPullParser; 27import org.xmlpull.v1.XmlPullParserException; 28 29import java.io.ByteArrayInputStream; 30import java.io.ByteArrayOutputStream; 31import java.io.IOException; 32import java.io.InputStream; 33 34import com.android.frameworks.saxtests.R; 35 36public class ExpatPerformanceTest extends AndroidTestCase { 37 38 private static final String TAG = ExpatPerformanceTest.class.getSimpleName(); 39 40 private byte[] mXmlBytes; 41 42 @Override 43 public void setUp() throws Exception { 44 super.setUp(); 45 InputStream in = mContext.getResources().openRawResource(R.raw.youtube); 46 ByteArrayOutputStream out = new ByteArrayOutputStream(); 47 byte[] buffer = new byte[1024]; 48 int length; 49 while ((length = in.read(buffer)) != -1) { 50 out.write(buffer, 0, length); 51 } 52 mXmlBytes = out.toByteArray(); 53 54 Log.i("***", "File size: " + (mXmlBytes.length / 1024) + "k"); 55 } 56 57 @LargeTest 58 public void testPerformance() throws Exception { 59// try { 60// Debug.startMethodTracing("expat3"); 61// for (int i = 0; i < 1; i++) { 62 runJavaPullParser(); 63 runSax(); 64 runExpatPullParser(); 65// } 66// } finally { 67// Debug.stopMethodTracing(); 68// } 69 } 70 71 private InputStream newInputStream() { 72 return new ByteArrayInputStream(mXmlBytes); 73 } 74 75 private void runSax() throws IOException, SAXException { 76 long start = System.currentTimeMillis(); 77 Xml.parse(newInputStream(), Xml.Encoding.UTF_8, new DefaultHandler()); 78 long elapsed = System.currentTimeMillis() - start; 79 Log.i(TAG, "expat SAX: " + elapsed + "ms"); 80 } 81 82 private void runExpatPullParser() throws XmlPullParserException, IOException { 83 long start = System.currentTimeMillis(); 84 XmlPullParser pullParser = Xml.newPullParser(); 85 pullParser.setInput(newInputStream(), "UTF-8"); 86 withPullParser(pullParser); 87 long elapsed = System.currentTimeMillis() - start; 88 Log.i(TAG, "expat pull: " + elapsed + "ms"); 89 } 90 91 private void runJavaPullParser() throws XmlPullParserException, IOException { 92 XmlPullParser pullParser; 93 long start = System.currentTimeMillis(); 94 pullParser = new KXmlParser(); 95 pullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 96 pullParser.setInput(newInputStream(), "UTF-8"); 97 withPullParser(pullParser); 98 long elapsed = System.currentTimeMillis() - start; 99 Log.i(TAG, "java pull parser: " + elapsed + "ms"); 100 } 101 102 private static void withPullParser(XmlPullParser pullParser) 103 throws IOException, XmlPullParserException { 104 int eventType = pullParser.next(); 105 while (eventType != XmlPullParser.END_DOCUMENT) { 106 switch (eventType) { 107 case XmlPullParser.START_TAG: 108 pullParser.getName(); 109// int nattrs = pullParser.getAttributeCount(); 110// for (int i = 0; i < nattrs; ++i) { 111// pullParser.getAttributeName(i); 112// pullParser.getAttributeValue(i); 113// } 114 break; 115 case XmlPullParser.END_TAG: 116 pullParser.getName(); 117 break; 118 case XmlPullParser.TEXT: 119 pullParser.getText(); 120 break; 121 } 122 eventType = pullParser.next(); 123 } 124 } 125} 126