[Switch] Jackson, JSON-lib, Gson performance comparison

Jackson, JSON-lib, Gson performance comparison

Recently, I did some performance optimization work. When I selected the JSON class library, I found that in addition to the commonly used JSON-lib, there is also a JSON processor, Jackson, which is said to have the fastest performance. So I used the JMeter I just learned. The class library conducts 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 with a total of 6GHz, memory 4GB, WinXP

2. JSON-lib uses 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. Do not start any unrelated processes during the test. After each test is completed, close JMeter to sort out the memory, and then proceed to the next test. Each test is run 3 times and the average value is taken.

4. Converting JSON to Java  Bean means converting the JSON format into a Java class. This class includes attributes of types such as Map, List, Date, Integer/Long/Double, and String. The same is true for Java Bean to Json. In addition, the two are converted to each other, and the data of each conversion is randomly generated

 

Test Results:

 

* The larger the value of throughput, the better, and the smaller the value of total time-consuming, the better

 

JSON to Bean, 5 concurrent threads, about 200 bytes of objects, 10 million conversions:

 

  Jackson JSON-lib Gson
TPS 64113.7 8067.4 13952.8
Total time (seconds) 155 1238 700

 

 

Bean to JSON, 5 concurrent threads, about 200 bytes of objects, 10 million conversions:

 

  Jackson JSON-lib Gson
TPS 54802 15093.2 17308.2
Total time (seconds) 181 661 560

 

 

JSON to Bean, 5 concurrent threads, about 2K objects, 10 million conversions:

 

  Jackson JSON-lib Gson
TPS 37314 2406.9 3657.50
Total time (seconds) 267 4120 2720

 

 

Bean to JSON, 5 concurrent threads, about 2K objects, 10 million conversions:

 

  Jackson JSON-lib Gson
TPS 30922.2 4274.8 4977.00
Total time (seconds) 322 2320 2000

 

 

Test summary:

 

1. Obviously, no matter what form of conversion, Jackson > Gson > Json-lib.

     The processing power of Jackson is even about 10 times higher than that of Json-lib

2. JSON-lib seems to have stopped updating, the latest version is also based on JDK15, and the Jackson community is more active;

3. While testing the performance, the correctness of the conversion of these three class libraries was checked in a human way  , and all three were 100% correct   ;

4. JSON-lib is more cumbersome when converting types such as Date. For example, the following are the conversion results of the two:

JSON-lib:

{"brithday":{"date":17,"day":2,"hours":9,"minutes":24,"month":7,"seconds":26,"time":1282008266398,"timezoneOffset":-480,"year":110}}

Jackson:

{"brithday":1282008123101}

5. JSON-lib relies on 5 commons series packages and ezmorph packages , while Jackson only depends on commons-logging except its own.
6. Jackson provides complete node-based Tree Model and complete OJM data binding function.

 

Jackson usage example:

 

JacksonMapper:

Created as a hungry Chinese-style singleton mode  , the core class ObjectMapper used by Jackson for conversion does not need to create a new object every time. There is a sentence on the official website: can reuse, share globally

Java code
  1. /**  
  2.  * @author xuanyin  
  3.  *   
  4.  */  
  5. publicclass JacksonMapper {    
  6.   
  7.     /**  
  8.      *   
  9.      */  
  10.     privatestaticfinal ObjectMapper mapper = new ObjectMapper();     
  11.   
  12.     /**  
  13.      *   
  14.      */  
  15.     private JacksonMapper() {   
  16.   
  17.     }   
  18.   
  19.     /**  
  20.      *   
  21.      * @return  
  22.      */  
  23.     publicstatic ObjectMapper getInstance() {    
  24.   
  25.         return mapper;   
  26.     }   
  27.   
  28. }   

JSON to Bean:

Java code
  1. ......   
  2. String json = "...";   
  3. ObjectMapper mapper = JacksonMapper.getInstance();   
  4. YourBean bean = mapper.readValue(json, new YourBean().getClass());   
  5. ......  

 

Bean to JSON:

Java code
  1. ......   
  2. YourBean bean = new YourBean();   
  3. ......   
  4. ObjectMapper mapper = JacksonMapper.getInstance();   
  5. StringWriter sw = new StringWriter();   
  6. JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);   
  7. mapper.writeValue(gen, bean);   
  8. gen.close();   
  9. String json = sw.toString();   
  10. ......  

 

* Of course YourBean in the above two pieces of code can also be the basic type of Java

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326926989&siteId=291194637