summaryrefslogtreecommitdiff
path: root/autochomp/benches/parse/main.rs
blob: 64fd88ae5051a4093430635593e89eedd1cc4567 (plain)
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
use std::error::Error;

use chewed::{IterWrapper, Parser};
use chomp::{
    chomp::ast::NamedExpression,
    nibble::{
        self,
        convert::{Context, Convert},
    },
};
use criterion::{
    criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
    Throughput,
};

const INPUTS: &[&str] = &[
    include_str!("data/12.nb"),
    include_str!("data/24.nb"),
    include_str!("data/48.nb"),
    include_str!("data/97.nb"),
    include_str!("data/194.nb"),
    include_str!("data/387.nb"),
    include_str!("data/774.nb"),
    include_str!("data/1548.nb"),
    include_str!("data/3096.nb"),

];

fn parse_autochomp(input: &str) -> Result<NamedExpression, Box<dyn Error>> {
    IterWrapper::new(input.chars())
        .parse::<autochomp::Ast>()
        .map_err(|e| Box::new(e) as Box<dyn Error>)
        .and_then(|ast| {
            ast.convert(&mut Context::default())
                .map_err(|e| Box::new(e) as Box<dyn Error>)
        })
}

fn parse_chomp(input: &str) -> Result<NamedExpression, Box<dyn Error>> {
    syn::parse_str::<nibble::Statement>(input)
        .map_err(|e| Box::new(e) as Box<dyn Error>)
        .and_then(|stmt| {
            stmt.convert(&mut Context::default())
                .map_err(|e| Box::new(e) as Box<dyn Error>)
        })
}

fn bench_parse(c: &mut Criterion) {
    let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
    let mut group = c.benchmark_group("Parse");
    group.plot_config(plot_config);
    for (i, input) in INPUTS.iter().enumerate() {
        group.throughput(Throughput::Bytes(input.len() as u64));
        group.bench_with_input(BenchmarkId::new("Chomp", i), *input, |b, i| {
            b.iter(|| parse_chomp(i))
        });
        group.bench_with_input(BenchmarkId::new("AutoChomp", i), *input, |b, i| {
            b.iter(|| parse_autochomp(i))
        });
    }
}

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