File: json.rs

package info (click to toggle)
rust-json5 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 500 kB
  • sloc: makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,754 bytes parent folder | download
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
use criterion::{
    AxisScale, BenchmarkId, Criterion, PlotConfiguration, Throughput, criterion_group,
    criterion_main,
};
use serde_json::Value;

fn criterion_benchmark(c: &mut Criterion) {
    // JSON strings to deserialize
    let data = ["small.json", "medium.json", "large.json"].map(|file| {
        (
            file,
            std::fs::read_to_string(format!("benches/data/{file}")).unwrap(),
        )
    });

    let mut group = c.benchmark_group("json");
    group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));

    for (file, input) in data {
        group.sample_size(10);
        group.throughput(Throughput::Bytes(u64::try_from(input.len()).unwrap()));
        group.bench_with_input(
            BenchmarkId::new("callum-oakley/json5-rs", file),
            &input,
            |b, input| {
                b.iter(|| json5::from_str::<Value>(input).unwrap());
            },
        );
        group.bench_with_input(
            BenchmarkId::new("serde-rs/json", file),
            &input,
            |b, input| {
                b.iter(|| serde_json::from_str::<Value>(input).unwrap());
            },
        );
        group.bench_with_input(
            BenchmarkId::new("spyoungtech/json-five-rs", file),
            &input,
            |b, input| {
                b.iter(|| json_five::from_str::<Value>(input).unwrap());
            },
        );
        group.bench_with_input(
            BenchmarkId::new("google/serde_json5", file),
            &input,
            |b, input| {
                b.iter(|| serde_json5::from_str::<Value>(input).unwrap());
            },
        );
    }
    group.finish();
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);