Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benches: Explicit benches relevant to #1860 #1870

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use sbor::*;
use scrypto::prelude::*;

#[derive(Sbor, ScryptoEvent)]
Expand Down Expand Up @@ -92,39 +91,58 @@ mod transaction_limits_substate {
}

impl TransactionLimitSubstateTest {
pub fn write_large_value(raw_array_size: usize) -> Global<TransactionLimitSubstateTest> {
// SBOR encoding of Vec<u8>
let mut buf = Vec::new();
let mut encoder = VecEncoder::<ScryptoCustomValueKind>::new(&mut buf, 100);
encoder
.write_payload_prefix(SCRYPTO_SBOR_V1_PAYLOAD_PREFIX)
.unwrap();
encoder.write_value_kind(ValueKind::Array).unwrap();
encoder.write_value_kind(ValueKind::U8).unwrap();
encoder.write_size(raw_array_size).unwrap();
buf.reserve(raw_array_size);
let new_len = buf.len() + raw_array_size;
unsafe { buf.set_len(new_len) };

pub fn write_large_values(
raw_array_sizes: Vec<usize>,
) -> Global<TransactionLimitSubstateTest> {
// Create a KVStore
let kv_store = KeyValueStore::<u32, Vec<u8>>::new();

// Insert into store
let key_payload = scrypto_encode(&1u32).unwrap();
let handle = ScryptoVmV1Api::kv_store_open_entry(
kv_store.id.as_node_id(),
&key_payload,
LockFlags::MUTABLE,
);
unsafe { wasm_api::kv_entry::kv_entry_write(handle, buf.as_ptr(), buf.len()) };
ScryptoVmV1Api::kv_entry_close(handle);
let mut key_value = 0u32;
for raw_array_size in raw_array_sizes {
// SBOR encoding of Vec<u8>
let mut buf = Vec::new();
let mut encoder = VecEncoder::<ScryptoCustomValueKind>::new(&mut buf, 100);
encoder
.write_payload_prefix(SCRYPTO_SBOR_V1_PAYLOAD_PREFIX)
.unwrap();
encoder.write_value_kind(ValueKind::Array).unwrap();
encoder.write_value_kind(ValueKind::U8).unwrap();
encoder.write_size(raw_array_size).unwrap();
buf.reserve(raw_array_size);
let new_len = buf.len() + raw_array_size;
unsafe { buf.set_len(new_len) };

// Insert into store
let key_payload = scrypto_encode(&key_value).unwrap();
let handle = ScryptoVmV1Api::kv_store_open_entry(
kv_store.id.as_node_id(),
&key_payload,
LockFlags::MUTABLE,
);
unsafe { wasm_api::kv_entry::kv_entry_write(handle, buf.as_ptr(), buf.len()) };
ScryptoVmV1Api::kv_entry_close(handle);

key_value += 1;
}

// Put the kv store into a component
TransactionLimitSubstateTest { kv_store }
.instantiate()
.prepare_to_globalize(OwnerRole::None)
.globalize()
}

pub fn read_values(&self, limit: u32) {
for key in 0..limit {
let key_payload = scrypto_encode(&key).unwrap();
let handle = ScryptoVmV1Api::kv_store_open_entry(
self.kv_store.id.as_node_id(),
&key_payload,
LockFlags::read_only(),
);
let _raw_bytes = ScryptoVmV1Api::kv_entry_read(handle);
ScryptoVmV1Api::kv_entry_close(handle);
}
}
}
}

Expand Down
112 changes: 106 additions & 6 deletions radix-engine-tests/benches/costing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,46 @@ use sbor::rust::iter;
use scrypto_test::prelude::LedgerSimulatorBuilder;
use wabt::wat2wasm;

