Skip to content

Commit 6e6c73e

Browse files
committed
Add deserialize json benchmark
1 parent 508517c commit 6e6c73e

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

benches/serde.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
21
use std::time::Duration;
32

3+
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
4+
45
use mlua::prelude::*;
56

67
fn collect_gc_twice(lua: &Lua) {
@@ -43,6 +44,38 @@ fn encode_json(c: &mut Criterion) {
4344
});
4445
}
4546

47+
fn decode_json(c: &mut Criterion) {
48+
let lua = Lua::new();
49+
50+
let decode = lua
51+
.create_function(|lua, s: String| {
52+
lua.to_value(&serde_json::from_str::<serde_json::Value>(&s).unwrap())
53+
})
54+
.unwrap();
55+
let json = r#"{
56+
"name": "Clark Kent",
57+
"address": {
58+
"city": "Smallville",
59+
"state": "Kansas",
60+
"country": "USA"
61+
},
62+
"age": 22,
63+
"parents": ["Jonathan Kent", "Martha Kent"],
64+
"superman": true,
65+
"interests": ["flying", "saving the world", "kryptonite"]
66+
}"#;
67+
68+
c.bench_function("deserialize json", |b| {
69+
b.iter_batched(
70+
|| collect_gc_twice(&lua),
71+
|_| {
72+
decode.call::<_, LuaTable>(json).unwrap();
73+
},
74+
BatchSize::SmallInput,
75+
);
76+
});
77+
}
78+
4679
criterion_group! {
4780
name = benches;
4881
config = Criterion::default()
@@ -51,6 +84,7 @@ criterion_group! {
5184
.noise_threshold(0.02);
5285
targets =
5386
encode_json,
87+
decode_json,
5488
}
5589

5690
criterion_main!(benches);

0 commit comments

Comments
 (0)