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

criterion: cuprate-helper #322

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
581 changes: 394 additions & 187 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ resolver = "2"
members = [
# Binaries
"binaries/cuprated",

# Benchmarks
"benches/benchmark/bin",
"benches/benchmark/lib",
"benches/benchmark/example",
"benches/criterion/example",
"benches/criterion/cuprate-json-rpc",

"benches/criterion/cuprate-helper",
# Consensus
"consensus",
"consensus/context",
Expand Down
2 changes: 1 addition & 1 deletion benches/benchmark/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ tracing-subscriber = { workspace = true, features = ["fmt", "std", "env-filter"]
[dev-dependencies]

[lints]
workspace = true
workspace = true
26 changes: 26 additions & 0 deletions benches/criterion/cuprate-helper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "cuprate-criterion-helper"
version = "0.0.0"
edition = "2021"
description = "Criterion benchmarks for cuprate-helper"
license = "MIT"
authors = ["hinto-janai"]
repository = "https://github.com/Cuprate/cuprate/tree/main/benches/criterion/example"
keywords = ["cuprate", "helper", "criterion", "benchmark"]

[dependencies]
cuprate-constants = { path = "../../../constants", features = ["block"] }
cuprate-helper = { path = "../../../helper", features = ["cast", "map", "num", "tx"] }
cuprate-test-utils = { path = "../../../test-utils" }

criterion = { workspace = true }
function_name = { workspace = true }
monero-serai = { workspace = true }
serde_json = { workspace = true, features = ["default"] }

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

[lints]
workspace = true
40 changes: 40 additions & 0 deletions benches/criterion/cuprate-helper/benches/cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! Benchmarks for `cuprate_helper::cast`.
#![allow(unused_attributes, unused_crate_dependencies)]

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use function_name::named;

use cuprate_helper::cast::{
i32_to_isize, i64_to_isize, isize_to_i64, u32_to_usize, u64_to_usize, usize_to_u64,
};

criterion_group! {
name = benches;
config = Criterion::default();
targets = integer, unsigned,
}
criterion_main!(benches);

/// Benchmark integer casts.
#[named]
fn integer(c: &mut Criterion) {
c.bench_function(function_name!(), |b| {
b.iter(|| {
black_box(i32_to_isize(black_box(0)));
black_box(i64_to_isize(black_box(0)));
black_box(isize_to_i64(black_box(0)));
});
});
}

/// Benchmark unsigned integer casts.
#[named]
fn unsigned(c: &mut Criterion) {
c.bench_function(function_name!(), |b| {
b.iter(|| {
black_box(u32_to_usize(black_box(0)));
black_box(u64_to_usize(black_box(0)));
black_box(usize_to_u64(black_box(0)));
});
});
}
14 changes: 14 additions & 0 deletions benches/criterion/cuprate-helper/benches/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! `cuprate_helper` benchmarks.
#![allow(unused_crate_dependencies)]

mod cast;
mod map;
mod num;
mod tx;

criterion::criterion_main! {
cast::benches,
map::benches,
num::benches,
tx::benches,
}
70 changes: 70 additions & 0 deletions benches/criterion/cuprate-helper/benches/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! Benchmarks for `cuprate_helper::cast`.
#![allow(unused_attributes, unused_crate_dependencies)]

use criterion::black_box as b;
use criterion::{criterion_group, criterion_main, Criterion};
use function_name::named;

use cuprate_constants::block::MAX_BLOCK_HEIGHT;
use cuprate_helper::map;

criterion_group! {
name = benches;
config = Criterion::default();
targets =
combine_low_high_bits_to_u128,
split_u128_into_low_high_bits,
timelock_to_u64,
u64_to_timelock,
}
criterion_main!(benches);

/// Benchmark [`curpate_helper::map::combine_low_high_bits_to_u128`].
#[named]
fn combine_low_high_bits_to_u128(c: &mut Criterion) {
c.bench_function(function_name!(), |bench| {
bench.iter(|| {
b(map::combine_low_high_bits_to_u128(b(0), b(0)));
});
});
}

/// Benchmark [`curpate_helper::map::split_u128_into_low_high_bits`].
#[named]
fn split_u128_into_low_high_bits(c: &mut Criterion) {
c.bench_function(function_name!(), |bench| {
bench.iter(|| {
b(map::split_u128_into_low_high_bits(b(0)));
});
});
}

/// Benchmark [`curpate_helper::map::timelock_to_u64`].
#[named]
fn timelock_to_u64(c: &mut Criterion) {
c.bench_function(function_name!(), |bench| {
bench.iter(|| {
b(map::timelock_to_u64(b(
monero_serai::transaction::Timelock::None,
)));
b(map::timelock_to_u64(b(
monero_serai::transaction::Timelock::Time(0),
)));
b(map::timelock_to_u64(b(
monero_serai::transaction::Timelock::Block(0),
)));
});
});
}

/// Benchmark [`curpate_helper::map::u64_to_timelock`].
#[named]
fn u64_to_timelock(c: &mut Criterion) {
c.bench_function(function_name!(), |bench| {
bench.iter(|| {
b(map::u64_to_timelock(b(0)));
b(map::u64_to_timelock(b(MAX_BLOCK_HEIGHT)));
b(map::u64_to_timelock(b(MAX_BLOCK_HEIGHT + 1)));
});
});
}
64 changes: 64 additions & 0 deletions benches/criterion/cuprate-helper/benches/num.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Benchmarks for `cuprate_helper::cast`.
#![allow(unused_attributes, unused_crate_dependencies)]

use criterion::black_box as b;
use criterion::{criterion_group, criterion_main, Criterion};
use function_name::named;

use cuprate_helper::num;

criterion_group! {
name = benches;
config = Criterion::default();
targets =
cmp_float,
cmp_float_nan,
get_mid,
median,
}
criterion_main!(benches);

/// Benchmark [`curpate_helper::num::cmp_float`].
#[named]
fn cmp_float(c: &mut Criterion) {
c.bench_function(function_name!(), |bench| {
bench.iter(|| {
b(num::cmp_float(b(0.0), b(0.0)));
});
});
}

/// Benchmark [`curpate_helper::num::cmp_float_nan`].
#[named]
fn cmp_float_nan(c: &mut Criterion) {
c.bench_function(function_name!(), |bench| {
bench.iter(|| {
b(num::cmp_float_nan(b(0.0), b(0.0)));
});
});
}

/// Benchmark [`curpate_helper::num::get_mid`].
#[named]
fn get_mid(c: &mut Criterion) {
c.bench_function(function_name!(), |bench| {
bench.iter(|| {
b(num::get_mid(b(0_u8), b(0_u8)));
b(num::get_mid(b(1_i64), b(10_i64)));
b(num::get_mid(b(0.0_f32), b(0.0_f32)));
b(num::get_mid(b(0.0_f64), b(10.0_f64)));
});
});
}

/// Benchmark [`curpate_helper::num::median`].
#[named]
fn median(c: &mut Criterion) {
c.bench_function(function_name!(), |bench| {
bench.iter(|| {
b(num::median(b(vec![0_u8, 1, 2, 3, 4, 5])));
b(num::median(b(vec![0.0_f32, 1.0, 2.0, 3.0, 4.0, 5.0])));
b(num::median(b(vec![0_u64; 100])));
});
});
}
28 changes: 28 additions & 0 deletions benches/criterion/cuprate-helper/benches/tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Benchmarks for `cuprate_helper::cast`.
#![allow(unused_attributes, unused_crate_dependencies)]

use criterion::black_box as b;
use criterion::{criterion_group, criterion_main, Criterion};
use function_name::named;

use cuprate_helper::tx;
use cuprate_test_utils::data::{TX_V1_SIG0, TX_V1_SIG2, TX_V2_RCT3};

criterion_group! {
name = benches;
config = Criterion::default();
targets = tx_fee,
}
criterion_main!(benches);

/// Benchmark [`curpate_helper::tx::tx_fee`].
#[named]
fn tx_fee(c: &mut Criterion) {
c.bench_function(function_name!(), |bench| {
bench.iter(|| {
b(tx::tx_fee(b(&TX_V1_SIG0.tx)));
b(tx::tx_fee(b(&TX_V1_SIG2.tx)));
b(tx::tx_fee(b(&TX_V2_RCT3.tx)));
});
});
}
1 change: 1 addition & 0 deletions benches/criterion/cuprate-helper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#![allow(unused_crate_dependencies, reason = "used in benchmarks")]
2 changes: 1 addition & 1 deletion benches/criterion/cuprate-json-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ name = "main"
harness = false

[lints]
workspace = true
workspace = true