|
Neither Java nor XML Technology need an introduction, nor the synergy between the two: "Portable Code and Portable Data". With the growing interest in web services and e-business platforms, XML is joining Java in the developer's toolbox. As of today, no less than six extensions to the Java Platform empower the Java developer when building XML-based applications:
This article is the second in a series of three. The first one gave an overview of the Java API for XML Processing (JAXP), and the technologies that it directly or indirectly provides to the Java developer or technologies that rely on it in order to process XML documents. It illustrated the use of different APIs with some sample programs. This second article focuses on the relative performance of these APIs as obtained by running the sample programs presented in the first article. This series will conclude with a third article which gives tips on how to improve the performance of XML-based applications from a programmatic and architectural point of view. The purpose of the tests presented in this paper is primarily to highlight the respective performance of different XML processing techniques: SAX, DOM, XSLT, and the impact of validation against a DTD or an XML Schema. The performances of different API implementations: Xerces, Crimson, Xalan, Saxon, XSLTC, and so on when run on different Java runtimes JDK 1.2 and JDK 1.3 (Client and Server) are also compared. The results presented here don't claim to cover all the API implementations available today but underline that the tradeoff between ease of use and performance of a chosen processing models can be biased by the implementation of the underlying parser, document builder or style sheet engine. MethodologyThe sample programs presented in the first article demonstrated the use of the different APIs available to the Java developer. In order to be compared, we applied them to solving the same problem and providing an identical solution. The problem was kept simple to accommodate the different capabilities of those technologies and therefore it may not have revealed the richness and power of the more sophisticated ones like XSLT and XPath. In this respect, the performance tests presented here may be considered as micro-benchmarks. The sample programs are based on different XML processing APIs,
applied to the same set of documents to provide the exact same outputs. The XML documents processed using those different techniques conform to the same Document Type Definition or XML Schema. Those schemas specify the representation of a set of chessboard configurations.
Figure 1: A chessboard configuration In the schemas, the number of chessboard configurations is unbounded, allowing any number of chessboard configurations to be defined in a single document. In order to measure the performance of the different sample programs, documents with 10, 100, 200, 300, 400, 500, 1000, 2000, 3000, 4000 and 5000 chessboard configurations have been created.
XML documents conforming to the Code Sample 1: An XML document conforming to the DTD (Chessboards-[10-5000].xml)
The different implementations based on SAX, DOM, XSLT, XPath, and so on, when applied to the same input documents, provide the same outputs: a simple human-readable text format representation of a chessboard configuration. Code Sample 2: A simple human-readable text format representation of a chessboard configuration
All the sample programs use the Java API for XML Processing (JAXP) to interface with different underlying implementations of the SAX parser, DOM document builder and XSL Transformation engines, and they share a common structure that allows their respective performance to be compared. They all follow the typical steps of using JAXP, and additionally include two loops to process the same document multiple times and in multiple runs. For each run, a factory is used to create a parser, a style sheet processor, and so on, which is in turn used to process an XML source document several times. The validation of the source document against its declared DTDs or XML Schemas may be specified when invoking the program and is implemented by configuring the factory through its
In the code sample ( Code Sample 3: Example of the common structure of the sample programs (ChessboardSAXPrinter.java)
Depending on the performance tested, two different measurements are performed:
The code fragment from a makefile (below) shows that for the target Code Sample 4: Code fragment from a makefile
To select the XML parser, DOM builder or XSLT engine implementation to be tested, the following JAXP properties were passed accordingly to the JVM (
ConfigurationEach sample program is tested in different configurations: with different sizes of processed documents conforming to either a DTD or an XML Schema, with or without validation, with different underlying parser or style sheet engine implementations and with different JVM versions.
Table 1: Configurations tested Performance MeasurementsComparing SAX, DOM and XSLT Performances This test compares the performance of SAX, DOM and XSLT when applied to process the XML documents defined earlier. Two different SAX and DOM parser implementations have been used: Xerces and Crimson.
This test measured the time to process XML documents describing 1000, 2000, 3000, 4000 and 5000 chessboard configurations. Each document has been processed 10 times for each of the 10 runs. The measured time was the sum of the user and system times, as returned by the Measurements
Figure 2: Time to process different sizes of XML documents using SAX, DOM and XSLT with Crimson and Xerces (JDK 1.2) Analysis We can make three observations from these measurements:
![]() Figure 3: Time to process 1000-Chessboard XML documents using SAX, DOM and XSLT with Crimson and Xerces (JDK 1.2) Comparing XSLT and XSLTC PerformancesThis second test compares the performance of regular XSLT engines and the XSLTC engine which compiles the style sheets into Java byte code. XSLTC is now part of Xalan and can be invoked through JAXP. No modification of the sample program was necessary in order to use XSLTC.
This test measured the time to process XML documents describing 1000, 2000, 3000, 4000 and 5000 chessboard configurations (though only the result for a 1000-Chessboard XML document is presented here). Each document has been processed 10 times for each of the 10 runs. The measured time was the sum of the user and system times, as returned by the Measurements ![]() Figure 4: Time to process 1000-Chessboard XML documents using XSLT and XSLTC (JDK 1.2) Analysis This test shows that Xalan and Saxon are performing equally and that compiling style sheets to Java bytecode may improve performances up to 40% when compared to the classic implementations, such as Xalan or Saxon on Crimson. When compared to the previous tests, compiled style sheets may perform better than DOM and close to SAX. Comparing Validation and Non-Validation PerformancesThis benchmark compares the performances of SAX and DOM with and without validation. It's using both Crimson and Xerces' SAX and DOM implementations. The intent here is to measure the impact of validation on the processing time. Per the XML specifications, a non-validating parser is not required to read external entities (including external DTD); therefore external entities referenced in the document may not be expanded and attributes may not have their default value substituted. Thus, the information passed to the invoking application may not be equivalent when using a validating parser and a non-validating parser. In the context of this document, we are only considering parsers which even when non-validating, load and parse the DTD and the entities referenced in the document. This allows, for example, the entities to be substituted, the attribute values to be normalized and their default value properly substituted, so that the application can run unchanged when switching from validation to non-validation of the input document.
This test measured the time to process XML documents describing 1000, 2000, 3000, 4000 and 5000 chessboard configurations (though only the result for a 1000-Chessboard XML document is presented here). Each document has been processed 10 times for each of the 10 runs. The measured time was the sum of the user and system times, as returned by the Measurements ![]() Figure 5: Time to process a 1000-Chessboard XML document using Crimson and Xerces' DOM and SAX implementations with and without validation (JDK 1.2) Analysis
SAX performs better than DOM and the validation of the XML input documents against their DTD incurs an overhead of up to 12% in the case of Xerces' SAX implementation. With the exception of Xerces' SAX implementation, the cost of validation is relatively independent of the API used. The validation is performed internally by the parser. If a DOM builder is implemented on a SAX parser as a custom Comparing Alternative API Performances (JDOM, Xpath, YAXA) This test compares the performances of DOM to JDOM and XPath. The JDOM sample program uses a SAXBuilder to parse and load the XML source document in memory. It walks through the resulting JDOM document and outputs the result in a text format. The XPath sample program uses the DOM API to parse and load an XML source document in memory. It then evaluates XPath expressions to locate the elements to be processed and outputs the result in text format.
Additionally, this test compares YAXA to SAX and DOM. YAXA is an experimental API which works on top of SAX and adds the classical Java Event/Source/Listener paradigm. The YAXA sample program uses a custom SAX2
This test measured the time to process XML documents describing 1000, 2000, 3000, 4000 and 5000 chessboard configurations (though only the result for a 1000-Chessboard XML document is presented here). Each document has been processed 10 times for each of the 10 runs. The measured time was the sum of the user and system times, as returned by the
Measurements ![]() Figure 6: Time to process a 1000-Chessboard XML document with DOM, JDOM and XPath, using Crimson and Xerces with validation (JDK 1.2) ![]() Figure 7: Time to process a 1000-Chessboard XML document with SAX, DOM, YAXA and YAXA-SE (event stream editor), using Crimson with validation (JDK 1.2) Analysis JDOM is slightly faster (around 10%) than DOM using the same underlying parser implementation. XPath on the other hand performs quasi-identically, independent of the underlying parser implementation. This is due to the fact that most of the time goes into locating the relevant elements to be processed with the XPath expressions. Since we are using the same XPath implementation (from Xalan), the overall processing time is the same. YAXA barely performs better than DOM. Because the elements to be processed constitute 90% of the overall document, it ends up creating a large number of event objects in much the same way as DOM creates node objects. It also keeps track of the current context with a stack which ultimately may be equivalent to building the document object model tree. On the other hand, with a simpler but still comparable (in the given example) pattern-matching mechanism, YAXA and YAXA's stream editor perform better than XPath and XSLT for this simple task. Comparing Different JVM VersionsThis test compares the performances of three different Java runtime implementations/configurations, JDK 1.2, JDK 1.3 server, and JDK 1.3 client. They were used to process the XML documents defined earlier with two different SAX and DOM implementations (Xerces and Crimson). This test measured the time to process XML documents describing 1000, 2000, 3000, 4000 and 5000 chessboard configurations (though only the result for a 1000-Chessboard XML document is presented here). Each document has been processed 10 times for each of the 60 runs. The measured time was the average of the elapsed times per document for the last 30 runs. Measurements ![]() Figure 8: Time to process 1000-Chessboard XML documents using SAX and DOM with Crimson and Xerces on JDK 1.2, JDK 1.3 Client, and JDK 1.3 Server. ![]() Figure 9: The JDK 1.3 Server runtime performance improvement over the 60 runs of the DOM sample program with Xerces. A sensitive optimization was performed by the end of the test that could not be shown in the previous chart. Analysis JDK 1.2 and JDK 1.3 Client runtimes perform quite similarly. With the exception of the DOM sample program using Xerces, the JDK 1.3 Server runtime is able to greatly improve the performances over JDK 1.2 from 33 to 38%. For the DOM sample programs using Xerces on the other hand, the performances were down 5%; but as shown on the curve chart, by the end of the run period the JDK 1.3 Server runtime was finally able to optimize it and gain 17% (from 6 to 5). Comparing DOM Access Method PerformanceThe DOM API offers different methods to access an element of an input document:
The first method is more expensive because the sub-tree is totally traversed for each call to the So far, the DOM test was only using the second method. This test measures how well different DOM implementations deal with this problem. Measurements ![]() Figure 10: Time to process different sizes of XML documents using different methods from the DOM API and different implementations (JDK 1.2) Analysis
As expected, the method relying on the Comparing DTD and XML Schema Validation PerformanceThis test compares the performance of validations against DTD and XML Schema. It uses Xerces's SAX implementation (the only one of the two implementations that supports XML Schemas).
This test measured the time to process XML documents describing 1000, 2000, 3000, 4000 and 5000 chessboard configurations (though only the result for a 1000-Chessboard XML document is presented here). Each document has been processed 10 times for each of the 10 runs. The measured time was the sum of the user and system times, as returned by the Measurements ![]() Figure 11: Time to process a 1000-Chessboard XML document using Xerces's SAX implementation with validation against a DTD and an XML Schema (using Xerces 1.4.4). Analysis
With Xerces, validating against an XML Schema is more expensive than validating against an equivalent DTD. When no validation is performed, using XML Schema appears to be more performant than using DTD. This is due to the fact that in that particular implementation of Xerces (1.4.4), the XML Schema referenced in the input document is not even read (as revealed by monitoring the calls to the
When running the benchmark with a more recent version of Xerces (2.0b4), when no validation is performed, using XML Schema is less performant than using DTD, quasi-mirroring the results obtained when validating. In that particular instance, monitoring the calls to the ![]() Figure 12: Time to process a 1000-Chessboard XML document using Xerces's SAX implementation with validation against a DTD and an XML Schema (using Xerces 2.0b4). ConclusionIn this second article, we have tested the different sample programs presented in the first article and analyzed their respective performance when run in different configurations: with different sizes of processed documents conforming to either a DTD or an XML Schema, with or without validation, with different underlying parser or style sheet engine implementations and with different JVM versions. Taking into account the results presented in this document, the next article will attempt to give tips on how to improve the performance of XML-based applications from a programmatic and architectural point of view. Resources
Java Technology & XML - Part 1 -- An Introduction to APIs for XML Processing About the AuthorThierry Violleau is a staff engineer at Sun Microsystems where he works on the J2EE BluePrints program. Previously, he worked in Market Development Engineering - Enabling Technologies group where he helped ISVs integrate Java and XML technologies in their products and solutions.
Have a question about programming? Use Java Online Support. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||