【问题标题】:Which xml serialization library is performance oriented? [closed]哪个 xml 序列化库是面向性能的? [关闭]
【发布时间】:2023-04-07 07:28:01
【问题描述】:

如果性能是决定因素,Java 最好的 XML 序列化库是什么?

应用的重点

  • 基于休息的 API。
  • Tomcat Servlet 容器
  • 需要 Java 对象到 XML 序列化
  • 不需要反序列化或重绑定库。
  • 需要开源库。

当前性能数据

  • 使用“”等的StringBuffer 附加生成的XML。
    • 平均响应时间 = 15 毫秒。
    • 容易出现格式错误的 XML 和 xml 编码错误。
  • 使用 XStream 序列化生成的 XML。
    • 平均响应时间 = 200 毫秒。
    • 易于维护和注释。

我遇到的其他库(例如 JiBx、JaxB、Castor 或 Simple)似乎是绑定框架,并且维护开销似乎很大。

还有其他用于 XML 序列化的高性能替代方案,还是我应该继续使用 XMLStreamWriter API 使用 woodstox Stax 实现来实现 toXml()(这似乎有报告称其在稳定的开源库中是最快的)?

【问题讨论】:

    标签: java xml performance serialization


    【解决方案1】:

    我严重怀疑 XStream 需要 200 毫秒,除非您发送一个非常大的对象。你确定你的虚拟机已经预热了吗?

    我不会使用 StringBuffer 作为它的线程安全,并在每次调用时加锁。请改用 StringBuilder。

    下面的测试打印

    Took 56 us on average to serialise a Person
    

    您要序列化的内容要多花 4000 倍。要么您的测试没有预热,要么您正在发送大量数据。如果是后者,我建议以二进制格式发送数据。


    // 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】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-19
        • 2010-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        相关资源
        最近更新 更多