FileDocCategorySizeDatePackage
ExpatPerformanceTest.javaAPI DocAndroid 5.1 API4449Thu Mar 12 22:22:42 GMT 2015android.sax

ExpatPerformanceTest

public class ExpatPerformanceTest extends android.test.AndroidTestCase

Fields Summary
private static final String
TAG
private byte[]
mXmlBytes
Constructors Summary
Methods Summary
private java.io.InputStreamnewInputStream()

        return new ByteArrayInputStream(mXmlBytes);
    
private voidrunExpatPullParser()

        long start = System.currentTimeMillis();
        XmlPullParser pullParser = Xml.newPullParser();
        pullParser.setInput(newInputStream(), "UTF-8");
        withPullParser(pullParser);
        long elapsed = System.currentTimeMillis() - start;
        Log.i(TAG, "expat pull: " + elapsed + "ms");
    
private voidrunJavaPullParser()

        XmlPullParser pullParser;
        long start = System.currentTimeMillis();
        pullParser = new KXmlParser();
        pullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        pullParser.setInput(newInputStream(), "UTF-8");
        withPullParser(pullParser);
        long elapsed = System.currentTimeMillis() - start;
        Log.i(TAG, "java pull parser: " + elapsed + "ms");
    
private voidrunSax()

        long start = System.currentTimeMillis();
        Xml.parse(newInputStream(), Xml.Encoding.UTF_8, new DefaultHandler());
        long elapsed = System.currentTimeMillis() - start;
        Log.i(TAG, "expat SAX: " + elapsed + "ms");
    
public voidsetUp()


    
         
        super.setUp();
        InputStream in = mContext.getResources().openRawResource(R.raw.youtube);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = in.read(buffer)) != -1) {
            out.write(buffer, 0, length);
        }
        mXmlBytes = out.toByteArray();

        Log.i("***", "File size: " + (mXmlBytes.length / 1024) + "k");
    
public voidtestPerformance()

//        try {
//            Debug.startMethodTracing("expat3");
//        for (int i = 0; i < 1; i++) {
            runJavaPullParser();
            runSax();
            runExpatPullParser();
//        }
//    } finally {
//            Debug.stopMethodTracing();
//        }
    
private static voidwithPullParser(org.xmlpull.v1.XmlPullParser pullParser)

        int eventType = pullParser.next();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    pullParser.getName();
//                        int nattrs = pullParser.getAttributeCount();
//                        for (int i = 0; i < nattrs; ++i) {
//                            pullParser.getAttributeName(i);
//                            pullParser.getAttributeValue(i);
//                        }
                    break;
                case XmlPullParser.END_TAG:
                    pullParser.getName();
                    break;
                case XmlPullParser.TEXT:
                    pullParser.getText();
                    break;
            }
            eventType = pullParser.next();
        }