开发者

Which xml serialization library is performance oriented? [closed]

开发者 https://www.devze.com 2023-03-03 19:00 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
开发者_运维问答

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 5 years ago.

Improve this question

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

Salient Points of application

  • Rest Based API.
  • Tomcat Servlet Container
  • Requires Java Object to XML Serialization
  • No requirement for Deserialization or heavy binding libraries.
  • Requires open source libraries.

Current Performance Numbers

  • XML generated using StringBuffer append of "<", ">" and the like.
    • Average Response Time = 15 ms.
    • Prone to malformed XML and xml encoding errors.
  • XML generated using XStream serialization.
    • Average Response Time = 200 ms.
    • Easy to maintain and annotate.

The other libraries which I've come across such as JiBx, JaxB, Castor or Simple seem to be binding frameworks and seem to have a heavy maintenance overhead.

Are there other high performant alternatives for XML serialization or should I just go ahead and implement toXml() using XMLStreamWriter API using woodstox Stax implementation(which seems to have reports of being the fastest among stable open source libraries for the purpose)?


I seriously doubt XStream is taking 200 ms, unless you are sending a very large object. Are you sure your VM is warmed up?

I wouldn't use StringBuffer as its thread safe with a lock on every call. Use StringBuilder instead.

The following test prints

Took 56 us on average to serialise a Person

What ever you are serialising is taking 4000x longer. Either your test is not warmed up or you are sending alot of data. If the later is the case I suggest sending data in a 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);
    }
}


protobuf or apache avro

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号