Which xml serialization library is performance oriented?

What is the best XML serialization library for Java if performance is a decisive factor?

Bright points of the application

  • Recreation-based API.
  • Tomcat Servlet Container
  • Requires Java Object to XML Serialization
  • There are no requirements for Deserialization or heavy binding files.
  • Requires open source libraries.

Current Performance Numbers

  • XML generated by adding StringBuffer from "<", ">", etc.
    • Average response time = 15 ms.
    • Predisposed to garbled XML and xml coding errors.
  • XML created using XStream serialization.
    • Average response time = 200 ms.
    • Easy to maintain and comment.

Other libraries I've come across, such as JiBx, JaxB, Castor, or Simple, seem to be required frameworks and seem to have heavy overhead.

Are there other high-performance alternatives for serializing XML, or should I just go ahead and implement toXml () using the XMLStreamWriter API using the woodstox Stax implementation (which seems to indicate that it is the fastest among the stable open source libraries code for this purpose)?

+3
source share
2 answers

I seriously doubt that the XStream takes 200ms unless you send a very large object. Are you sure your virtual machine is hot?

I would not use StringBuffer as a safe thread with a lock on every call. Use StringBuilder instead.

The following test prints

Took 56 us on average to serialise a Person 

Whatever you serialize, takes more than 4,000 times. Either your test is not warming up, or you are sending a lot of data. If in the latter case, I suggest sending the data in binary format.


 // based on the example in the two-minute tutorial. public class XStreamTest { public static class Person { private String firstname; private String lastname; private PhoneNumber phone; private PhoneNumber fax; public Person(String firstname, String lastname, PhoneNumber phone, PhoneNumber fax) { this.firstname = firstname; this.lastname = lastname; this.phone = phone; this.fax = fax; } } public static class PhoneNumber { private int code; private String number; public PhoneNumber(int code, String number) { this.code = code; this.number = number; } } public static void main(String... args) { XStream xstream = new XStream(); xstream.alias("person", Person.class); xstream.alias("phonenumber", PhoneNumber.class); Person joe = new Person("Joe", "Walnes", new PhoneNumber(123, "1234-456"), new PhoneNumber(123, "9999-999")); final int warmup = 10000; final int runs = 20000; long start = 0; for (int i = -warmup; i < runs; i++) { if(i == 0) start = System.nanoTime(); String xml = xstream.toXML(joe); } long time = System.nanoTime() - start; System.out.printf("Took %,d us on average to serialise a Person%n", time / runs / 1000); } } 
+2
source

Source: https://habr.com/ru/post/907117/


All Articles