Recently done some performance optimization work, in the selection of JSON class library, found in addition to the commonly used json-lib, there is also known as the fastest performance of the JSON processor Jackson, and then used the newly learned jmeter, the two class library for a simple performance comparison.
jackson:http://jackson.codehaus.org/
json-lib:http://json-lib.sourceforge.net/
gson:http://code.google.com/p/google-gson/
test Environment:
1, working computer: Intel Dual Core E8400 Total 6GHz, memory 4gb,winxp
2, Json-lib with the latest Jdk15,gson version is the latest V1.4,jackson is also the latest v1.5.5,jdk-v1.6.0_20,jmeter-v2.4
3, the test does not open any unrelated processes, each completed a test after the shutdown JMeter finishing memory, then the next test, each test run 3 times, take the average
4. The JSON-to-Java Bean translates the JSON format into a Java class, which includes a map, List, Date, Integer/long/double, String, and so on, and the same is true of the Java Bean's turn to JSON. In addition, the two turn each other, each conversion of the data is randomly generated
Test Results:
* The larger the throughput value, the better, the smaller the total time consuming value.
JSON bean,5 A thread concurrency, about 200 byte objects, 10 million conversions:
|
Jackson |
Json-lib |
Gson |
Tps |
64113.7 |
8067.4 |
13952.8 |
Total time consuming (seconds) |
155 |
1238 |
700 |
The bean json,5 A thread concurrency, about 200 byte objects, 10 million conversions:
|
Jackson |
Json-lib |
Gson |
Tps |
54802 |
15093.2 |
17308.2 |
Total time consuming (seconds) |
181 |
661 |
560 |
JSON bean,5 A thread concurrency, about 2K objects, 10 million conversions:
|
Jackson |
Json-lib |
Gson |
Tps |
37314 |
2406.9 |
3657.50 |
Total time consuming (seconds) |
267 |
4120 |
2720 |
The bean json,5 A thread concurrency, about 2K objects, 10 million conversions:
|
Jackson |
Json-lib |
Gson |
Tps |
30922.2 |
4274.8 |
4977.00 |
Total time consuming (seconds) |
322 |
2320 |
2000 |
Test Summary:
1, Obviously, whatever the form of conversion, Jackson > Gson > Json-lib.
Jackson's processing capacity is even 10 times times higher than the json-lib.
2, Json-lib seems to have stopped updating, the latest version is also based on JDK15, and Jackson's community is more active;
3, in the test performance at the same time, and human flesh way to the three class library conversion correctness of the check, the three are up to 100% correct ;
4. Json-lib is more cumbersome when converting such as date types, as shown in the following conversion results:
Json-lib:
{"Brithday": {"date": "Day": 2, "hours": 9, "minutes": "Month": 7, "seconds": num, "Time": 1282008266398, " Timezoneoffset ": -480," Year ": 110}}
Jackson:
{"Brithday": 1282008123101}
5, Json-lib rely on the Commons series of packages and Ezmorph pack a total of 5, and Jackson in addition to their own only rely on commons-logging
6, Jackson provides a complete node-based tree Model, as well as complete OJM data binding function.
Jackson Use Example:
Jacksonmapper:
Created as a a hungry man mode , Jackson used to convert the core class Objectmapper do not need to be a new object each time, the official online sentence: Can reuse, share globally
Java code /** * @author xuanyin * */ public class jacksonmapper { /** * */ private static final objectmapper mapper = new objectmapper (); /** * */ private jacksonmapper () { } /** * * @return */ public static objectmapper getinstance () { return mapper; } }
json turn Bean:
Java code ... String json = "..."; Objectmapper mapper = Jacksonmapper.getinstance (); Yourbean bean = Mapper.readvalue (JSON, New Yourbean (). GetClass ()); ......
The Bean turns to JSON:
Java code ... Yourbean bean = new Yourbean (); ......