Skip to content

Commit

Permalink
Add a benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVeryDarkness committed Oct 8, 2024
1 parent e670d00 commit 2dc4c18
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Criterion.rs with Benchmark Action
on: [push]

permissions:
contents: write
deployments: write

jobs:
benchmark:
name: Run Criterion.rs benchmark example
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Install Rust Toolchain
id: toolchain
uses: dtolnay/rust-toolchain@v1
with:
toolchain: "nightly"
- name: Prepare for benchmark
run: cargo run --example benchmark_integers
- name: Run benchmark
run: cargo bench -- --output-format bencher | tee output.txt
- name: Store benchmark result - separate results repo
uses: benchmark-action/github-action-benchmark@v1
with:
name: Criterion.rs Benchmark
tool: "cargo"
output-file-path: output.txt
github-token: ${{ secrets.BENCHMARK_ACTION_BOT_TOKEN }}
auto-push: true
# Show alert with commit comment on detecting possible performance regression
alert-threshold: "200%"
comment-on-alert: true
fail-on-alert: true
alert-comment-cc-users: "@TheVeryDarkness"
gh-repository: "github.com/TheVeryDarkness/iof-benchmark-results"
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ default = []

[dev-dependencies]
anyhow = "1.0.86"
criterion = "0.5.1"
ntest = "0.9.3"

[[bench]]
name = "read_cursor"
harness = false

[[bench]]
name = "read_file"
harness = false

[[bench]]
name = "show"
harness = false
1 change: 1 addition & 0 deletions benches/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
integers.txt
41 changes: 41 additions & 0 deletions benches/read_cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use criterion::{criterion_group, criterion_main, Criterion};
use iof::{unwrap, InputStream, ReadInto, ReadOneInto};
use std::{fs::read_to_string, io::Cursor};

const COUNT: usize = 0x10000 * 4;

fn many_integers(c: &mut Criterion) {
let s = unwrap!(read_to_string("benches/integers.txt"));
c.bench_function("read_all", |_| {
let mut reader = InputStream::new(Cursor::new(s.as_str()));
let results: Vec<i32> = reader.read_all();
assert_eq!(results.len(), COUNT);
})
.bench_function("read_n", |_| {
let mut reader = InputStream::new(Cursor::new(s.as_str()));
let results: Vec<i32> = reader.read_n(COUNT);
assert_eq!(results.len(), COUNT);
})
.bench_function("read", |_| {
let mut reader = InputStream::new(Cursor::new(s.as_str()));
let results: Vec<i32> = reader.read();
assert_eq!(results.len(), COUNT);
})
.bench_function("read while let", |_| {
let mut reader = InputStream::new(Cursor::new(s.as_str()));
let mut results: Vec<i64> = Vec::new();
while let Ok(a) = reader.try_read_one() {
results.push(a);
}
})
.bench_function("read for in", |_| {
let mut reader = InputStream::new(Cursor::new(s.as_str()));
let mut results: Vec<i64> = Vec::new();
for _ in 0..COUNT {
results.push(reader.read());
}
});
}

criterion_group!(benches, many_integers);
criterion_main!(benches);
43 changes: 43 additions & 0 deletions benches/read_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use criterion::{criterion_group, criterion_main, Criterion};
use iof::{unwrap, InputStream, ReadInto, ReadOneInto};
use std::{fs::File, io::BufReader};

const COUNT: usize = 0x10000 * 4;

fn create_reader() -> InputStream<BufReader<File>> {
let f = unwrap!(File::open("benches/integers.txt"));
let buf = BufReader::new(f);
InputStream::new(buf)
}

fn many_integers(c: &mut Criterion) {
c.bench_function("read_all", |_| {
let mut reader = create_reader();
let _: Vec<i32> = reader.read_all();
})
.bench_function("read_n", |_| {
let mut reader = create_reader();
let _: Vec<i32> = reader.read_n(COUNT);
})
.bench_function("read", |_| {
let mut reader = create_reader();
let _: Vec<i32> = reader.read();
})
.bench_function("read while let", |_| {
let mut reader = create_reader();
let mut results: Vec<i64> = Vec::new();
while let Ok(a) = reader.try_read_one() {
results.push(a);
}
})
.bench_function("read for in", |_| {
let mut reader = create_reader();
let mut results: Vec<i64> = Vec::new();
for _ in 0..COUNT {
results.push(reader.read());
}
});
}

criterion_group!(benches, many_integers);
criterion_main!(benches);
38 changes: 38 additions & 0 deletions benches/show.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use criterion::{criterion_group, criterion_main, Criterion};
use iof::{sep_by, show, unwrap};

fn many_integers(c: &mut Criterion) {
let s = unwrap!(std::fs::read_to_string("benches/integers.txt"));
c.bench_function("default_separator", |_| {
let mut buf = Vec::new();
let integers = [
-0x10000i64..0x10000i64,
-0x80000000..-0x7fff0000,
0x70000000..0x70010000,
];
show!(sep_by!(integers, "\n", " ") => buf);
assert_eq!(buf, s.as_bytes());
})
.bench_function("longer_separator", |_| {
let mut buf = Vec::new();
let integers = [
-0x10000i64..0x10000i64,
-0x80000000..-0x7fff0000,
0x70000000..0x70010000,
];
show!(sep_by!(integers, "\n\n", " :: ") => buf);
assert_eq!(buf, s.as_bytes());
})
.bench_function("char_separator", |_| {
let mut buf = Vec::new();
let integers = [
-0x10000i64..0x10000i64,
-0x80000000..-0x7fff0000,
0x70000000..0x70010000,
];
show!(sep_by!(integers, '\n', ' ') => buf);
});
}

criterion_group!(benches, many_integers);
criterion_main!(benches);
23 changes: 23 additions & 0 deletions examples/benchmark_integers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Run `cargo run --example benchmark_integers` to prepare data for the benchmark.
use iof::{sep_by, show, unwrap};
use std::{fs::File, path::PathBuf};

fn main() {
let integers = [
-0x10000i64..0x10000i64,
-0x80000000..-0x7fff0000,
0x70000000..0x70010000,
];

let manifest_dir = env!("CARGO_MANIFEST_DIR");

let mut results = PathBuf::from(manifest_dir);
results.push("benches");
results.push("integers.txt");

let mut f = unwrap!(File::create(results));

// Write integers to file
show!(sep_by!(integers, "\n", " ") => f);
}

0 comments on commit 2dc4c18

Please sign in to comment.