JSON0 - Speed Meets Simplicity
JSON and Financial Software System
Recently, software developers needed to make a wise decision on the message format of internal communication between different modules when developing a financial software system. Then, JSON format caught our attention as it has many advantages:
In short, JSON seems to be a perfect match to our needs. Unfortunately, everything has its downside. JSON format is also well-known for its low performance in producing and consuming compared to other formats. So, before using it, we need to run some simple test scenarios to see if they match our requirements. We focus on producing the side as it would be the main task of our servers.
Test setup
For producing JSON objects, we used the JSON-simple library.
The source code is in JSONSimplePerf.java. It’s pretty straightforward.
I use the JSONSimple method to produce JSON objects using the JSON-simple library and convert them to wire format. Note that we change them slightly between the iterator and put them in global memory to prevent compiler optimizations. Parameter size specifies the number of objects that will be produced
In the execute method, we run produceUsingJSONSimple with a size of about 1 million continuously several times (0 means infinite) and print out the elapsed time in between. Note that the first run was with a size of only 1000 to warm up.
The output is in the file JSONSimpleOutput.txt
The source code of the tests and outputs (JSONSimplePerf.java, JSONSimpleOutput.txt) can be found in the link below:
JSONSimple test results
We see a pretty stable number of about 3s for producing 1 million JSON objects, which is not bad at all. This performance should be enough for almost all real-life purposes.
However, in our case, we need to send a lot of messages in this format (about 100K/s), so even producing a speed of 3us/object rate could be problematic, not to mention our messages tend to be bigger and more complex than in this sample.
Points of improvement
A quick look at the sample code gives the following idea of optimization
JSON0 approaches
Recommended by LinkedIn
JSON0 test results
We see a stable number of about 1.35s for producing 1 million JSON objects, which is more than double the performance of the json-simple library.
In a nutshell
While it’s rare nowadays, the library helps developers boost performance through software optimizations, without relying on hardware upgrades. Furthermore, the library is easy to use.
Reference & Installation
Check the JSON0 source code and the usage guidance from the link below:
JSON0 is already deployed on the Maven Central Repositories.
Add the dependency below to your pom.xml:
<dependency>
<groupId>io.github.kwangng</groupId>
<artifactId>JSON0</artifactId>
<version>1.0.8</version>
</dependency>
Example:
JSON0 json = new JSON0();
json.reset();
json.put("Town", "Hoi An Town");
json.put("DayLength", 3);
json.put("Vehicles", "Taxi or Bike");
JSON0 visits = json.createJSON("MustVisits");
visits.put("Morning", "Tra Que Vengetable Village");
visits.put("Afternoon", "The Japanese Bridge");
visits.put("Evening", "An Bang Beach");
JSON0 mustTry = json.createJSONArray("MustTries");
JSON0 foods = mustTry.createJSON("Foods");
foods.put("Food1", "Banh Mi, Banh Xeo");
foods.put("Food2", "Com Ga Hoi An");
JSON0 drinks = mustTry.createJSON("Drinks");
drinks.put("Drink1", "Cafe Sua Da");
drinks.put("Drink2", "Mot Hoi An");
Get the element in JSON:
json.get("MustVisits");
Convert to String:
json.toString()
From the example, we can have some guiding steps. Firstly, creating a JSON parent object:
JSON0 json = new JSON0();
Building key and value:
json.put(String key, Object value);
Getting value:
json.get(String key);
Creating a children JSON object:
json.createJSON(String key);
Creating an array of children JSON objects:
json.createJSONArray(String key);
Reset the JSON object:
json.reset();