Since I have built a light-weight XML/JSON binding framework tailored for Android platform, I want to test its performance on Android when compared with typical xml parsers on Android like SAX, DOM and Xml Pull, I will show you detailed test results in this post.
I did the benchmark on my own mobile phone(it was quite cheap when I bought it:)), below is the spec of my mobile phone:
- Brand : Samsung SCH-i559(Galaxy Mini)
- CPU : ARM6 600MHz
- RAM : 128MB
- OS : Android 2.2.1
The payload I used is a faked person list, you can find the scheam here and all payloads here, I created these payloads with Nano and an auto-fake data generator called podam. I tested 3 kinds of payload size :
- 10 records - 4KB xml, 2KB json
- 50 records - 17KB xml, 9KB json
- 300 records - 100KB xml, 50KB json
Basically, the size of json payload is almost half of the size of xml, this is because json has a more compact messsage format, the 300 records case is used for testing performance when dealing with big payload size.
The benchmark program itself is a typcial Android application, I adaped the test project from this post, I added Nano cases and SAX, DOM, XML pull parsing cases in the benchmark program, you can download the whole benchmark program here.
Below is the UI of the benchmark application on PC emulator,
following choices are avaliable for combined benchmark:
- Thread Number - 1, 3, 5 threads
- Payload Size - 10, 50, 300 records
- Test Type - Nano Xml Read, Nano Json Read, SAX Read, DOM Read, XML Pull Read, Nano Xml Write, Nano Json Write
Test Result
Note:
- the unit of test result is milliseconds
- all test results are average of 20 runs.
- for read test, the time includes file reading time, for write test, serialized content is only written in memory, not real file.
1 Thread Read(Unmarshall) Test
[Records] | [Nano XML] | [Nano JSON] | [RAW SAX] | [RAW DOM] | [RAW Pull] |
10 | 34 | 31 | 18 | 53 | 24 |
50 | 133 | 67 | 70 | 217 | 90 |
300 | 724 | 292 | 388 | 1318 | 497 |
3 Threads Read(Unmarshall) Test
[Records] | [Nano XML] | [Nano JSON] | [RAW SAX] | [RAW DOM] | [RAW Pull] |
10 | 102 | 99 | 53 | 163 | 76 |
50 | 405 | 215 | 206 | 713 | 272 |
300 | 2318 | 951 | 1186 | 3812 | 1599 |
1 Thread Write(Marshall) Test
[Records] | [Nano XML] | [Nano JSON] |
10 | 15 | 23 |
50 | 61 | 87 |
300 | 403 | 508 |
3 Threads Write(Marshall) Test
[Records] | [Nano XML] | [Nano JSON] |
10 | 39 | 60 |
50 | 118 | 276 |
300 | 1290 | 1664 |
Conclusion
For Read Test:
- Of all the tests, the best performers are Nano JSON and RAW SAX, this is not to say that the parsing speed of JSON is identical to SAX, but JSON message format is more compact, and its payload size is almost half of XML, leading to better performance.
- Of all the tests, the worst performer is RAW DOM, this is obvious since DOM needs to put whole doc tree in memory and this will triger much GC on memory limited mobile device, leading to much slower speed.
- The performance of Nano XML is in the middle, internally, Nano XML also use SAX paring technology, but it also needs to do automatic binding by reflection, this causes Nano XML to lose almost half of its performance, however, one the other hand, the maintainability and development effciency of Nano XML are much better than SAX.
For Write Test:
Both Nano XML and Nano JSON perform quite good, in singe thread case they both can serialize 300 records(100KB xml, 50KB json) with less than half second.
Another finding is, there is almost a linear relationship between thread number and performance, when more threads are addded to benchmark, performance will degrade in proportion, considering the computation resource limitation of mobile device, it’s not advisable to do multi-threading xml parsing on mobile device.
My Recommendation:
If your mobile application is performance critical, Nano JSON or RAW SAX is the way to go, I perfer Nano JSON since its automatic binding feature will have better maintainability and can impove development effciency a lot.
In other cases, Nano XML is also a good choice since it’s a good balance among maintainability, development effciency, readability and performance. This is especially the case when you have a complex business domain, in such case, parsing a large amount of domain class will be a big development headache, never to say later maintenance, instead, the automatic code generation and binding features of Nano binding framework will be a big help in such case.