fn bench_decode_sbor(c: &mut Criterion) {
fn bench_decode_rpd_to_manifest_value(c: &mut Criterion) {
let payload = include_workspace_asset_bytes!("radix-transaction-scenarios", "radiswap.rpd");
println!("Payload size: {}", payload.len());
c.bench_function("costing::decode_sbor", |b| {
c.bench_function("costing::decode_rpd_to_manifest_value", |b| {
b.iter(|| manifest_decode::<ManifestValue>(payload))
});
}

fn bench_decode_sbor_bytes(c: &mut Criterion) {
fn bench_decode_rpd_to_manifest_raw_value(c: &mut Criterion) {
let payload = include_workspace_asset_bytes!("radix-transaction-scenarios", "radiswap.rpd");
println!("Payload size: {}", payload.len());
c.bench_function("costing::decode_rpd_to_manifest_raw_value", |b| {
b.iter(|| manifest_decode::<ManifestRawValue>(payload))
});
}

fn bench_decode_bytes_to_manifest_value(c: &mut Criterion) {
let payload = manifest_encode(include_workspace_asset_bytes!(
"radix-transaction-scenarios",
"radiswap.rpd"
))
.unwrap();
println!("Payload size: {}", payload.len());
c.bench_function("costing::decode_sbor_bytes", |b| {
c.bench_function("costing::decode_bytes_to_manifest_value", |b| {
b.iter(|| manifest_decode::<ManifestValue>(&payload))
});
}

fn bench_decode_bytes_to_manifest_raw_value(c: &mut Criterion) {
let payload = manifest_encode(include_workspace_asset_bytes!(
"radix-transaction-scenarios",
"radiswap.rpd"
))
.unwrap();
println!("Payload size: {}", payload.len());
c.bench_function("costing::decode_bytes_to_manifest_raw_value", |b| {
b.iter(|| manifest_decode::<ManifestRawValue>(&payload))
});
}

fn bench_validate_sbor_payload(c: &mut Criterion) {
let package_definition = manifest_decode::<PackageDefinition>(include_workspace_asset_bytes!(
"radix-transaction-scenarios",
Expand Down Expand Up @@ -228,10 +248,88 @@ fn bench_prepare_wasm(c: &mut Criterion) {
});
}

fn bench_execute_transaction_creating_big_vec_substates(c: &mut Criterion) {
let mut ledger = LedgerSimulatorBuilder::new().without_kernel_trace().build();

let (code, definition) = PackageLoader::get("transaction_limits");
let package_address =
ledger.publish_package((code, definition), BTreeMap::new(), OwnerRole::None);

let substate_sizes = [
1000,
100000,
MAX_SUBSTATE_VALUE_SIZE - 100,
MAX_SUBSTATE_VALUE_SIZE - 100,
MAX_SUBSTATE_VALUE_SIZE - 100,
MAX_SUBSTATE_VALUE_SIZE - 100,
];

c.bench_function(
"costing::execute_transaction_creating_big_vec_substates",
|b| {
b.iter(|| {
ledger
.call_function(
package_address,
"TransactionLimitSubstateTest",
"write_large_values",
manifest_args!(&substate_sizes),
)
.expect_commit_success();
})
},
);
}

fn bench_execute_transaction_reading_big_vec_substates(c: &mut Criterion) {
let mut ledger = LedgerSimulatorBuilder::new().without_kernel_trace().build();

let (code, definition) = PackageLoader::get("transaction_limits");
let package_address =
ledger.publish_package((code, definition), BTreeMap::new(), OwnerRole::None);

let substate_sizes = [
1000,
100000,
MAX_SUBSTATE_VALUE_SIZE - 100,
MAX_SUBSTATE_VALUE_SIZE - 100,
MAX_SUBSTATE_VALUE_SIZE - 100,
MAX_SUBSTATE_VALUE_SIZE - 100,
];
let component_address = ledger
.call_function(
package_address,
"TransactionLimitSubstateTest",
"write_large_values",
manifest_args!(&substate_sizes),
)
.expect_commit_success()
.new_component_addresses()[0];

let substates_to_read = substate_sizes.len() as u32;

c.bench_function(
"costing::execute_transaction_reading_big_vec_substates",
|b| {
b.iter(|| {
ledger
.call_method(
component_address,
"read_values",
manifest_args!(substates_to_read),
)
.expect_commit_success();
})
},
);
}

criterion_group!(
costing,
bench_decode_sbor,
bench_decode_sbor_bytes,
bench_decode_rpd_to_manifest_value,
bench_decode_rpd_to_manifest_raw_value,
bench_decode_bytes_to_manifest_value,
bench_decode_bytes_to_manifest_raw_value,
bench_validate_sbor_payload,
bench_validate_sbor_payload_bytes,
bench_validate_secp256k1,
Expand All @@ -241,5 +339,7 @@ criterion_group!(
bench_deserialize_wasm,
bench_validate_wasm,
bench_prepare_wasm,
bench_execute_transaction_creating_big_vec_substates,
bench_execute_transaction_reading_big_vec_substates,
);
criterion_main!(costing);
8 changes: 4 additions & 4 deletions radix-engine-tests/tests/system/transaction_limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ fn test_default_substate_size_limit() {
.call_function(
package_address,
"TransactionLimitSubstateTest",
"write_large_value",
manifest_args!(MAX_SUBSTATE_VALUE_SIZE - 17),
"write_large_values",
manifest_args!([MAX_SUBSTATE_VALUE_SIZE - 17]),
)
.build();
let receipt = ledger.execute_manifest(manifest, vec![]);
Expand All @@ -221,8 +221,8 @@ fn test_default_substate_size_limit() {
.call_function(
package_address,
"TransactionLimitSubstateTest",
"write_large_value",
manifest_args!(MAX_SUBSTATE_VALUE_SIZE - 16),
"write_large_values",
manifest_args!([MAX_SUBSTATE_VALUE_SIZE - 16]),
)
.build();
let receipt = ledger.execute_manifest(manifest, vec![]);
Expand Down
Loading