1
use criterion::measurement::WallTime;
2
use criterion::{black_box, criterion_group, criterion_main, BenchmarkGroup, Criterion};
3

            
4
const SMALL_OBJECT: &str = r#"{
5
    "@context": "https://www.w3.org/ns/activitystreams",
6
    "summary": "A note",
7
    "type": "Note",
8
    "content": "My dog has fleas.",
9
    "numbers": [1, 2, 4.4],
10
    "keywords": {
11
        "true": true,
12
        "false": false,
13
        "null": null
14
    }
15
}"#;
16

            
17
const SMALL_OBJECT_COMPACT: &str = r#"{"@context":"https://www.w3.org/ns/activitystreams","summary":"A note","type":"Note","content":"My dog has fleas.","numbers":[1,2,4.4],"keywords":{"true":true,"false":false,"null":null}}"#;
18

            
19
2
fn justjson_parse(json: &str) -> justjson::Value<'_> {
20
2
    justjson::Value::from_json(json).unwrap()
21
2
}
22

            
23
2
fn justjson_parse_doc(json: &str) -> justjson::doc::Document<'_> {
24
2
    justjson::doc::Document::from_json(json).unwrap()
25
2
}
26

            
27
2
fn serde_json_value_parse(json: &str) -> serde_json::Value {
28
2
    serde_json::from_str(json).unwrap()
29
2
}
30

            
31
2
fn json_deserializer_parse_bytes(json: &str) -> json_deserializer::Value {
32
2
    json_deserializer::parse(json.as_bytes()).unwrap()
33
2
}
34

            
35
#[cfg(feature = "simd-json")]
36
fn simd_json_parse(json: &mut Vec<u8>) {
37
    let _ = simd_json::to_borrowed_value(json).unwrap();
38
}
39

            
40
2
fn justjson_parse_bytes(json: &str) -> justjson::Value<'_> {
41
2
    justjson::Value::from_json_bytes(json.as_bytes()).unwrap()
42
2
}
43

            
44
2
fn justjson_parse_doc_bytes(json: &str) -> justjson::doc::Document<'_> {
45
2
    justjson::doc::Document::from_json_bytes(json.as_bytes()).unwrap()
46
2
}
47

            
48
2
fn serde_json_value_parse_bytes(json: &str) -> serde_json::Value {
49
2
    serde_json::from_slice(json.as_bytes()).unwrap()
50
2
}
51

            
52
2
fn bench_with_input(mut group: BenchmarkGroup<'_, WallTime>, input: &str) {
53
2
    group.bench_function("justjson/str", |b| {
54
2
        b.iter(|| justjson_parse(black_box(input)));
55
2
    });
56
2

            
57
2
    group.bench_function("justjson/doc/str", |b| {
58
2
        b.iter(|| justjson_parse_doc(black_box(input)));
59
2
    });
60
2

            
61
2
    group.bench_function("justjson/bytes", |b| {
62
2
        b.iter(|| justjson_parse_bytes(black_box(input)));
63
2
    });
64
2

            
65
2
    group.bench_function("justjson/doc/bytes", |b| {
66
2
        b.iter(|| justjson_parse_doc_bytes(black_box(input)));
67
2
    });
68
2

            
69
2
    group.bench_function("serde-json/str", |b| {
70
2
        b.iter(|| serde_json_value_parse(black_box(input)));
71
2
    });
72
2

            
73
2
    group.bench_function("serde-json/bytes", |b| {
74
2
        b.iter(|| serde_json_value_parse_bytes(black_box(input)));
75
2
    });
76
2

            
77
2
    #[cfg(feature = "simd-json")]
78
2
    group.bench_function("simd-json/bytes", |b| {
79
2
        let mut bytes = input.as_bytes().to_vec();
80
2
        b.iter(|| simd_json_parse(black_box(&mut bytes)));
81
2
    });
82
2

            
83
2
    group.bench_function("json-deserializer/bytes", |b| {
84
2
        b.iter(|| json_deserializer_parse_bytes(black_box(input)));
85
2
    });
86
2
}
87

            
88
1
fn benchmarks(c: &mut Criterion) {
89
1
    bench_with_input(c.benchmark_group("small-pretty"), SMALL_OBJECT);
90
1
    bench_with_input(c.benchmark_group("small"), SMALL_OBJECT_COMPACT);
91
1
}
92

            
93
criterion_group!(benches, benchmarks);
94
criterion_main!(benches);