add benches/criterion/cuprate-rpc-types

This commit is contained in:
hinto.janai 2024-10-25 19:45:34 -04:00
parent 1cada331f0
commit a5ba54422f
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
7 changed files with 179 additions and 0 deletions

13
Cargo.lock generated
View file

@ -758,6 +758,19 @@ dependencies = [
"serde_json",
]
[[package]]
name = "cuprate-criterion-rpc-types"
version = "0.0.0"
dependencies = [
"criterion",
"cuprate-epee-encoding",
"cuprate-rpc-types",
"cuprate-test-utils",
"function_name",
"paste",
"serde_json",
]
[[package]]
name = "cuprate-cryptonight"
version = "0.1.0"

View file

@ -9,6 +9,7 @@ members = [
"benches/benchmark/example",
"benches/criterion/example",
"benches/criterion/cuprate-json-rpc",
"benches/criterion/cuprate-rpc-types",
# Consensus
"consensus",
"consensus/fast-sync",

View file

@ -0,0 +1,26 @@
[package]
name = "cuprate-criterion-rpc-types"
version = "0.0.0"
edition = "2021"
description = "Criterion benchmarking for cuprate-rpc-types"
license = "MIT"
authors = ["hinto-janai"]
repository = "https://github.com/Cuprate/cuprate/tree/main/benches/criterion/cuprate-rpc-types"
keywords = ["cuprate", "rpc", "types", "criterion", "benchmark"]
[dependencies]
cuprate-epee-encoding = { path = "../../../net/epee-encoding" }
cuprate-rpc-types = { path = "../../../rpc/types", features = ["serde", "epee"] }
cuprate-test-utils = { path = "../../../test-utils" }
criterion = { workspace = true }
function_name = { workspace = true }
serde_json = { workspace = true, features = ["default"] }
paste = { workspace = true }
[[bench]]
name = "main"
harness = false
[lints]
workspace = true

View file

@ -0,0 +1,65 @@
//! This module contains benchmarks for any
//! non-trivial/manual `epee` implementation.
//!
//! Types with the standard `epee` derive implementation are not included.
#![allow(unused_attributes, unused_crate_dependencies)]
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use function_name::named;
use cuprate_epee_encoding::{from_bytes, to_bytes};
use cuprate_rpc_types::bin::{GetBlocksRequest, GetBlocksResponse};
criterion_group! {
name = benches;
config = Criterion::default();
targets =
epee_to_bytes_get_blocks_request,
epee_from_bytes_get_blocks_request,
epee_to_bytes_get_blocks_response,
epee_from_bytes_get_blocks_response,
}
criterion_main!(benches);
/// TODO
macro_rules! impl_epee_benchmark {
(
$(
$t:ty
),* $(,)?
) => { paste::paste! {
$(
#[named]
fn [<epee_from_bytes_ $t:snake>](c: &mut Criterion) {
let bytes = to_bytes($t::default()).unwrap();
c.bench_function(function_name!(), |b| {
b.iter_batched(
|| bytes.clone(),
|mut bytes| drop(from_bytes::<$t, _>(black_box(&mut bytes)).unwrap()),
BatchSize::SmallInput,
);
});
}
#[named]
fn [<epee_to_bytes_ $t:snake>](c: &mut Criterion) {
let t = $t::default();
c.bench_function(function_name!(), |b| {
b.iter_batched(
|| t.clone(),
|t| drop(to_bytes(black_box(t)).unwrap()),
BatchSize::SmallInput,
);
});
}
)*
}};
}
impl_epee_benchmark! {
GetBlocksRequest,
GetBlocksResponse
}

View file

@ -0,0 +1,10 @@
//! Benchmarks for `cuprate-json-rpc`.
#![allow(unused_crate_dependencies)]
mod serde;
mod epee;
criterion::criterion_main! {
epee::benches,
serde::benches,
}

View file

@ -0,0 +1,63 @@
//! This module contains benchmarks for any
//! non-trivial/manual `serde` implementation.
//!
//! Types with the standard `serde` derive implementation are not included.
#![allow(unused_attributes, unused_crate_dependencies)]
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use function_name::named;
use serde_json::{from_str, to_string};
use cuprate_rpc_types::misc::TxEntry;
criterion_group! {
name = benches;
config = Criterion::default();
targets =
serde_from_str_tx_entry,
serde_to_string_tx_entry,
}
criterion_main!(benches);
/// TODO
macro_rules! impl_serde_benchmark {
(
$(
// The type to test =>
// A `const: &str` from `cuprate_test_utils` for that type or just an inline expression
$t:ty => $t_example:expr
),* $(,)?
) => { paste::paste! {
$(
#[named]
fn [<serde_from_str_ $t:snake>](c: &mut Criterion) {
c.bench_function(function_name!(), |b| {
b.iter(|| {
drop(from_str::<$t>(
black_box($t_example)
).unwrap());
});
});
}
#[named]
fn [<serde_to_string_ $t:snake>](c: &mut Criterion) {
let t = $t::default();
c.bench_function(function_name!(), |b| {
b.iter_batched(
|| t.clone(),
|t| drop(to_string(black_box(&t)).unwrap()),
BatchSize::SmallInput,
);
});
}
)*
}};
}
impl_serde_benchmark! {
TxEntry => r#"{"as_hex":"","as_json":"","double_spend_seen":false,"prunable_as_hex":"","prunable_hash":"","pruned_as_hex":"","received_timestamp":0,"relayed":false,"tx_hash":"","in_pool":false}"#,
// Distribution => "TODO: enable after type is finalized"
}

View file

@ -0,0 +1 @@
#![allow(unused_crate_dependencies, reason = "used in benchmarks")]