在往常接口測試工作中都會遇到將對象(實體)類轉(zhuǎn)成Json格式的數(shù)據(jù)構(gòu)造成body發(fā)送給服務(wù)端,抽空對比了下幾個第三方工具類的轉(zhuǎn)換速度(jackson鸠天、Gson讼育、fastjson)
步驟:
1.新建實體類Body
2.在測試用例@Test方法中統(tǒng)計各種第三方包的時間
上代碼(代碼上不了,放開源中國的Git上:https://gitee.com/veryxly/TestAPIDamo,后期這個項目慢慢做成接口測試demo)
BodyClass為實體類
TestCase為測試方法
可以用這種方法構(gòu)造接口請求的body(Json類型)奶段,也可以將Json數(shù)據(jù)轉(zhuǎn)換為實體饥瓷。原先的做法是用map存儲數(shù)據(jù),然后對map進行轉(zhuǎn)換成Json痹籍。
結(jié)論:Gson相對較快呢铆,其次是fastjson,然后jackson蹲缠,以上僅實驗所得數(shù)據(jù)棺克!
貼部分代碼,主要是轉(zhuǎn)換部分
@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