随着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
1 public class JsonEntity {
2 /**
3 * @Expose 是 GSON中表示该字段是需要被序列化的,没有@Expose表示不需要被序列化
4 */
5 @Expose
6 private int id;
7 @Expose
8 private String name;
9 @Expose
10 private Date date;
11 @Expose
12 private BigDecimal money;
13 /**
14 * 该字段将被忽略
15 * @JsonIgnore 是 jackson中表示该字段是不需要被序列化的
16 */
17 @JsonIgnore
18 private Long longTime;
19
20 public JsonEntity(int i){
21 id = i;
22 name = "sss" + i;
23 date = new Date();
24 money = BigDecimal.valueOf(200);
25 longTime = 2222222l;
26 }
27 public int getId() {
28 return id;
29 }
30 public void setId(int id) {
31 this.id = id;
32 }
33 public String getName() {
34 return name;
35 }
36 public void setName(String name) {
37 this.name = name;
38 }
39 public Date getDate() {
40 return date;
41 }
42 public void setDate(Date date) {
43 this.date = date;
44 }
45 public BigDecimal getMoney() {
46 return money;
47 }
48 public void setMoney(BigDecimal money) {
49 this.money = money;
50 }
51 @JsonIgnore
52 public Long getLongTime() {
53 return longTime;
54 }
55 @JsonIgnore
56 public void setLongTime(Long longTime) {
57 this.longTime = longTime;
58 }
59
2. 因json-lib不能直接设置日期的转换方式,需要写1个Processor的类
DateJsonValueProcessor
1 public class DateJsonValueProcessor implements JsonValueProcessor {
2 private DateFormat dateFormat;
3
4 public DateJsonValueProcessor(String datePattern) {
5 dateFormat = new SimpleDateFormat(datePattern);
6 }
7 @Override
8 public Object processArrayValue(Object o, JsonConfig jc) {
9 return "";
10 }
11 @Override
12 public Object processObjectValue(String string, Object o, JsonConfig jc) {
13 return dateFormat.format(o);
14 }
15
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;
}
}
4、下面是4个JSP页面的代码,json-lib使用了2种不同的方式进行测试:
jsonlib1.jsp 代码:
<%
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());
%>
jackson,jsp
<%
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));
%>
三、测试结果
测试结果中只需关注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的还是非常不错的。