/ ledger / benches / dag.rs
dag.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  use deltavm_console::prelude::*;
17  use deltavm_ledger::narwhal::subdag::test_helpers::sample_subdag;
18  use deltavm_utilities::bytes::unchecked_deserialize;
19  
20  use criterion::{Criterion, criterion_group, criterion_main};
21  
22  /// Helper method to benchmark serialization.
23  fn bench_serialization<T: Serialize + DeserializeOwned + ToBytes + FromBytes + Clone>(
24      c: &mut Criterion,
25      name: &str,
26      object: T,
27  ) {
28      ///////////////
29      // Serialize //
30      ///////////////
31  
32      // deltavm_utilities::ToBytes
33      c.bench_function(&format!("{name}::to_bytes_le"), |b| b.iter(|| object.to_bytes_le().unwrap()));
34  
35      // bincode::serialize
36      c.bench_function(&format!("{name}::serialize (bincode)"), |b| b.iter(|| bincode::serialize(&object).unwrap()));
37  
38      // serde_json::to_string
39      c.bench_function(&format!("{name}::to_string (serde_json)"), |b| {
40          b.iter(|| serde_json::to_string(&object).unwrap())
41      });
42  
43      /////////////////
44      // Deserialize //
45      /////////////////
46  
47      // deltavm_utilities::FromBytes
48      {
49          let buffer = object.to_bytes_le().unwrap();
50          c.bench_function(&format!("{name}::from_bytes_le"), |b| b.iter(|| T::from_bytes_le(&buffer).unwrap()));
51  
52          c.bench_function(&format!("{name}::from_bytes_le_unchecked"), |b| {
53              b.iter(|| T::from_bytes_le_unchecked(&buffer).unwrap())
54          });
55      }
56  
57      // bincode::deserialize and unchecked_deserialize.
58      {
59          let buffer = bincode::serialize(&object).unwrap();
60          c.bench_function(&format!("{name}::deserialize (bincode)"), |b| {
61              b.iter(|| bincode::deserialize::<T>(&buffer).unwrap())
62          });
63  
64          c.bench_function(&format!("{name}::unchecked_deserialize (bincode)"), |b| {
65              b.iter(|| unchecked_deserialize::<T>(&buffer).unwrap())
66          });
67      }
68  
69      // serde_json::from_str
70      {
71          let object = serde_json::to_string(&object).unwrap();
72          c.bench_function(&format!("{name}::from_str (serde_json)"), move |b| {
73              b.iter(|| serde_json::from_str::<T>(&object).unwrap())
74          });
75      }
76  }
77  
78  fn subdag_serialization(c: &mut Criterion) {
79      let rng = &mut TestRng::default();
80      let subdag = sample_subdag(rng);
81      let batch = subdag.iter().next().unwrap().1.iter().next().unwrap().clone();
82      let batch_header = batch.batch_header().clone();
83  
84      bench_serialization(c, "BatchHeader", batch_header);
85      bench_serialization(c, "BatchCertificate", batch);
86      bench_serialization(c, "Subdag", subdag.clone());
87  }
88  
89  criterion_group! {
90      name = subdag;
91      config = Criterion::default().sample_size(10);
92      targets = subdag_serialization
93  }
94  
95  criterion_main!(subdag);