Gson,FastJson, Jackson测试

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: false
gson 10 threads, execute time: 37235
fastjson isFail: false
fastjson 10 threads, execute time: 7135
jackson isFail: false
jackson 10 threads, execute time: 12622

测试结果如下:

序列化工具时间(ms)
Gson37235
FastJson7135
Jackson12622



从执行结果我们可以看到,Gson的耗时最长,是FastJson的5倍左右,是Jackson的三倍左右,而FastJson是耗时最短的,当然,这个结果并不能完全说明FastJson的性能最好,因为测试的对象毕竟是个简单的对象,如果涉及到的对象比较复杂,也许就不是这个结果了。这也说明了FastJson是一款很好的json序列化工具,国产良心之作!希望以后会有越来越多国内的互联网公司致力于开源吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值