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

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

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

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}