在往常接口测试工作中都会遇到将对象(实体)类转成Json格式的数据构造成body发送给服务端,抽空对比了下几个第三方工具类的转换速度(jackson、Gson、fastjson)
步骤:
1.新建实体类Body
2.在测试用例@Test方法中统计各种第三方包的时间
上代码(代码上不了,放开源中国的Git上:https://gitee.com/veryxly/TestAPIDamo,后期这个项目慢慢做成接口测试demo)
BodyClass为实体类
TestCase为测试方法
可以用这种方法构造接口请求的body(Json类型),也可以将Json数据转换为实体。原先的做法是用map存储数据,然后对map进行转换成Json。
结论:Gson相对较快,其次是fastjson,然后jackson,以上仅实验所得数据!
贴部分代码,主要是转换部分
@Test
public void TC_XZ_001() throws Exception{
String jsonStr;
BodyClass body = new BodyClass();
body.setAccess_token("8447ff97-9b8c-4224-9cec");
body.setCatalog("1");
body.setTag("1");
body.setPageSize("10");
body.setPage("1");
body.setDataType("json");
long startTime = System.currentTimeMillis();
ObjectMapper objectMapper = new ObjectMapper();
jsonStr = objectMapper.writeValueAsString(body);
long endTime = System.currentTimeMillis();
long userTime = endTime - startTime;
System.out.println("jackson json:"+jsonStr+";Time:"+userTime);
}
@Test
public void TC_XZ_002(){
String jsonStr;
BodyClass body = new BodyClass();
body.setAccess_token("8447ff97-9b8c-4224-9cec");
body.setCatalog("1");
body.setTag("1");
body.setPageSize("10");
body.setPage("1");
body.setDataType("json");
long startTime = System.currentTimeMillis();
Gson gson = new Gson();
jsonStr = gson.toJson(body);
long endTime = System.currentTimeMillis();
long userTime = endTime - startTime;
System.out.println("Gson json:"+jsonStr+";Time:"+userTime);
}
@Test
public void TC_XZ_003(){
Object jsonStr;
BodyClass body = new BodyClass();
body.setAccess_token("8447ff97-9b8c-4224-9cec");
body.setCatalog("1");
body.setTag("1");
body.setPageSize("10");
body.setPage("1");
body.setDataType("json");
long startTime = System.currentTimeMillis();
jsonStr = JSON.toJSON(body);
long endTime = System.currentTimeMillis();
long userTime = endTime - startTime;
System.out.println("fastjson json:"+jsonStr+";Time:"+userTime);
}
期间怀疑jackson包用错了,试了三四个版本放弃了,是相对慢!Time单位为ms。
jackson json:{"access_token":"8447ff97-9b8c-4224-9cec","catalog":"1","tag":"1","pageSize":"10","page":"1","dataType":"json"};Time:375
Gson json:{"access_token":"8447ff97-9b8c-4224-9cec","catalog":"1","tag":"1","pageSize":"10","page":"1","dataType":"json"};Time:237
fastjson json:{"access_token":"8447ff97-9b8c-4224-9cec","catalog":"1","dataType":"json","pageSize":"10","page":"1","tag":"1"};Time:405
jackson json:{"access_token":"8447ff97-9b8c-4224-9cec","catalog":"1","tag":"1","pageSize":"10","page":"1","dataType":"json"};Time:395
Gson json:{"access_token":"8447ff97-9b8c-4224-9cec","catalog":"1","tag":"1","pageSize":"10","page":"1","dataType":"json"};Time:181
fastjson json:{"access_token":"8447ff97-9b8c-4224-9cec","catalog":"1","dataType":"json","pageSize":"10","page":"1","tag":"1"};Time:203
jackson json:{"access_token":"8447ff97-9b8c-4224-9cec","catalog":"1","tag":"1","pageSize":"10","page":"1","dataType":"json"};Time:351
Gson json:{"access_token":"8447ff97-9b8c-4224-9cec","catalog":"1","tag":"1","pageSize":"10","page":"1","dataType":"json"};Time:63
fastjson json:{"access_token":"8447ff97-9b8c-4224-9cec","catalog":"1","dataType":"json","pageSize":"10","page":"1","tag":"1"};Time:91
网友评论