随着AJAX的火热应用,JSON也越来越被重视,随之而来的就是众多的JSON库,下面对常用的3个JSON库(json-lib,jackson,gson)进行性能测试,看下结果如何:
一. 测试环境
该测试只是在本人电脑上进行测试,每次测试前都重启tomcat,清空缓存,日志等。开的程序也一样,3次测试测试环境相差无几。
json-lib版本:json-lib-2.3-jdk15(最新)
jackson版本 : 1.4.3(最新)
gson : 1.4 (最新)
测试工具:apache带的ab工具
二. 测试代码
1.先定义用于序列化成JSON的Bean,其中字段longTime需要在序列化的过程中被忽略,不输出:
JsonEntity
2. 因json-lib不能直接设置日期的转换方式,需要写1个Processor的类
DateJsonValueProcessor
3. 写1个辅助类JsonTestFactory:
public
class
JsonTestFactory {
static JsonConfig jc;
static Gson gson;
static ObjectMapper mapper;
public static ObjectMapper getMapper(){
if (mapper == null ){
mapper = new ObjectMapper();
SerializationConfig sc = mapper.getSerializationConfig();
sc.setDateFormat( new SimpleDateFormat( " yyyy-MM-dd " ));
mapper.setSerializationConfig(sc);
}
return mapper;
}
public static Gson getGson(){
if (gson == null ){
GsonBuilder gb = new GsonBuilder();
gb.setDateFormat( " yyyy-MM-dd " );
gb.excludeFieldsWithoutExposeAnnotation();
gson = gb.create();
}
return gson;
}
public static JsonConfig getJsonConfig() {
if (jc == null ){
jc = new JsonConfig();
jc.registerJsonValueProcessor(Date. class , new DateJsonValueProcessor( " yyyy-MM-dd " ));
jc.registerPropertyExclusion(JsonEntity. class , " longTime " );
}
return jc;
}
}
static JsonConfig jc;
static Gson gson;
static ObjectMapper mapper;
public static ObjectMapper getMapper(){
if (mapper == null ){
mapper = new ObjectMapper();
SerializationConfig sc = mapper.getSerializationConfig();
sc.setDateFormat( new SimpleDateFormat( " yyyy-MM-dd " ));
mapper.setSerializationConfig(sc);
}
return mapper;
}
public static Gson getGson(){
if (gson == null ){
GsonBuilder gb = new GsonBuilder();
gb.setDateFormat( " yyyy-MM-dd " );
gb.excludeFieldsWithoutExposeAnnotation();
gson = gb.create();
}
return gson;
}
public static JsonConfig getJsonConfig() {
if (jc == null ){
jc = new JsonConfig();
jc.registerJsonValueProcessor(Date. class , new DateJsonValueProcessor( " yyyy-MM-dd " ));
jc.registerPropertyExclusion(JsonEntity. class , " longTime " );
}
return jc;
}
}
4、下面是4个JSP页面的代码,json-lib使用了2种不同的方式进行测试:
jsonlib1.jsp 代码:
<%
JsonEntity je = new JsonEntity( 1 );
out.println(JSONSerializer.toJSON(je,entity.JsonTestFactory.getJsonConfig()).toString());
%>
JsonEntity je = new JsonEntity( 1 );
out.println(JSONSerializer.toJSON(je,entity.JsonTestFactory.getJsonConfig()).toString());
%>
jsonlib2.jsp 代码:
<%
DateFormat dateFormat = new SimpleDateFormat( " yyyy-MM-dd " );
JSONObject json = new JSONObject();
JsonEntity je = new JsonEntity( 1 );
json.element( " id " , je.getId());
json.element( " name " , je.getName());
json.element( " money " , je.getMoney());
json.element( " date " , dateFormat.format(je.getDate()));
out.println(json.toString());
%>
DateFormat dateFormat = new SimpleDateFormat( " yyyy-MM-dd " );
JSONObject json = new JSONObject();
JsonEntity je = new JsonEntity( 1 );
json.element( " id " , je.getId());
json.element( " name " , je.getName());
json.element( " money " , je.getMoney());
json.element( " date " , dateFormat.format(je.getDate()));
out.println(json.toString());
%>
jackson,jsp
<%
JsonEntity je = new JsonEntity( 1 );
out.println(entity.JsonTestFactory.getMapper().writeValueAsString(je));
%>
JsonEntity je = new JsonEntity( 1 );
out.println(entity.JsonTestFactory.getMapper().writeValueAsString(je));
%>
gson.jsp
<%
JsonEntity je = new JsonEntity( 1 );
out.println(entity.JsonTestFactory.getGson().toJson(je));
%>
JsonEntity je = new JsonEntity( 1 );
out.println(entity.JsonTestFactory.getGson().toJson(je));
%>
三、测试结果
测试结果中只需关注Requests per second(平均每秒处理的请求数),Time per request(平均每个请求处理的时间)
1、ab 测试条件: 发送5000个请求,并发数为10。
jsonlib1.jsp | jsonlib2.jsp | jackson.jsp | gson.jsp | |
Concurrency Level | 10 | 10 | 10 | 10 |
Time taken for tests | 3.016 seconds | 3.078 seconds | 2.859 seconds | 3.234 seconds |
Complete requests | 5000 | 5000 | 5000 | 5000 |
Requests per second | 1658.03 [#/sec] | 1624.37 [#/sec] | 1748.63 [#/sec] | 1545.89 [#/sec] |
Time per request | 6.031 [ms] | 6.156 [ms] | 5.719 [ms] | 6.469 [ms] |
1、ab 测试条件: 发送30000个请求,并发数为50。
jsonlib1.jsp | jsonlib2.jsp | jackson.jsp | gson.jsp | |
Concurrency Level | 50 | 50 | 10 | 10 |
Time taken for tests | 11.359 seconds | 12.047 seconds | 10.922 seconds | 13.391 seconds |
Complete requests | 30000 | 30000 | 30000 | 30000 |
Requests per second | 2640.99 [#/sec] | 2490.27 [#/sec] | 2746.78 [#/sec] | 2240.37 [#/sec] |
Time per request | 18.932 [ms] | 20.078 [ms] | 18.203 [ms] | 22.318 [ms] |
google的gson传说性能比较好,不知道为什么这次测试性能这么有问题。jackson的还是非常不错的。