forked from toon-format/toon-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_decode.rs
More file actions
92 lines (78 loc) 路 2.61 KB
/
Copy pathencode_decode.rs
File metadata and controls
92 lines (78 loc) 路 2.61 KB
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
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use serde_json::{json, Value};
use toon_format::{decode_default, encode_default};
fn make_tabular(rows: usize) -> Value {
let mut items = Vec::with_capacity(rows);
for i in 0..rows {
items.push(json!({
"id": i,
"name": format!("User_{i}"),
"score": i * 2,
"active": i % 2 == 0,
"tag": format!("tag{i}"),
}));
}
Value::Array(items)
}
fn make_deep_object(depth: usize) -> Value {
let mut value = json!({
"leaf": "value",
"count": 1,
});
for i in 0..depth {
value = json!({
format!("level_{i}"): value,
});
}
value
}
fn make_long_unquoted(words: usize) -> String {
let mut parts = Vec::with_capacity(words);
for i in 0..words {
parts.push(format!("word{i}"));
}
parts.join(" ")
}
fn bench_tabular(c: &mut Criterion) {
let mut group = c.benchmark_group("tabular");
for rows in [128_usize, 1024] {
let value = make_tabular(rows);
let toon = encode_default(&value).expect("encode tabular");
group.bench_with_input(BenchmarkId::new("encode", rows), &value, |b, val| {
b.iter(|| encode_default(black_box(val)).expect("encode tabular"));
});
group.bench_with_input(BenchmarkId::new("decode", rows), &toon, |b, input| {
b.iter(|| decode_default::<Value>(black_box(input)).expect("decode tabular"));
});
}
group.finish();
}
fn bench_deep_object(c: &mut Criterion) {
let mut group = c.benchmark_group("deep_object");
for depth in [32_usize, 128] {
let value = make_deep_object(depth);
let toon = encode_default(&value).expect("encode deep object");
group.bench_with_input(BenchmarkId::new("encode", depth), &value, |b, val| {
b.iter(|| encode_default(black_box(val)).expect("encode deep object"));
});
group.bench_with_input(BenchmarkId::new("decode", depth), &toon, |b, input| {
b.iter(|| decode_default::<Value>(black_box(input)).expect("decode deep object"));
});
}
group.finish();
}
fn bench_long_unquoted(c: &mut Criterion) {
let words = 512;
let long_value = make_long_unquoted(words);
let toon = format!("value: {long_value}");
c.bench_function("decode_long_unquoted", |b| {
b.iter(|| decode_default::<Value>(black_box(&toon)).expect("decode long unquoted"));
});
}
criterion_group!(
benches,
bench_tabular,
bench_deep_object,
bench_long_unquoted
);
criterion_main!(benches);