Skip to content

Commit

Permalink
ManagedBufferBuilder gas benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Apr 8, 2024
1 parent 57eb904 commit ed8e28e
Show file tree
Hide file tree
Showing 13 changed files with 674 additions and 1 deletion.
3 changes: 3 additions & 0 deletions contracts/benchmarks/str-repeat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ publish = false
[lib]
path = "src/str_repeat.rs"

[features]
managed-buffer-builder-cached = ["multiversx-sc/managed-buffer-builder-cached"]

[dependencies.multiversx-sc]
version = "0.47.8"
path = "../../../framework/base"
Expand Down
12 changes: 11 additions & 1 deletion contracts/benchmarks/str-repeat/sc-config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
[settings]
main = "str-repeat"

# the only purpose of this config is to specify the allocator
[contracts.str-repeat]
allocator = "leaking"

[contracts.str-repeat-mb-builder-basic]
add-unlabelled = false
add-labels = ["mb-builder"]
add-endpoints = ["init"]

[contracts.str-repeat-mb-builder-cached]
add-unlabelled = false
add-labels = ["mb-builder"]
add-endpoints = ["init"]
features = ["managed-buffer-builder-cached"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"name": "str-repeat",
"steps": [
{
"step": "setState",
"accounts": {
"address:owner": {
"nonce": "0",
"balance": "0"
},
"sc:contract": {
"code": "mxsc:../output/str-repeat-mb-builder-basic.mxsc.json"
}
}
},
{
"step": "scCall",
"id": "benchmark-mb-builder-basic-10",
"comment": "code is smaller, so basic wins here",
"tx": {
"from": "address:owner",
"to": "sc:contract",
"function": "mb_builder_benchmark",
"arguments": [
"0x01020304",
"10"
],
"gasLimit": "10,000,000",
"gasPrice": "0"
},
"expect": {
"out": "*",
"status": "",
"gas": "8855882"
}
},
{
"step": "scCall",
"id": "benchmark-mb-builder-basic-tipping-point",
"comment": "the caching optimization starts to compensate the larger code size",
"tx": {
"from": "address:owner",
"to": "sc:contract",
"function": "mb_builder_benchmark",
"arguments": [
"0x01020304",
"22"
],
"gasLimit": "10,000,000",
"gasPrice": "0"
},
"expect": {
"out": "*",
"status": "",
"gas": "8778242"
}
},
{
"step": "scCall",
"id": "benchmark-mb-builder-basic",
"comment": "for many repeats, the cached version wins",
"tx": {
"from": "address:owner",
"to": "sc:contract",
"function": "mb_builder_benchmark",
"arguments": [
"0x01020304",
"10000"
],
"gasLimit": "10,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": "*",
"status": "",
"gas": "9934220582"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"name": "str-repeat",
"steps": [
{
"step": "setState",
"accounts": {
"address:owner": {
"nonce": "0",
"balance": "0"
},
"sc:contract": {
"code": "mxsc:../output/str-repeat-mb-builder-cached.mxsc.json"
}
}
},
{
"step": "scCall",
"id": "benchmark-mb-builder-cached-10",
"comment": "code is smaller, so basic wins here",
"tx": {
"from": "address:owner",
"to": "sc:contract",
"function": "mb_builder_benchmark",
"arguments": [
"0x01020304",
"10"
],
"gasLimit": "10,000,000",
"gasPrice": "0"
},
"expect": {
"out": "*",
"status": "",
"gas": "8834532"
}
},
{
"step": "scCall",
"id": "benchmark-mb-builder-cached-tipping-point",
"comment": "the caching optimization starts to compensate the larger code size",
"tx": {
"from": "address:owner",
"to": "sc:contract",
"function": "mb_builder_benchmark",
"arguments": [
"0x01020304",
"22"
],
"gasLimit": "10,000,000",
"gasPrice": "0"
},
"expect": {
"out": "*",
"status": "",
"gas": "8779332"
}
},
{
"step": "scCall",
"id": "benchmark-mb-builder-cached",
"comment": "for many repeats, the cached version wins",
"tx": {
"from": "address:owner",
"to": "sc:contract",
"function": "mb_builder_benchmark",
"arguments": [
"0x01020304",
"10000"
],
"gasLimit": "10,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": "*",
"status": "",
"gas": "9938367902"
}
}
]
}
11 changes: 11 additions & 0 deletions contracts/benchmarks/str-repeat/src/str_repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,15 @@ pub trait StrRepeat {
#[view(getByteArray)]
#[storage_mapper("byteArray")]
fn byte_array(&self) -> SingleValueMapper<Vec<u8>>;

#[view]
#[label("mb-builder")]
fn mb_builder_benchmark(&self, payload: u32, num_repeats: usize) -> ManagedBuffer {
let mut builder = ManagedBufferBuilder::default();
let payload_bytes = payload.to_be_bytes();
for _ in 0..num_repeats {
builder.append_bytes(&payload_bytes);
}
builder.into_managed_buffer()
}
}
12 changes: 12 additions & 0 deletions contracts/benchmarks/str-repeat/tests/scenario_go_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ fn world() -> ScenarioWorld {
ScenarioWorld::vm_go()
}

#[test]
#[ignore = "gas benchmark, too brittle to include permanently"]
fn mb_builder_basic_go() {
world().run("scenarios/mb_builder_basic.scen.json");
}

#[test]
#[ignore = "gas benchmark, too brittle to include permanently"]
fn mb_builder_cached_go() {
world().run("scenarios/mb_builder_cached.scen.json");
}

#[test]
fn str_repeat_go() {
world().run("scenarios/str_repeat.scen.json");
Expand Down
18 changes: 18 additions & 0 deletions contracts/benchmarks/str-repeat/tests/scenario_rs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,27 @@ fn world() -> ScenarioWorld {
"mxsc:output/str-repeat.mxsc.json",
str_repeat::ContractBuilder,
);
blockchain.register_contract(
"mxsc:output/str-repeat-mb-builder-basic.mxsc.json",
str_repeat::ContractBuilder,
);
blockchain.register_contract(
"mxsc:output/str-repeat-mb-builder-cached.mxsc.json",
str_repeat::ContractBuilder,
);
blockchain
}

#[test]
fn mb_builder_basic_rs() {
world().run("scenarios/mb_builder_basic.scen.json");
}

#[test]
fn mb_builder_cached_rs() {
world().run("scenarios/mb_builder_cached.scen.json");
}

#[test]
fn str_repeat_rs() {
world().run("scenarios/str_repeat.scen.json");
Expand Down
Loading

0 comments on commit ed8e28e

Please sign in to comment.