Peter is a DZone MVB and is not an employee of DZone and has posted 161 posts at DZone. You can read more from them at their website. View Full User Profile

Send XML over a Socket fast

08.01.2011
| 6245 views |
  • submit to reddit

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

Published at DZone with permission of Peter Lawrey, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: