Gson,FastJson, Jackson测试
来源:互联网 发布:linux spi驱动 编辑:程序博客网 时间:2024/05/20 01:11
Gson是google开源的json序列化和反序列化工具,拥趸众多;FastJson是阿里巴巴开源的json工具,国产高品质;而 Jackson是老牌的json工具,spring的默认json转化就使用jackson。
json作为当前序列化最重要的形式之一,经常应用在各个项目中,而选择一种好的序列化工具当然就很重要,因此,我简单对这三种json工具进行了测试,测试使用Junit 4,分别对一个简单的User对象进行并发10个线程,每个线程1kw次的序列化操作,具体代码如下。
@Test public void testGson() throws InterruptedException { final CountDownLatch startLatch = new CountDownLatch(1); final CountDownLatch endLatch = new CountDownLatch(10); final Gson gson = new Gson(); final AtomicBoolean isFail = new AtomicBoolean(false); MallUser user = new MallUser(); user.setAgreementUid("1111"); user.setId(7809808L); user.setUserId(898080L); ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 0 , TimeUnit.SECONDS, new ArrayBlockingQueue<>(100)); long start = System.currentTimeMillis(); for(int i= 0; i < 10; i++){ executor.execute(new Runnable() { @Override public void run() { try{ startLatch.await(); for (int j = 0; j < 10000000; j ++){ gson.toJson(user); } }catch (Exception e){ System.out.println(e.getMessage()); isFail.set(true); }finally { endLatch.countDown(); } } }); } startLatch.countDown(); endLatch.await(); System.out.println("gson isFail: " + isFail); System.out.println("gson 10 threads, execute time: " + (System.currentTimeMillis() - start)); } @Test public void testJackson() throws InterruptedException { final CountDownLatch startLatch = new CountDownLatch(1); final CountDownLatch endLatch = new CountDownLatch(10); final ObjectMapper mapper = new ObjectMapper(); final AtomicBoolean isFail = new AtomicBoolean(false); MallUser user = new MallUser(); user.setAgreementUid("1111"); user.setId(7809808L); user.setUserId(898080L); ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 0 , TimeUnit.SECONDS, new ArrayBlockingQueue<>(100)); long start = System.currentTimeMillis(); for(int i= 0; i < 10; i++){ executor.execute(new Runnable() { @Override public void run() { try{ startLatch.await(); for (int j = 0; j < 10000000; j ++){ mapper.writeValueAsString(user); } }catch (Exception e){ System.out.println(e.getMessage()); isFail.set(true); }finally { endLatch.countDown(); } } }); } startLatch.countDown(); endLatch.await(); System.out.println("jackson isFail: " + isFail); System.out.println("jackson 10 threads, execute time: " + (System.currentTimeMillis() - start)); } @Test public void testFastJson() throws InterruptedException { final CountDownLatch startLatch = new CountDownLatch(1); final CountDownLatch endLatch = new CountDownLatch(10); final AtomicBoolean isFail = new AtomicBoolean(false); MallUser user = new MallUser(); user.setAgreementUid("1111"); user.setId(7809808L); user.setUserId(898080L); ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 0 , TimeUnit.SECONDS, new ArrayBlockingQueue<>(100)); long start = System.currentTimeMillis(); for(int i= 0; i < 10; i++){ executor.execute(new Runnable() { @Override public void run() { try{ startLatch.await(); for (int j = 0; j < 10000000; j ++){ JSON.toJSONString(user); } }catch (Exception e){ System.out.println(e.getMessage()); isFail.set(true); }finally { endLatch.countDown(); } } }); } startLatch.countDown(); endLatch.await(); System.out.println("fastjson isFail: " + isFail); System.out.println("fastjson 10 threads, execute time: " + (System.currentTimeMillis() - start)); }
执行输出结果:
gson isFail: falsegson 10 threads, execute time: 37235fastjson isFail: falsefastjson 10 threads, execute time: 7135jackson isFail: falsejackson 10 threads, execute time: 12622
测试结果如下:
从执行结果我们可以看到,Gson的耗时最长,是FastJson的5倍左右,是Jackson的三倍左右,而FastJson是耗时最短的,当然,这个结果并不能完全说明FastJson的性能最好,因为测试的对象毕竟是个简单的对象,如果涉及到的对象比较复杂,也许就不是这个结果了。这也说明了FastJson是一款很好的json序列化工具,国产良心之作!希望以后会有越来越多国内的互联网公司致力于开源吧。
阅读全文
0 0
- Gson,FastJson, Jackson测试
- Gson,fastjson,jackson效率测试程序
- Gson、Jackson和FastJSON
- FastJSON、Gson、Jackson和Json-lib性能测试
- FastJSON、Jackson和Gson用法
- FastJson Jackson Gson使用教程
- FastJSON,Gson,Jackson性能对比
- java jackson fastjson gson json-lib
- FastJSON、Gson和Jackson性能对比
- FastJSON、Gson和Jackson性能对比
- Android Jackson、Gson、FastJson解析框架对比
- FastJSON、Gson和Jackson性能对比
- FastJSON、Gson和Jackson性能对比
- FastJSON、Gson和Jackson性能对比
- FastJson、Gson、Jackson json解析性能比较
- fastjson,gson,jackson类型性能对比
- FastJSON、Gson和Jackson性能对比
- FastJSON、Gson和Jackson性能对比
- 内存映射文件原理探索
- 拦截有序广播
- html基础知识二
- 用Navicat 计划任务备份mysql数据库和创建计划任务时报错80070057
- RBAC权限管理
- Gson,FastJson, Jackson测试
- Minimum Moves to Equal Array Elements
- 刷机常出问题及解决方案
- LeetCode 9. Palindrome Number
- Caffe(18)——绘制网络模型
- golang Glide 包管理windows下使用
- 【MFC/C++操作Excel】Excel篇 (OLE/COM)
- Git Windows version default option false(core.ignorecase is true)
- Android pm 命令