我是靠谱客的博主 兴奋可乐,这篇文章主要介绍序列化性能测试:jdk和fastjson序列化性能测试: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

复制代码
1
2
3
4
5
6
7
8
9
10
11
/** * 1w,写入耗时:10974 * 1w,写入耗时:10849 * 1w,写入耗时:10759 * 1k,写入耗时:1224 * 1k,写入耗时:1099 * 1k,写入耗时:1093 * 1k,写入耗时:1075 * 1k,写入耗时:1101 */

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

复制代码
1
2
3
4
10w,写入耗时:151796 10w,写入耗时:284473 10w,写入耗时:284109

jdk序列化

复制代码
1
2
3
4
5
6
7
8
9
10
11
/** * 1w,写入耗时:11136 * 1w,写入耗时:11002 * 1w,写入耗时:11009 * 1k,写入耗时:1175 * 1k,写入耗时:1109 * 1k,写入耗时:1108 * 1k,写入耗时:1087 * 1k,写入耗时:1096 */

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

复制代码
1
2
3
4
10w,写入耗时:159640 10w,写入耗时:286590 10w,写入耗时:289706

读取

json

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
/** * 1w,读取耗时:1501 * 1w,读取耗时:1373 * 1w,读取耗时:1345 * 1w,读取耗时:1347 * 1w,读取耗时:1350 * 10w,读取耗时:13137 * 10w,读取耗时:13064 * 10w,读取耗时:13111 * 10w,读取耗时:13151 * 10w,读取耗时:13094 */

jdk序列化

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
/** * 1w,读取耗时:1738 * 1w,读取耗时:1553 * 1w,读取耗时:1561 * 1w,读取耗时:1565 * 1w,读取耗时:1565 * 10w,读取耗时:14954 * 10w,读取耗时:14728 * 10w,读取耗时:14817 * 10w,读取耗时:14679 * 10w,读取耗时:14722 */

实体类

复制代码
1
2
3
4
5
6
7
public class TokenDetails implements Serializable { private String token; private String username; private Set<String> role; // get set }

jdk序列化工具

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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; } } }

操作类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@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(); } } }

建表语句

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
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;

最后

以上就是兴奋可乐最近收集整理的关于序列化性能测试:jdk和fastjson序列化性能测试:jdk和fastjson的全部内容,更多相关序列化性能测试内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部