这篇文章是我2015-02-08写的 Android为啥使用org.json而不用gson,如何更好的测试和对比这两个东西,哪个大神能告诉我小萌头? 那篇文章的续篇.
首先,要PK的这三个都是json解析与生成的类库. 上次那篇文章可能写的太初级或有主观情绪在里面,导致没有人愿意帮我做出更好的指导. 我这次学了一点点专业级性能测试的皮毛,打算做客观的基准测试. 每一种基准测试都必须严格做出限定条件,得出足够窄的某一面的结论. 我这里限定了如下条件:
- 不比较这三个类库的易用性可维护性,尽管在我看来fastjson,gson比org.json好用好维护的多;
- 仅做解析json的速度测试,不做生成json的速度测试,不做期间消耗的内存,cpu相关的测试以及其他不相干的测试,有兴趣的可自行测试;
- 假定json字符串中所有的内容数据都能用的上且仅用一次. 因为org.json可能直接以”流”模式操作字符串,如果不需要用到所有内容数据,仅用两个字段,其速度可能优于另外两个,而如果内容数据需要保存到JavaBean中进行复用,看起来fastjson和gson要占便宜;
- 未考虑大文本json和json里含对象数组的情况;
- 限定在64位Ubuntu14.04和64位Windows7平台,Intel 酷睿4核,8G内存,Oracle Hotspot server VM mixed mode的JDK1.8.0的情况下;
- 这里的org.json指group: ‘org.json’,name: ‘json’,version: ‘20141113’,这里的fastjson指group: ‘com.alibaba’,name: ‘fastjson’,version: ‘1.2.5’,这里的gson指group: ‘com.google.code.gson’,name: ‘gson’,version: ‘2.3.1’;
要达到的几个测试目标有:
- 得出正确的几个类库解析速度数据和对比结论,这是总纲,事实上,还是不能得出十分精确的结论,但尽可能确保趋势是正确的,排除所有的干扰;
- 考虑到垃圾回收的干扰,尽可能减少测试区域中的对象创建以及指定较大的堆,目前使用-Xms1024m -Xmx1024m -Xss2m;
- 考虑到线程切换的干扰和锁的干扰,仅在单线程中运行,并未使用同步;
- 考虑到JIT编译干扰,记录解释执行的数据,加大测试次数(大于1000万),尽可能统计JIT编译后的平均数据. 当然可以指定-server -Xcomp直接测试编译后的速度;
- 考虑到jvm做逃逸跟踪分析时的性能影响,以及分析后动态优化措施的影响,包括锁消除,栈上分配对象,标量替换,去除无用代码和多余赋值,自动装箱拆箱消除等,使用大数量测试次数来平摊这种影响,不睬信javap反汇编代码执行流程,并且在使用-XX:+AggressiveOpts参数和不使用该参数各做若干次执行测试;
测试结果是fastjson > gson > org.json;
fastjson : (前面摘取: 189670,218408),(平均值摘取: 4170,4453);
gson: (前面摘取: 71434,62812),(平均值摘取: 6427,5033);
org.json: (前面摘取: 87035,89498),(平均值摘取: 9641,9652);
以下是测试代码:
(JsonTest.java)
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import org.json.JSONObject;
public class JsonTestAgain {
private static final Gson gson = new Gson();
private static final int iterTimes = 20000000;
private static long avgTime = 0L;
public static void main(String[] args) {
String json = "{\"id\":109394012,\"name\":\"zhangsan\",\"school\":\"renmindaxue\",\"age\":24,\"isFemale\":false,\"area\":\"beijing\",\"city\":\"beijing\",\"address\":\"changping\",\"phone\":\"13100049348\",\"email\":\"xxx@163.com\",\"isMarried\":true,\"beginDate\":\"2009-01-01\",\"beginCompany\":\"pingan\",\"height\":178,\"weight\":200,\"style\":1,\"leaveDate\":\"2010-01-01\",\"mrRight\":{\"id\":1093079412,\"name\":\"meinv\",\"age\":21,\"isFemale\":true,\"phone\":\"13149348000\",\"email\":\"zzz@163.com\",\"beginDate\":\"2007-09-01\",\"beginCompany\":\"nuoya\",\"height\":160,\"weight\":100,\"style\":2,\"leaveDate\":\"2012-11-01\"}}";
JohnTestData data = new JohnTestData();
data.mrRight = new JohnTestData();
for (int i = 0; i < 1000; i++) {
System.out.println(uSEOrgJson(json,data));
}
avgTime = uSEOrgJson(json,data);
for (int i = 0; i < iterTimes; i++) {
avgTime += uSEOrgJson(json,data);
avgTime = avgTime / 2;
}
System.out.println("============================");
System.out.println(avgTime);
}
private static long uSEOrgJson(String json,JohnTestData right) {
long start = System.nanoTime();
JSONObject left = new JSONObject(json);
right.address = left.getString("address");
right.age = left.getInt("age");
right.area = left.getString("area");
right.beginCompany = left.getString("beginCompany");
right.beginDate = left.getString("beginDate");
right.city = left.getString("city");
right.email = left.getString("email");
right.height = left.getInt("height");
right.id = left.getLong("id");
right.isFemale = left.getBoolean("isFemale");
right.isMarried = left.getBoolean("isMarried");
right.leaveDate = left.getString("leaveDate");
right.name = left.getString("name");
right.phone = left.getString("phone");
right.weight = left.getInt("weight");
right.school = left.getString("school");
right.style = left.getInt("style");
JSONObject object = left.getJSONObject("mrRight");
right.mrRight.address = object.getString("address");
right.mrRight.age = object.getInt("age");
right.mrRight.area = object.getString("area");
right.mrRight.beginCompany = object.getString("beginCompany");
right.mrRight.beginDate = object.getString("beginDate");
right.mrRight.city = object.getString("city");
right.mrRight.email = object.getString("email");
right.mrRight.height = object.getInt("height");
right.mrRight.id = object.getLong("id");
right.mrRight.isFemale = object.getBoolean("isFemale");
right.mrRight.isMarried = object.getBoolean("isMarried");
right.mrRight.leaveDate = object.getString("leaveDate");
right.mrRight.name = object.getString("name");
right.mrRight.phone = object.getString("phone");
right.mrRight.weight = object.getInt("weight");
right.mrRight.school = object.getString("school");
right.mrRight.style = object.getInt("style");
right.mrRight.mrRight = null;
return System.nanoTime() - start;
}
private static long useFastjson(String json,JohnTestData right) {
long start = System.nanoTime();
JohnTestData left = JSON.parSEObject(json,JohnTestData.class);
right.address = left.address;
right.age = left.age;
right.area = left.area;
right.beginCompany = left.beginCompany;
right.beginDate = left.beginDate;
right.city = left.city;
right.email = left.email;
right.height = left.height;
right.id = left.id;
right.isFemale = left.isFemale;
right.isMarried = left.isMarried;
right.leaveDate = left.leaveDate;
right.name = left.name;
right.phone = left.phone;
right.weight = left.weight;
right.school = left.school;
right.style = left.style;
right.mrRight = left.mrRight;
return System.nanoTime() - start;
}
private static long useGson(String json,JohnTestData right) {
long start = System.nanoTime();
JohnTestData left = gson.fromJson(json,JohnTestData.class);
right.address = left.address;
right.age = left.age;
right.area = left.area;
right.beginCompany = left.beginCompany;
right.beginDate = left.beginDate;
right.city = left.city;
right.email = left.email;
right.height = left.height;
right.id = left.id;
right.isFemale = left.isFemale;
right.isMarried = left.isMarried;
right.leaveDate = left.leaveDate;
right.name = left.name;
right.phone = left.phone;
right.weight = left.weight;
right.school = left.school;
right.style = left.style;
right.mrRight = left.mrRight;
return System.nanoTime() - start;
}
}
(JohnTestData.java)
public final class JohnTestData {
public long id;
public String name;
public String school;
public int age;
public boolean isFemale;
public String area;
public String city;
public String address;
public String phone;
public String email;
public boolean isMarried;
public String beginDate;
public String beginCompany;
public int height;
public int weight;
public int style;
public String leaveDate;
public JohnTestData mrRight;
}
注: 这里JohnTestData left里的mrRight不能再指向left,即如果出现循环引用,gson和fastjson可能发生异常.