/sax/tests/saxtests/src/android/sax/ExpatPerformanceTest.java

https://github.com/aizuzi/platform_frameworks_base · Java · 125 lines · 83 code · 15 blank · 27 comment · 5 complexity · 4912283e8bfba8cacac2a49671bcc521 MD5 · raw file

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