Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Send XML over a Socket fast

DZone's Guide to

Send XML over a Socket fast

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

XML parsers are designed to handle any type of XML message you can send. However if you assume the XML will always follow a specific format how fast can you make it.
This follows my article on How fast are Java sockets

The format

For this article I have written a self contained test which sends a heartbeat request and which expect a heartbeat response.
<Root timestamp="{timestamp}"><HeartbeatRequest sequence="{sequence}"/></Root>
<Root timestamp="{timestamp}"><HeartbeatResponse sequence="{sequence}"/></Root>
In this test, the sequence number contains the System.nanoTime(). When the client gets back the heartbeat response it can calculate the round trip time.

Messages are parsed and a listener interface called.
    static interface EventListener {
        void heartbeatRequest(long timestamp, long sequenceNumber);

        void heartbeatResponse(long timestamp, long sequenceNumber);

        void error(String message);
    }

Latency

Two messages are sent in quick succession and any data returned is processed. From this the latency of each message can be determined by comparing the System.nanoTime() with the sequence number.

Sending simple XML messages
Socket latency was 1/50/99%tile 11.3/15.6/63.9 us
Compared with sending 1KB with no data.
Threaded Socket Latency for 1/50/99%tile 6.0/8.5/10.7 us
The latency has increased, in particular the high percentile latency. This indicates the variation in times has also increased.

Throughput

Two messages are sent in quick succession and any data returned is processed. The throughput after more than 10 seconds is calculated.

Send simple XML messages.
Socket throughput was 264 K/s
Compared with sending 1KB with no data.
Threaded Socket Throughput was 234 K/s
The throughput is slightly higher. This could be because the message size is much smaller.

The code

It is worth noting that this example creates next to no garbage and the GC doesn't run (checked with -verbosegc) even though the test is running flat out for more than 10 seconds.

XmlServiceTest.java

 

From http://vanillajava.blogspot.com/2011/07/send-xml-over-socket-fast.html

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}