Skip to content

Commit

Permalink
Consolidate benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
certainty committed Sep 5, 2021
1 parent 9a9a01a commit 1af4721
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ directories = "3.0"
[dev-dependencies]
matches = "0.1"
criterion = "0.3.4"
cargo-criterion = "1.1.0"
quickcheck = "1"
quickcheck_macros = "1.0"
35 changes: 31 additions & 4 deletions benches/compiler_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use braces::compiler::source::*;
use braces::compiler::Compiler;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn compiler_benchmark(c: &mut Criterion) {
fn full_compiler_benchmark(c: &mut Criterion) {
let mut compiler = Compiler::new();
let mut source = StringSource::new(
r#"
Expand All @@ -15,10 +15,37 @@ fn compiler_benchmark(c: &mut Criterion) {
"#,
);

c.bench_function("compile_expression", |b| {
b.iter(|| compiler.compile(black_box(&mut source)))
c.bench_function("Compiler#compile", |b| {
b.iter(|| black_box(compiler.compile(&mut source)))
});
}

criterion_group!(benches, compiler_benchmark);
fn frontend_benchmark(c: &mut Criterion) {
let source = Source::new(
SourceId::synthetic(),
r#"
(define (add x y) (+ x y))
(define (minus x y) (- x y))
(add 10 20)
; this is a comment
(sub 20 10)
"#,
);

let mut frontend = braces::compiler::frontend::Frontend::new();
let datum = frontend.read(&source).unwrap();

c.bench_function("Frontend#pass", |b| {
b.iter(|| black_box(frontend.pass(&source)))
});
c.bench_function("Frontend#read", |b| {
b.iter(|| black_box(frontend.read(&source)))
});
c.bench_function("Frontend#parse", |b| {
b.iter(|| black_box(frontend.parse(&datum)))
});
}

criterion_group!(benches, full_compiler_benchmark, frontend_benchmark);
criterion_main!(benches);
55 changes: 50 additions & 5 deletions benches/vm_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
use braces::compiler::source::*;
use braces::compiler::Compiler;
use braces::vm::stack::Stack;
use braces::vm::value;
use braces::vm::VM;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};

fn vm_fibonacci_benchmark(c: &mut Criterion) {
fn stack_benchmark(c: &mut Criterion) {
let mut stack: Stack<bool> = Stack::new(100);
stack.push(true);
stack.push(false);
stack.push(false);
stack.push(true);

c.bench_function("Stack#pop_push", |b| {
b.iter(|| {
stack.push(black_box(false));
black_box(stack.pop())
})
});
c.bench_function("Stack#at", |b| b.iter(|| black_box(stack.at(3))));
c.bench_function("Stack#peek", |b| b.iter(|| black_box(stack.peek(2))));
c.bench_function("Stack#top", |b| b.iter(|| black_box(stack.top())));
}

fn writer_benchmark(c: &mut Criterion) {
let writer = braces::vm::scheme::writer::Writer::new();
let mut factory = value::Factory::default();
let list = factory.proper_list(vec![
factory.bool_true(),
value::Value::UninternedString(String::from("test")),
]);

c.bench_function("Writer#write_list", |b| {
b.iter(|| black_box(writer.write(&list, &factory)))
});

let vector_input = factory.vector(vec![value::Value::Bool(true), value::Value::Char('z')]);
c.bench_function("Writer#write_vector", |b| {
b.iter(|| black_box(writer.write(&vector_input, &factory)))
});

let symbol_input = factory.symbol(" a weird symbol ");
c.bench_with_input(
BenchmarkId::new("Writer#write_symbol", format!("{:?}", symbol_input)),
&symbol_input,
|b, i| b.iter(|| black_box(writer.write(i, &factory))),
);
}

fn vm_benchmark(c: &mut Criterion) {
let mut source = StringSource::new(
r#"
(define (fib-tc n)
Expand All @@ -23,10 +68,10 @@ fn vm_fibonacci_benchmark(c: &mut Criterion) {
let mut compiler = Compiler::new();
let unit = compiler.compile(&mut source).unwrap();

c.bench_function("interpret_fibonacci", |b| {
b.iter(|| vm.interpret(unit.clone()))
c.bench_function("VM#interpret", |b| {
b.iter(|| black_box(vm.interpret(unit.clone())))
});
}

criterion_group!(benches, vm_fibonacci_benchmark);
criterion_group!(benches, vm_benchmark, stack_benchmark, writer_benchmark);
criterion_main!(benches);

0 comments on commit 1af4721

Please sign in to comment.