序列化性能测试:jdk和fastjson

序列化性能测试:jdk和fastjson

我开发一个认证授权框架时,需要添加数据库存储token或者会话,于是想测试使用jdk的blob存储解析快还是存储string的json序列化解析快,从而选择他们其中一个。

1、数据库存储json快
2、数据库存储二进制数据库

我的开源框架:
分布式会话:https://gitee.com/lingkang_top/final-session
授权认证: https://gitee.com/lingkang_top/final-security

我的电脑配置,
AMD Ryzen 5 3600 6-Core Processor 3.59 GHz 6核12线程
32G=金士顿DDR4 16G*2 三星 1T固态、金士顿 1T固态
jdk8fastjson 1.2.72 mysql 5.7.34
验证方式,一个普通springboot项目,启动后执行序列化写入、读取反序列化。

结论

我用脚投票了,选择fastjson序列化与反序列化,因为大家性能差不多,json领先不明显,但是json很直观看到数据。

写入

json

    /**
     * 1w,写入耗时:10974
     * 1w,写入耗时:10849
     * 1w,写入耗时:10759
     * 1k,写入耗时:1224
     * 1k,写入耗时:1099
     * 1k,写入耗时:1093
     * 1k,写入耗时:1075
     * 1k,写入耗时:1101
     */

洗个澡出来测试写入10w,3次写入,后面两次变为了进5分钟,应该是缓存用满。

10w,写入耗时:151796
10w,写入耗时:284473
10w,写入耗时:284109

jdk序列化

    /**
     * 1w,写入耗时:11136
     * 1w,写入耗时:11002
     * 1w,写入耗时:11009
     * 1k,写入耗时:1175
     * 1k,写入耗时:1109
     * 1k,写入耗时:1108
     * 1k,写入耗时:1087
     * 1k,写入耗时:1096
     */

测试写入10w,3次写入,后面两次变为了进5分钟,应该是缓存用满。

10w,写入耗时:159640
10w,写入耗时:286590
10w,写入耗时:289706

读取

json

    /**
     * 1w,读取耗时:1501
     * 1w,读取耗时:1373
     * 1w,读取耗时:1345
     * 1w,读取耗时:1347
     * 1w,读取耗时:1350
     * 10w,读取耗时:13137
     * 10w,读取耗时:13064
     * 10w,读取耗时:13111
     * 10w,读取耗时:13151
     * 10w,读取耗时:13094
     */

jdk序列化

    /**
     * 1w,读取耗时:1738
     * 1w,读取耗时:1553
     * 1w,读取耗时:1561
     * 1w,读取耗时:1565
     * 1w,读取耗时:1565
     * 10w,读取耗时:14954
     * 10w,读取耗时:14728
     * 10w,读取耗时:14817
     * 10w,读取耗时:14679
     * 10w,读取耗时:14722
     */

实体类

public class TokenDetails implements Serializable {
    private String token;
    private String username;
    private Set<String> role;
// get set
}

jdk序列化工具

import java.io.*;

/**
 * @author lingkang
 * Created by 2022/1/27
 * 序列化反序列化工具
 */
public class SerializationUtils {
    public static byte[] serialization(Object source) throws IOException {
        ByteArrayOutputStream bytes = null;
        try {
            bytes = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bytes);
            oos.writeObject(source);
            oos.close();
            return bytes.toByteArray();
        } catch (IOException e) {
            throw e;
        } finally {
            if (bytes != null)
                bytes.close();
        }
    }

    public static Object unSerialization(InputStream in) throws IOException, ClassNotFoundException {
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(in);
            return objectInputStream.readObject();
        } catch (Exception e) {
            throw e;
        }
    }
}

操作类

