ads1

четверг, 10 декабря 2015 г.

Compare Java-library for working with JSON: JSON.simple, GSON, Jackson and JSONP

logo

JSON in date - a common standard of data exchange between the server and the web application, and that is often the case with many standards, we can take it as a given, without delving into how it works. Often we do not think about what kind of library use to work with JSON, although, of course, there is a difference between them. With this in mind, we conducted a series of tests to find out how fast each of the four most popular libraries for working with JSON parsing cope with different file size. Now we are ready to share with you the results.
JSON is often used for the transmission and analysis of large files. This scenario is common in applications for data running on Hadoop clusters or Spark. Depending on the file size can be observed a significant difference in the speed of a particular library. Small files may come all the time as queries, their analysis is fast, so the speed difference at first glance, does not play a big role, but the more files you need to handle, the greater the difference. Mikroservisy and distributed applications use JSON to transfer this kind of file, because is the de facto standard for Web API.
Not all JSON-library of the same - a suitable choice for your task can be critical. Benchmarks given in this article will help you choose the correct library.

Library to work with JSON

For our tests, we have chosen four libraries - JSON.simle, GSON, Jackson, JSONP - based on their popularity on Github, they are most often used in projects in Java.
  • JSON.simple from Yidong Fang. Small and lightweight library for encoding and decoding JSON, despite its simplicity, gets the job done at a high level.
  • GSON from Google. The library, which is able to convert Java objects to JSON and vice versa. It does not require a special way to annotate classes, as well as a bonus has full support for Java Generics. No need to add annotations simplifies implementation and can even be the main requirement, if you're going to serialize objects without having the source code for them.
  • Jackson from FasterXML. A set of tools for processing data based on JSON-streaming parser and generator. Designed for Java library is able to not only work with JSON. It has the most popular JSON-parser (based on usage statistics on GitHub).
  • JSONP from Oracle. API for JSON, namely to generate and parse JSON-flow text. The reference implementation of JSR353 open source.

Performance Testing

We conducted performance tests on different-sized files. Requirements (and hence performance) for processing various different sized files, while there are new environment in which we need to process these files.
There were tested two key scenarios - analysis of large (190 MB) and small (1 KB) files. Big JSON-file has been taken  away, the small were generated randomly usinghttp://www.json-generator.com/.
In both cases, the run was performed 10 times for each file. For large files in each run was 10 iterations, for small - 10000. We are not allowed to small files in memory between iterations.All tests were run on c3.large cloud AWS.
The result is large files shown in the table below, for small to save space will only averaged results. Detailed results can be found here. All source code is available here.

Large files

table-1
Here we see a big difference in the results. Depending on the run, Jackson or JSON.simple show the best performance, in the sum of all tests Jackson still sees ahead. On average, in all tests, the best performance with large files show in Jackson, JSON.simple, far behind them there is a JSONP the third and the fourth GSON.
Let's look at the results from a different angle - as a percentage:
table-2
It's like a photo finish - Jackson comes out of the race winner, is breathing down his neck JSON.simple. Two other libraries faintly looming in the rearview mirror.

Small files

table-3
The table above shows the average results for 10 runs for each file and an average of all the files at the bottom of the table. The number of files that each of the libraries worked most quickly:
  • GSON - 14
  • JSONP - 5
  • Jackson - 1
  • JSON.simple - 0
The figures speak for themselves. In any case, if we consider the average results of all tests for small files, the winner goes GSON, JSON.simple JSONP and occupy second and third place respectively, Jackson in last place. Thus, despite the fact that no JSON.simple was fastest in any of the test files, on average, it goes to the second place. Similarly, although all JSONP quickly deal with many files, he ended up in third place.
It is necessary to notice that although Jackson in this case - the slowest library, he shows similar results on each file, while other libraries in most cases show a much higher speed operation, but some files speed of operation is comparable to Jackson, or even slightly lower.
Now look at the results as a percentage:
table-4
GSON wins the race on small files, JSON.simple again shows the second result, a bit behind the winner, JSONP takes a clear third place, and finally, Jackson closes the race.

Conclusion

The speed of parsing - not the only criterion in choosing the library to work with JSON, but one of the most important. Follow the steps in the article performance tests, we found that there is no library that works better than the others on files of all sizes. Libraries, which performed well on large files, failed to small and vice versa.
Thus, the selection of the library on the basis of speed depends on the particular case:
  • if you often work with large JSON-Files, then you are interested in the library will be Jackson. GSON experiencing the greatest difficulties when working with large files.
  • If you have to deal with a large number of short JSON-query (as often happens in services and distributed applications), select GSON. Jackson cope with a large number of small files worst.
  • If you need to work with both large and small file, JSON.simple to help you: the test results, he ranked second for both types of files. Neither Jackson, nor GSON can not with all due speed to work simultaneously with different file size.
When it comes to speed, JSONP is hardly possible to recommend for any occasion - it works slow (compared to other libraries available) with both large and small file. Fortunately, in Java 9 promise to build support for JSON, which should be the improvement of the current reference implementation of JSR353 represented JSONP.
That's all. Once more briefly: if you speed is a JSON-parsing library, choose Jackson for large files, GSON - for small and JSON.simple for simultaneous operation of both types of files.

Комментариев нет:

Отправить комментарий