标签:ejs ali fast object 数量级 oid sys fas 字符
迷信了很长时间的阿里,但今天突然做了一些json库的测试,结果大跌眼镜,评测
结果是,org.json库与org.simple.json库相差不多,在一个数量级,但是阿里fastjson、
net.sf.json库和Gson速度最慢。或许已经有人想喷了,但我就是呈现一个测试结果,
欢迎来指导我理解的不足,或许阿里还有很多特殊的泛型的处理和类型转换我没有用
到,本帖的所要阐明的是处理字符串解析并取得对象的一些属性没有涉及泛型时的速
度评测。
import junit.framework.TestCase;
import org.json.JSONException;
import org.json.simple.parser.ParseException;
import org.junit.Test;
import org.springframework.boot.json.GsonJsonParser;
import java.util.List;
import java.util.Map;
/**
* 对比json对象执行效率
* @author wangzhe 2020-04-14
*/
public class JSONObjectTest {
//static final String jsonStr = "{\"m\":\"W95\",\"r\":\"0.9\",\"f\":\"90\",\"a\":\"100\",\"l\":{\"m\":\"9999999999999999\",\"r\":\"0.1\",\"f\":\"10\",\"a\":\"100\"}}";
static final String jsonStr = "{\"m\":\"W95\",\"r\":\"0.9\",\"f\":\"90\",\"a\":\"100\",\"l\":[{\"m\":\"9999999999999999\",\"r\":\"0.1\",\"f\":\"10\",\"a\":\"100\"},{\"m\":\"FT\",\"r\":\"0.5\",\"f\":\"50\",\"a\":\"100\"}]}";
@Test
public void testAlibabaFastJsonParseTest() {
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonStr);
String m = jsonObject.getString("m");
String r = jsonObject.getString("r");
String f = jsonObject.getString("f");
String a = jsonObject.getString("a");
TestCase.assertEquals("W95", m);
TestCase.assertEquals("0.9", r);
TestCase.assertEquals("90", f);
TestCase.assertEquals("100", a);
com.alibaba.fastjson.JSONArray jsonArray = jsonObject.getJSONArray("l");
com.alibaba.fastjson.JSONObject jsonObjectSub = (com.alibaba.fastjson.JSONObject) jsonArray.get(0);
//com.alibaba.fastjson.JSONObject jsonObjectSub = jsonObject.getJSONObject("l");
m = jsonObjectSub.getString("m");
r = jsonObjectSub.getString("r");
f = jsonObjectSub.getString("f");
a = jsonObjectSub.getString("a");
TestCase.assertEquals("9999999999999999", m);
TestCase.assertEquals("0.1", r);
TestCase.assertEquals("10", f);
TestCase.assertEquals("100", a);
jsonObjectSub = (com.alibaba.fastjson.JSONObject) jsonArray.get(1);
m = jsonObjectSub.getString("m");
r = jsonObjectSub.getString("r");
f = jsonObjectSub.getString("f");
a = jsonObjectSub.getString("a");
TestCase.assertEquals("FT", m);
TestCase.assertEquals("0.5", r);
TestCase.assertEquals("50", f);
TestCase.assertEquals("100", a);
System.out.println("AlibabaFastJson:"+ jsonObject.toString());
}
@Test
public void testGoogleSimpleJsonParseTest() {
try {
org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject) new org.json.simple.parser.JSONParser().parse(jsonStr);
String m = String.valueOf(jsonObject.get("m"));
String r = String.valueOf(jsonObject.get("r"));
String f = String.valueOf(jsonObject.get("f"));
String a = String.valueOf(jsonObject.get("a"));
TestCase.assertEquals("W95", m);
TestCase.assertEquals("0.9", r);
TestCase.assertEquals("90", f);
TestCase.assertEquals("100", a);
org.json.simple.JSONArray jsonArray = (org.json.simple.JSONArray) jsonObject.get("l");
org.json.simple.JSONObject jsonObjectSub = (org.json.simple.JSONObject) jsonArray.get(0);
// org.json.simple.JSONObject jsonObjectSub = (org.json.simple.JSONObject) jsonObject.get("l");
m = String.valueOf(jsonObjectSub.get("m"));
r = String.valueOf(jsonObjectSub.get("r"));
f = String.valueOf(jsonObjectSub.get("f"));
a = String.valueOf(jsonObjectSub.get("a"));
TestCase.assertEquals("9999999999999999", m);
TestCase.assertEquals("0.1", r);
TestCase.assertEquals("10", f);
TestCase.assertEquals("100", a);
jsonObjectSub = (org.json.simple.JSONObject) jsonArray.get(1);
m = String.valueOf(jsonObjectSub.get("m"));
r = String.valueOf(jsonObjectSub.get("r"));
f = String.valueOf(jsonObjectSub.get("f"));
a = String.valueOf(jsonObjectSub.get("a"));
TestCase.assertEquals("FT", m);
TestCase.assertEquals("0.5", r);
TestCase.assertEquals("50", f);
TestCase.assertEquals("100", a);
System.out.println("GoogleSimpleJson:"+ jsonObject.toString());
} catch (ParseException e) {
e.printStackTrace();
}
}
@Test
public void testOrgJsonParseTest() {
try {
org.json.JSONObject jsonObject = new org.json.JSONObject(jsonStr);
String m = String.valueOf(jsonObject.get("m"));
String r = String.valueOf(jsonObject.get("r"));
String f = String.valueOf(jsonObject.get("f"));
String a = String.valueOf(jsonObject.get("a"));
TestCase.assertEquals("W95", m);
TestCase.assertEquals("0.9", r);
TestCase.assertEquals("90", f);
TestCase.assertEquals("100", a);
org.json.JSONArray jsonArray = jsonObject.getJSONArray("l");
org.json.JSONObject jsonObjectSub = (org.json.JSONObject) jsonArray.get(0);
// org.json.JSONObject jsonObjectSub = (org.json.JSONObject) jsonObject.get("l");
m = String.valueOf(jsonObjectSub.get("m"));
r = String.valueOf(jsonObjectSub.get("r"));
f = String.valueOf(jsonObjectSub.get("f"));
a = String.valueOf(jsonObjectSub.get("a"));
TestCase.assertEquals("9999999999999999", m);
TestCase.assertEquals("0.1", r);
TestCase.assertEquals("10", f);
TestCase.assertEquals("100", a);
jsonObjectSub = (org.json.JSONObject) jsonArray.get(1);
m = jsonObjectSub.getString("m");
r = jsonObjectSub.getString("r");
f = jsonObjectSub.getString("f");
a = jsonObjectSub.getString("a");
TestCase.assertEquals("FT", m);
TestCase.assertEquals("0.5", r);
TestCase.assertEquals("50", f);
TestCase.assertEquals("100", a);
System.out.println("OrgJson: "+ jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
@Test
public void testNetsfJsonParseTest() {
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonStr);
String m = String.valueOf(jsonObject.get("m"));
String r = String.valueOf(jsonObject.get("r"));
String f = String.valueOf(jsonObject.get("f"));
String a = String.valueOf(jsonObject.get("a"));
TestCase.assertEquals("W95", m);
TestCase.assertEquals("0.9", r);
TestCase.assertEquals("90", f);
TestCase.assertEquals("100", a);
net.sf.json.JSONArray jsonArray = jsonObject.getJSONArray("l");
net.sf.json.JSONObject jsonObjectSub = (net.sf.json.JSONObject) jsonArray.get(0);
// net.sf.json.JSONObject jsonObjectSub = (net.sf.json.JSONObject) jsonObject.get("l");
m = String.valueOf(jsonObjectSub.get("m"));
r = String.valueOf(jsonObjectSub.get("r"));
f = String.valueOf(jsonObjectSub.get("f"));
a = String.valueOf(jsonObjectSub.get("a"));
TestCase.assertEquals("9999999999999999", m);
TestCase.assertEquals("0.1", r);
TestCase.assertEquals("10", f);
TestCase.assertEquals("100", a);
jsonObjectSub = (net.sf.json.JSONObject) jsonArray.get(1);
m = jsonObjectSub.getString("m");
r = jsonObjectSub.getString("r");
f = jsonObjectSub.getString("f");
a = jsonObjectSub.getString("a");
TestCase.assertEquals("FT", m);
TestCase.assertEquals("0.5", r);
TestCase.assertEquals("50", f);
TestCase.assertEquals("100", a);
System.out.println("NetsfJson: "+ jsonObject.toString());
}
@Test
public void testGsonParseTest() {
Map<String, Object> jsonObject = new GsonJsonParser().parseMap(jsonStr);
String m = String.valueOf(jsonObject.get("m"));
String r = String.valueOf(jsonObject.get("r"));
String f = String.valueOf(jsonObject.get("f"));
String a = String.valueOf(jsonObject.get("a"));
TestCase.assertEquals("W95", m);
TestCase.assertEquals("0.9", r);
TestCase.assertEquals("90", f);
TestCase.assertEquals("100", a);
List<Map<String, Object>> jsonObjectSubList = (List<Map<String, Object>>) jsonObject.get("l");
Map<String, Object> jsonObjectSub = jsonObjectSubList.get(0);
// Map<String, Object> jsonObjectSub = (Map<String, Object>) jsonObject.get("l");
m = String.valueOf(jsonObjectSub.get("m"));
r = String.valueOf(jsonObjectSub.get("r"));
f = String.valueOf(jsonObjectSub.get("f"));
a = String.valueOf(jsonObjectSub.get("a"));
TestCase.assertEquals("9999999999999999", m);
TestCase.assertEquals("0.1", r);
TestCase.assertEquals("10", f);
TestCase.assertEquals("100", a);
jsonObjectSub = jsonObjectSubList.get(1);
m = String.valueOf(jsonObjectSub.get("m"));
r = String.valueOf(jsonObjectSub.get("r"));
f = String.valueOf(jsonObjectSub.get("f"));
a = String.valueOf(jsonObjectSub.get("a"));
TestCase.assertEquals("FT", m);
TestCase.assertEquals("0.5", r);
TestCase.assertEquals("50", f);
TestCase.assertEquals("100", a);
System.out.println("Gson: "+ jsonObject.toString());
}
}
标签:ejs ali fast object 数量级 oid sys fas 字符
原文地址:https://blog.51cto.com/7680741/2487376