/ ledger / benches / block.rs
block.rs
  1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
  2  // This file is part of the deltavm library.
  3  
  4  // Licensed under the Apache License, Version 2.0 (the "License");
  5  // you may not use this file except in compliance with the License.
  6  // You may obtain a copy of the License at:
  7  
  8  // http://www.apache.org/licenses/LICENSE-2.0
  9  
 10  // Unless required by applicable law or agreed to in writing, software
 11  // distributed under the License is distributed on an "AS IS" BASIS,
 12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  // See the License for the specific language governing permissions and
 14  // limitations under the License.
 15  
 16  #[macro_use]
 17  extern crate criterion;
 18  
 19  use deltavm_console::{account::PrivateKey, network::MainnetV0, prelude::*};
 20  use deltavm_ledger::test_helpers::{TestChainBuilder, sample_genesis_block};
 21  
 22  use criterion::Criterion;
 23  
 24  type CurrentNetwork = MainnetV0;
 25  
 26  /// Helper method to benchmark serialization.
 27  fn bench_serialization<T: Serialize + DeserializeOwned + ToBytes + FromBytes + Clone>(
 28      c: &mut Criterion,
 29      name: &str,
 30      object: T,
 31  ) {
 32      ///////////////
 33      // Serialize //
 34      ///////////////
 35  
 36      // deltavm_utilities::ToBytes
 37      c.bench_function(&format!("{name}::to_bytes_le"), |b| b.iter(|| object.to_bytes_le().unwrap()));
 38  
 39      // bincode::serialize
 40      c.bench_function(&format!("{name}::serialize (bincode)"), |b| b.iter(|| bincode::serialize(&object).unwrap()));
 41  
 42      // serde_json::to_string
 43      c.bench_function(&format!("{name}::to_string (serde_json)"), |b| {
 44          b.iter(|| serde_json::to_string(&object).unwrap())
 45      });
 46  
 47      /////////////////
 48      // Deserialize //
 49      /////////////////
 50  
 51      // deltavm_utilities::FromBytes
 52      {
 53          let buffer = object.to_bytes_le().unwrap();
 54          c.bench_function(&format!("{name}::from_bytes_le"), |b| b.iter(|| T::from_bytes_le(&buffer).unwrap()));
 55  
 56          let buffer = object.to_bytes_le().unwrap();
 57          c.bench_function(&format!("{name}::from_bytes_le_unchecked"), |b| {
 58              b.iter(|| T::from_bytes_le_unchecked(&buffer).unwrap())
 59          });
 60      }
 61      // bincode::deserialize
 62      {
 63          let buffer = bincode::serialize(&object).unwrap();
 64          c.bench_function(&format!("{name}::deserialize (bincode)"), move |b| {
 65              b.iter(|| bincode::deserialize::<T>(&buffer).unwrap())
 66          });
 67      }
 68      // serde_json::from_str
 69      {
 70          let object = serde_json::to_string(&object).unwrap();
 71          c.bench_function(&format!("{name}::from_str (serde_json)"), move |b| {
 72              b.iter(|| serde_json::from_str::<T>(&object).unwrap())
 73          });
 74      }
 75  }
 76  
 77  fn block_serialization(c: &mut Criterion) {
 78      let mut rng = TestRng::default();
 79  
 80      let mut builder = TestChainBuilder::<CurrentNetwork>::new(&mut rng).unwrap();
 81      let block = builder.generate_block(&mut rng).unwrap();
 82  
 83      bench_serialization(c, "Block", block);
 84  }
 85  
 86  fn block_header_serialization(c: &mut Criterion) {
 87      let mut rng = TestRng::default();
 88  
 89      let mut builder = TestChainBuilder::<CurrentNetwork>::new(&mut rng).unwrap();
 90      let block = builder.generate_block(&mut rng).unwrap();
 91  
 92      bench_serialization(c, "Header", *block.header());
 93  }
 94  
 95  fn block_transactions_serialization(c: &mut Criterion) {
 96      let mut rng = TestRng::default();
 97      let block = sample_genesis_block(&mut rng);
 98      bench_serialization(c, "Transactions", block.transactions().clone());
 99  }
100  
101  fn transaction_serialization(c: &mut Criterion) {
102      let mut rng = TestRng::default();
103      let block = sample_genesis_block(&mut rng);
104      let transaction = block.transactions().iter().next().unwrap().clone();
105      bench_serialization(c, "Transaction", transaction);
106  }
107  
108  fn transition_serialization(c: &mut Criterion) {
109      let mut rng = TestRng::default();
110      let block = sample_genesis_block(&mut rng);
111      let transaction = block.transactions().iter().next().unwrap().clone();
112      let transition = transaction.transitions().next().unwrap().clone();
113      bench_serialization(c, "Transition", transition);
114  }
115  
116  fn signature_serialization(c: &mut Criterion) {
117      let mut rng = TestRng::default();
118      let data = rng.r#gen();
119  
120      let private_key = PrivateKey::<CurrentNetwork>::new(&mut rng).unwrap();
121      let signature = private_key.sign(&[data], &mut rng).unwrap();
122  
123      bench_serialization(c, "Signature", signature);
124  }
125  
126  criterion_group! {
127      name = block;
128      config = Criterion::default().sample_size(10);
129      targets = block_serialization,block_header_serialization, block_transactions_serialization,
130      transaction_serialization, transition_serialization, signature_serialization,
131  }
132  
133  criterion_main!(block);