随着AJAX的火热应用,JSON也越来越被重视,随之而来的就是众多的JSON库,下面对常用的3个JSON库(json-lib,jackson,gson)进行性能测试,看下结果如何:
一. 测试环境
二. 测试代码
1.先定义用于序列化成JSON的Bean,其中字段longTime需要在序列化的过程中被忽略,不输出:
2. 因json-lib不能直接设置日期的转换方式,需要写1个Processor的类
3. 写1个辅助类JsonTestFactory:
4、下面是4个JSP页面的代码,json-lib使用了2种不同的方式进行测试:
jsonlib1.jsp 代码:
jsonlib2.jsp 代码:
jackson,jsp
gson.jsp
三、测试结果
测试结果中只需关注Requests per second(平均每秒处理的请求数),Time per request(平均每个请求处理的时间)
1、ab 测试条件: 发送5000个请求,并发数为10。
1、ab 测试条件: 发送30000个请求,并发数为50。
google的gson传说性能比较好,不知道为什么这次测试性能这么有问题。jackson的还是非常不错的。
一. 测试环境
该测试只是在本人电脑上进行测试,每次测试前都重启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
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
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;
}
}
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 代码:
<%</span><span style="background-color: #F5F5F5; color: #000000; "> <br /> JsonEntity je </span><span style="background-color: #F5F5F5; color: #000000; ">=</span><span style="background-color: #F5F5F5; color: #000000; "> </span><span style="background-color: #F5F5F5; color: #0000FF; ">new</span><span style="background-color: #F5F5F5; color: #000000; "> JsonEntity(</span><span style="background-color: #F5F5F5; color: #000000; ">1</span><span style="background-color: #F5F5F5; color: #000000; ">);<br /> <br /> out.println(JSONSerializer.toJSON(je,entity.JsonTestFactory.getJsonConfig()).toString());<br /> </span><span style="background-color: #FFFF00; color: #000000; ">%>
jsonlib2.jsp 代码:
<%</span><span style="background-color: #F5F5F5; color: #000000; "><br /> DateFormat dateFormat </span><span style="background-color: #F5F5F5; color: #000000; ">=</span><span style="background-color: #F5F5F5; color: #000000; "> </span><span style="background-color: #F5F5F5; color: #0000FF; ">new</span><span style="background-color: #F5F5F5; color: #000000; "> SimpleDateFormat(</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">yyyy-MM-dd</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">);<br /> JSONObject json </span><span style="background-color: #F5F5F5; color: #000000; ">=</span><span style="background-color: #F5F5F5; color: #000000; "> </span><span style="background-color: #F5F5F5; color: #0000FF; ">new</span><span style="background-color: #F5F5F5; color: #000000; "> JSONObject();<br /> JsonEntity je </span><span style="background-color: #F5F5F5; color: #000000; ">=</span><span style="background-color: #F5F5F5; color: #000000; "> </span><span style="background-color: #F5F5F5; color: #0000FF; ">new</span><span style="background-color: #F5F5F5; color: #000000; "> JsonEntity(</span><span style="background-color: #F5F5F5; color: #000000; ">1</span><span style="background-color: #F5F5F5; color: #000000; ">);<br /> json.element(</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">id</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">, je.getId());<br /> json.element(</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">name</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">, je.getName());<br /> json.element(</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">money</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">, je.getMoney());<br /> json.element(</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">date</span><span style="background-color: #F5F5F5; color: #000000; ">"</span><span style="background-color: #F5F5F5; color: #000000; ">, dateFormat.format(je.getDate()));<br /> out.println(json.toString());<br /> </span><span style="background-color: #FFFF00; color: #000000; ">%>
jackson,jsp
<%</span><span style="background-color: #F5F5F5; color: #000000; "><br /> JsonEntity je </span><span style="background-color: #F5F5F5; color: #000000; ">=</span><span style="background-color: #F5F5F5; color: #000000; "> </span><span style="background-color: #F5F5F5; color: #0000FF; ">new</span><span style="background-color: #F5F5F5; color: #000000; "> JsonEntity(</span><span style="background-color: #F5F5F5; color: #000000; ">1</span><span style="background-color: #F5F5F5; color: #000000; ">);<br /> out.println(entity.JsonTestFactory.getMapper().writeValueAsString(je));<br /> </span><span style="background-color: #FFFF00; color: #000000; ">%>
gson.jsp
<%</span><span style="background-color: #F5F5F5; color: #000000; "><br /> JsonEntity je </span><span style="background-color: #F5F5F5; color: #000000; ">=</span><span style="background-color: #F5F5F5; color: #000000; "> </span><span style="background-color: #F5F5F5; color: #0000FF; ">new</span><span style="background-color: #F5F5F5; color: #000000; "> JsonEntity(</span><span style="background-color: #F5F5F5; color: #000000; ">1</span><span style="background-color: #F5F5F5; color: #000000; ">);<br /> out.println(entity.JsonTestFactory.getGson().toJson(je));<br /> </span><span style="background-color: #FFFF00; color: #000000; ">%>
三、测试结果
测试结果中只需关注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的还是非常不错的。
[作者]:BearRui(AK-47)
[博客]: http://www.blogjava.net/bearrui/
[声明]:本博所有文章版权归作者所有(除特殊说明以外),转载请注明出处.
[博客]: http://www.blogjava.net/bearrui/
[声明]:本博所有文章版权归作者所有(除特殊说明以外),转载请注明出处.
英雄,别走啊,帮哥评论下:
精彩推荐 好文要顶 水平一般 看不懂 还需努力
精彩推荐 好文要顶 水平一般 看不懂 还需努力