@RestController
public class WebController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("")
    public Object index() throws Exception {
        long start = System.currentTimeMillis();
        json();
        // jdkSer();
        long time = System.currentTimeMillis() - start;
        System.out.println("1k,写入耗时:" + time);
        return time;
    }

    @GetMapping("/read")
    public Object read() throws Exception {
        long start = System.currentTimeMillis();
        readJson();
        // readSer();
        long time = System.currentTimeMillis() - start;
        System.out.println("10w,读取耗时:" + time);
        return time;
    }


    /**
     * 1w,写入耗时:10974
     * 1w,写入耗时:10849
     * 1w,写入耗时:10759
     * 1k,写入耗时:1224
     * 1k,写入耗时:1099
     * 1k,写入耗时:1093
     * 1k,写入耗时:1075
     * 1k,写入耗时:1101
     */
    private void json() {
        HashSet<String> role = new HashSet<>();
        role.add("user");
        role.add("admin");
        for (int i = 0; i < 1000; i++) {
            String token = UUID.randomUUID().toString();
            TokenDetails details = new TokenDetails();
            details.setToken(token);
            details.setRole(role);
            details.setUsername("username-" + i);
            jdbcTemplate.update("insert into test_json values(?,?)",
                    token, JSONObject.toJSONString(details)
            );
        }
    }

    /**
     * 1w,写入耗时:11136
     * 1w,写入耗时:11002
     * 1w,写入耗时:11009
     * 1k,写入耗时:1175
     * 1k,写入耗时:1109
     * 1k,写入耗时:1108
     * 1k,写入耗时:1087
     * 1k,写入耗时:1096
     */
    private void jdkSer() throws IOException {
        HashSet<String> role = new HashSet<>();
        role.add("user");
        role.add("admin");
        for (int i = 0; i < 1000; i++) {
            String token = UUID.randomUUID().toString();
            TokenDetails details = new TokenDetails();
            details.setToken(token);
            details.setRole(role);
            details.setUsername("username-" + i);
            byte[] serialization = SerializationUtils.serialization(details);
            jdbcTemplate.update("insert into test_ser values(?,?)", token, serialization);
        }
    }

    /**
     * 1w,读取耗时:1501
     * 1w,读取耗时:1373
     * 1w,读取耗时:1345
     * 1w,读取耗时:1347
     * 1w,读取耗时:1350
     * 10w,读取耗时:13137
     * 10w,读取耗时:13064
     * 10w,读取耗时:13111
     * 10w,读取耗时:13151
     * 10w,读取耗时:13094
     */
    private void readJson() {
        for (int i = 0; i < 100000; i++) {
            String token = "08915ec0-0ec6-44b3-8c21-c68c73edd068";
            String details = jdbcTemplate.queryForObject("select content from test_json where id=?",
                    new Object[]{token},
                    String.class
            );
            TokenDetails details1 = JSONObject.parseObject(details, TokenDetails.class);
            details1.getRole();
        }
    }

    /**
     * 1w,读取耗时:1738
     * 1w,读取耗时:1553
     * 1w,读取耗时:1561
     * 1w,读取耗时:1565
     * 1w,读取耗时:1565
     * 10w,读取耗时:14954
     * 10w,读取耗时:14728
     * 10w,读取耗时:14817
     * 10w,读取耗时:14679
     * 10w,读取耗时:14722
     */
    private void readSer() throws Exception {
        for (int i = 0; i < 100000; i++) {
            String token = "08fafa73-9ede-4c86-8175-8ab78c5f87bf";
            Blob blob = jdbcTemplate.queryForObject("select content from test_ser where id=?",
                    new String[]{token}, Blob.class);
            TokenDetails details = (TokenDetails) SerializationUtils.unSerialization(blob.getBinaryStream());
            details.getRole();
        }
    }
}

建表语句

CREATE TABLE `test_json` (
  `id` varchar(225) NOT NULL,
  `content` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `test_ser` (
  `id` varchar(225) NOT NULL,
  `content` blob,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
posted @ 2022-09-16 00:08  凌康  阅读(198)  评论(0编辑  收藏  举报