diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2024-03-23 22:30:59 +0000 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2024-03-23 22:31:42 +0000 |
commit | 6e6c73e4c7312d4d5803c4d86e97850955ca51cb (patch) | |
tree | c31d91f2a8197d379513c186e7de4558b9df8a11 | |
parent | 508517c45ec88ddf562fa33bf8af84745ab6a474 (diff) | |
download | mlua-6e6c73e4c7312d4d5803c4d86e97850955ca51cb.zip |
Add deserialize json benchmark
-rw-r--r-- | benches/serde.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/benches/serde.rs b/benches/serde.rs index 6d2718b..2ff193d 100644 --- a/benches/serde.rs +++ b/benches/serde.rs @@ -1,6 +1,7 @@ -use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use std::time::Duration; +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; + use mlua::prelude::*; fn collect_gc_twice(lua: &Lua) { @@ -43,6 +44,38 @@ fn encode_json(c: &mut Criterion) { }); } +fn decode_json(c: &mut Criterion) { + let lua = Lua::new(); + + let decode = lua + .create_function(|lua, s: String| { + lua.to_value(&serde_json::from_str::<serde_json::Value>(&s).unwrap()) + }) + .unwrap(); + let json = r#"{ + "name": "Clark Kent", + "address": { + "city": "Smallville", + "state": "Kansas", + "country": "USA" + }, + "age": 22, + "parents": ["Jonathan Kent", "Martha Kent"], + "superman": true, + "interests": ["flying", "saving the world", "kryptonite"] + }"#; + + c.bench_function("deserialize json", |b| { + b.iter_batched( + || collect_gc_twice(&lua), + |_| { + decode.call::<_, LuaTable>(json).unwrap(); + }, + BatchSize::SmallInput, + ); + }); +} + criterion_group! { name = benches; config = Criterion::default() @@ -51,6 +84,7 @@ criterion_group! { .noise_threshold(0.02); targets = encode_json, + decode_json, } criterion_main!(benches); |