From d2b30c1cbdc326524d67f323834a71916938a5f5 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Fri, 25 Oct 2024 20:03:30 -0400 Subject: [PATCH] add benches --- .../cuprate-rpc-types/benches/epee.rs | 41 ++++++++----- .../cuprate-rpc-types/benches/main.rs | 2 +- .../cuprate-rpc-types/benches/serde.rs | 60 ++++++++++++++----- 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/benches/criterion/cuprate-rpc-types/benches/epee.rs b/benches/criterion/cuprate-rpc-types/benches/epee.rs index 0ac6313..daec6e3 100644 --- a/benches/criterion/cuprate-rpc-types/benches/epee.rs +++ b/benches/criterion/cuprate-rpc-types/benches/epee.rs @@ -1,5 +1,10 @@ //! This module contains benchmarks for any -//! non-trivial/manual `epee` implementation. +//! +//! - non-trivial +//! - manual +//! - common +//! +//! type with a `epee` implementation. //! //! Types with the standard `epee` derive implementation are not included. @@ -11,29 +16,21 @@ 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 { +/// Create [`to_bytes`] and [`from_bytes`] benchmarks for `epee` types. +macro_rules! generate_epee_benchmarks { ( $( $t:ty ),* $(,)? ) => { paste::paste! { + // Generate the benchmarking functions. $( #[named] fn [](c: &mut Criterion) { let bytes = to_bytes($t::default()).unwrap(); + // `iter_batched()` is used so the `Default::default()` + // is not part of the timings. c.bench_function(function_name!(), |b| { b.iter_batched( || bytes.clone(), @@ -56,10 +53,22 @@ macro_rules! impl_epee_benchmark { }); } )* + + // Enable all the benchmark functions created in this macro. + criterion_group! { + name = benches; + config = Criterion::default(); + targets = + $( + [], + [], + )* + } + criterion_main!(benches); }}; } -impl_epee_benchmark! { +generate_epee_benchmarks! { GetBlocksRequest, - GetBlocksResponse + // GetBlocksResponse // TODO: fix epee impl } diff --git a/benches/criterion/cuprate-rpc-types/benches/main.rs b/benches/criterion/cuprate-rpc-types/benches/main.rs index 6267a91..dedb44a 100644 --- a/benches/criterion/cuprate-rpc-types/benches/main.rs +++ b/benches/criterion/cuprate-rpc-types/benches/main.rs @@ -1,8 +1,8 @@ //! Benchmarks for `cuprate-json-rpc`. #![allow(unused_crate_dependencies)] -mod serde; mod epee; +mod serde; criterion::criterion_main! { epee::benches, diff --git a/benches/criterion/cuprate-rpc-types/benches/serde.rs b/benches/criterion/cuprate-rpc-types/benches/serde.rs index 08c7363..c94ee0a 100644 --- a/benches/criterion/cuprate-rpc-types/benches/serde.rs +++ b/benches/criterion/cuprate-rpc-types/benches/serde.rs @@ -1,5 +1,10 @@ //! This module contains benchmarks for any -//! non-trivial/manual `serde` implementation. +//! +//! - non-trivial +//! - manual +//! - common +//! +//! type with a `serde` implementation. //! //! Types with the standard `serde` derive implementation are not included. @@ -9,19 +14,21 @@ 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; +use cuprate_rpc_types::{ + json::{ + CalcPowRequest, GetBlockHeadersRangeResponse, GetBlockResponse, GetBlockTemplateResponse, + GetConnectionsResponse, GetInfoResponse, GetLastBlockHeaderResponse, SyncInfoResponse, + }, + misc::TxEntry, +}; +use cuprate_test_utils::rpc::data::json::{ + CALC_POW_REQUEST, GET_BLOCK_HEADERS_RANGE_RESPONSE, GET_BLOCK_RESPONSE, + GET_BLOCK_TEMPLATE_RESPONSE, GET_CONNECTIONS_RESPONSE, GET_INFO_RESPONSE, + GET_LAST_BLOCK_HEADER_RESPONSE, SYNC_INFO_RESPONSE, +}; -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 { +/// Generate [`from_str`] and [`to_string`] benchmarks for `serde` types. +macro_rules! generate_serde_benchmarks { ( $( // The type to test => @@ -29,6 +36,7 @@ macro_rules! impl_serde_benchmark { $t:ty => $t_example:expr ),* $(,)? ) => { paste::paste! { + // Generate the benchmarking functions. $( #[named] fn [](c: &mut Criterion) { @@ -53,11 +61,35 @@ macro_rules! impl_serde_benchmark { ); }); } + )* + + // Enable all the benchmark functions created in this macro. + criterion_group! { + name = benches; + config = Criterion::default(); + targets = + $( + [], + [], + )* + } + criterion_main!(benches); }}; } -impl_serde_benchmark! { +generate_serde_benchmarks! { + // Custom serde types. 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" + + // Common types or heavy types (heap types, many fields, etc). + GetLastBlockHeaderResponse => GET_LAST_BLOCK_HEADER_RESPONSE, + CalcPowRequest => CALC_POW_REQUEST, + SyncInfoResponse => SYNC_INFO_RESPONSE, + GetInfoResponse => GET_INFO_RESPONSE, + GetBlockResponse => GET_BLOCK_RESPONSE, + GetConnectionsResponse => GET_CONNECTIONS_RESPONSE, + GetBlockTemplateResponse => GET_BLOCK_TEMPLATE_RESPONSE, + GetBlockHeadersRangeResponse => GET_BLOCK_HEADERS_RANGE_RESPONSE }