Send XML over a Socket fast

Overview

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 μs
Compared with sending 1KB with no data.
Threaded Socket Latency for 1/50/99%tile 6.0/8.5/10.7 μs
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

Related articles

How fast are Java sockets

How fast are Java Datagrams?

Comments

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues