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

测试结果如下:

序列化工具 时间(ms) Gson 37235 FastJson 7135 Jackson 12622



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

原创粉丝点击