From 0a78b32019b629cbda0add2dfedf8a1004a65bd1 Mon Sep 17 00:00:00 2001 From: Brandon Vrooman Date: Wed, 1 Nov 2023 16:07:05 -0400 Subject: [PATCH] test: Add state root and contract id benchmarks (#1462) Related issues: - https://github.com/FuelLabs/fuel-vm/issues/599 --------- Co-authored-by: xgreenx --- CHANGELOG.md | 1 + .../benches/{contract_root.rs => contract.rs} | 29 ++++++++++++++++++- benches/benches/vm.rs | 5 ++-- 3 files changed, 32 insertions(+), 3 deletions(-) rename benches/benches/{contract_root.rs => contract.rs} (63%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cfaab83d5a..de57f089f32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). Description of the upcoming release here. ### Added +- [#1642](https://github.com/FuelLabs/fuel-core/pull/1462): Added benchmark to measure the performance of contract state and contract ID calculation; use for gas costing. - [#1465](https://github.com/FuelLabs/fuel-core/pull/1465): Improvements for keygen cli and crates - [#1457](https://github.com/FuelLabs/fuel-core/pull/1457): Fixing incorrect measurement for fast(µs) opcodes. - [#1456](https://github.com/FuelLabs/fuel-core/pull/1456): Added flushing of the RocksDB during a graceful shutdown. diff --git a/benches/benches/contract_root.rs b/benches/benches/contract.rs similarity index 63% rename from benches/benches/contract_root.rs rename to benches/benches/contract.rs index 7e059e40db4..e85d9374ecf 100644 --- a/benches/benches/contract_root.rs +++ b/benches/benches/contract.rs @@ -2,7 +2,10 @@ use criterion::{ Criterion, Throughput, }; -use fuel_core_types::fuel_tx::Contract; +use fuel_core_types::fuel_tx::{ + Contract, + StorageSlot, +}; use rand::{ rngs::StdRng, Rng, @@ -46,3 +49,27 @@ pub fn contract_root(c: &mut Criterion) { group.finish(); } + +pub fn state_root(c: &mut Criterion) { + let rng = &mut StdRng::seed_from_u64(8586); + + let mut group = c.benchmark_group("state_root"); + + const N: usize = 20; + let sizes = successors(Some(2), |n| Some(n * 2)).take(N); + for (i, size) in sizes.enumerate() { + let gen_storage_slot = || rng.gen::(); + let storage_slots = std::iter::repeat_with(gen_storage_slot) + .take(size) + .collect::>(); + group.throughput(Throughput::Bytes(size as u64)); + let name = format!("state_root_from_slots_2^{exp:#02}", exp = i + 1); + group.bench_function(name, |b| { + b.iter(|| { + Contract::initial_state_root(storage_slots.iter()); + }) + }); + } + + group.finish(); +} diff --git a/benches/benches/vm.rs b/benches/benches/vm.rs index 68d192a239a..b067a3610cb 100644 --- a/benches/benches/vm.rs +++ b/benches/benches/vm.rs @@ -1,4 +1,4 @@ -mod contract_root; +mod contract; mod utils; mod vm_set; @@ -11,7 +11,7 @@ use criterion::{ Criterion, }; -use contract_root::*; +use contract::*; use fuel_core_benches::*; use fuel_core_storage::transactional::Transaction; use fuel_core_types::fuel_asm::Instruction; @@ -95,6 +95,7 @@ fn vm(c: &mut Criterion) { flow::run(c); mem::run(c); contract_root(c); + state_root(c); } criterion_group!(benches, vm);