Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmarks: restore benchmarks #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/lib.rs
Expand Up @@ -703,3 +703,81 @@ mod tests {
assert!(decode(s).is_ok());
}
}
#[cfg(bench)]
mod benches {
use test::{black_box, Bencher};

use crate::{Bech32, Bech32m};

#[bench]
fn bech32_parse_address(bh: &mut Bencher) {
let addr = black_box("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq");

bh.iter(|| {
let tuple = crate::decode(&addr).expect("address is well formed");
black_box(&tuple);
})
}

#[bench]
fn bech32m_parse_address(bh: &mut Bencher) {
let addr = black_box("bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297");

bh.iter(|| {
let tuple = crate::decode(&addr).expect("address is well formed");
black_box(&tuple);
})
}

// Encode with allocation.
#[bench]
fn encode_bech32_address(bh: &mut Bencher) {
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
let (hrp, data) = crate::decode(&addr).expect("address is well formed");

bh.iter(|| {
let s = crate::encode::<Bech32>(hrp, &data).expect("failed to encode");
black_box(&s);
});
}

// Encode without allocation.
#[bench]
fn encode_to_fmt_bech32_address(bh: &mut Bencher) {
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
let mut buf = String::with_capacity(64);

bh.iter(|| {
let res =
crate::encode_to_fmt::<Bech32, _>(&mut buf, hrp, &data).expect("failed to encode");
black_box(&res);
});
}

// Encode with allocation.
#[bench]
fn encode_bech32m_address(bh: &mut Bencher) {
let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297";
let (hrp, data) = crate::decode(&addr).expect("address is well formed");

bh.iter(|| {
let s = crate::encode::<Bech32m>(hrp, &data).expect("failed to encode");
black_box(&s);
});
}

// Encode without allocation.
#[bench]
fn encode_to_fmt_bech32m_address(bh: &mut Bencher) {
let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297";
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
let mut buf = String::with_capacity(64);

bh.iter(|| {
let res =
crate::encode_to_fmt::<Bech32, _>(&mut buf, hrp, &data).expect("failed to encode");
black_box(&res);
});
}
}