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

[dojo-core]: update gas logs #1877

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
195 changes: 78 additions & 117 deletions crates/dojo-core/src/benchmarks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,28 @@ use dojo::database;
use dojo::database::storage;
use dojo::model::Model;
use dojo::world_test::Foo;
use dojo::test_utils::end;
use dojo::test_utils::GasCounterImpl;


#[test]
#[available_gas(1000000000)]
fn bench_reference_offset() {
let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
end(gas, 'bench empty');
let gas = GasCounterImpl::start();
gas.end("bench empty");
}

#[test]
#[available_gas(1000000000)]
fn bench_storage_single() {
let keys = array!['database_test', '42'].span();

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
storage::set(0, keys, 420);
end(gas, 'storage set');
gas.end("storage set");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let res = storage::get(0, keys);
end(gas, 'storage get');
gas.end("storage get");

assert(res == 420, 'values differ');
}
Expand All @@ -46,15 +43,13 @@ fn bench_storage_many() {
let values = array![1, 2].span();
let layout = array![251, 251].span();

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
storage::set_many(0, keys, values, layout).unwrap();
end(gas, 'storage set mny');
gas.end("storage set_many");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let res = storage::get_many(0, keys, layout).unwrap();
end(gas, 'storage get mny');
gas.end("storage get_many");

assert(res.len() == 2, 'wrong number of values');
assert(*res.at(0) == *values.at(0), 'value not set');
Expand All @@ -64,45 +59,39 @@ fn bench_storage_many() {
#[test]
#[available_gas(1000000000)]
fn bench_native_storage() {
let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let keys = array![0x1337].span();
let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys));
let address = starknet::storage_address_from_base(base);
end(gas, 'native prep');
gas.end("native prep");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
starknet::storage_write_syscall(0, address, 42).unwrap_syscall();
end(gas, 'native write');
gas.end("native write");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let value = starknet::storage_read_syscall(0, address).unwrap_syscall();
end(gas, 'native read');
gas.end("native read");

assert(value == 42, 'read invalid');
}

#[test]
#[available_gas(1000000000)]
fn bench_native_storage_offset() {
let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let keys = array![0x1337].span();
let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys));
let address = starknet::storage_address_from_base_and_offset(base, 42);
end(gas, 'native prep of');
gas.end("native prep of");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
starknet::storage_write_syscall(0, address, 42).unwrap_syscall();
end(gas, 'native writ of');
gas.end("native writ of");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let value = starknet::storage_read_syscall(0, address).unwrap_syscall();
end(gas, 'native read of');
gas.end("native read of");

assert(value == 42, 'read invalid');
}
Expand All @@ -129,15 +118,13 @@ fn bench_database_array() {
i += 1;
};

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
database::set('table', 'key', values.span(), layout.span());
end(gas, 'db set arr');
gas.end( "db set arr");
glihm marked this conversation as resolved.
Show resolved Hide resolved

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let res = database::get('table', 'key', layout.span());
end(gas, 'db get arr');
gas.end("db get arr");

let mut i = 0;
loop {
Expand All @@ -155,27 +142,20 @@ fn bench_database_array() {
fn bench_simple_struct() {
let caller = starknet::contract_address_const::<0x42>();

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let mut foo = Foo {
caller,
a: 0x123456789abcdef,
b: 0x123456789abcdef,
};
end(gas, 'foo init');
let gas = GasCounterImpl::start();
let mut foo = Foo { caller, a: 0x123456789abcdef, b: 0x123456789abcdef, };
gas.end("foo init");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let mut serialized = ArrayTrait::new();
serde::Serde::serialize(@foo.a, ref serialized);
serde::Serde::serialize(@foo.b, ref serialized);
let serialized = array::ArrayTrait::span(@serialized);
end(gas, 'foo serialize');
gas.end("foo serialize");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let values: Span<felt252> = foo.values();
end(gas, 'foo values');
gas.end("foo values");

assert(serialized.len() == 2, 'serialized wrong length');
assert(values.len() == 2, 'value wrong length');
Expand All @@ -199,8 +179,7 @@ struct PositionWithQuaterions {
#[test]
#[available_gas(1000000000)]
fn test_struct_with_many_fields() {
let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();

let mut pos = PositionWithQuaterions {
id: 0x123456789abcdef,
Expand All @@ -212,10 +191,9 @@ fn test_struct_with_many_fields() {
c: 0x123456789abcdef,
d: 0x123456789abcdef,
};
end(gas, 'pos init');
gas.end( "pos init");
glihm marked this conversation as resolved.
Show resolved Hide resolved

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let mut serialized = ArrayTrait::new();
serde::Serde::serialize(@pos.x, ref serialized);
serde::Serde::serialize(@pos.y, ref serialized);
Expand All @@ -225,12 +203,11 @@ fn test_struct_with_many_fields() {
serde::Serde::serialize(@pos.c, ref serialized);
serde::Serde::serialize(@pos.d, ref serialized);
let serialized = array::ArrayTrait::span(@serialized);
end(gas, 'pos serialize');
gas.end("pos serialize");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let values: Span<felt252> = pos.values();
end(gas, 'pos values');
gas.end("pos values");

assert(serialized.len() == values.len(), 'serialized not equal');
let mut idx = 0;
Expand All @@ -242,15 +219,13 @@ fn test_struct_with_many_fields() {
idx += 1;
};

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
database::set('positions', '42', pos.values(), pos.layout());
end(gas, 'pos db set');
gas.end("pos db set");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
database::get('positions', '42', pos.layout());
end(gas, 'pos db get');
gas.end("pos db get");
}


Expand All @@ -273,34 +248,27 @@ struct Case {
#[available_gas(1000000000)]
fn bench_nested_struct() {
let caller = starknet::contract_address_const::<0x42>();
let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();


let gas = GasCounterImpl::start();
let mut case = Case {
owner: caller,
sword: Sword {
swordsmith: caller,
damage: 0x12345678,
},
material: 'wooden',
owner: caller, sword: Sword { swordsmith: caller, damage: 0x12345678, }, material: 'wooden',
};
end(gas, 'case init');
gas.end( "case init");
glihm marked this conversation as resolved.
Show resolved Hide resolved

// ????
glihm marked this conversation as resolved.
Show resolved Hide resolved
let _gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();


let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let mut serialized = ArrayTrait::new();
serde::Serde::serialize(@case.sword, ref serialized);
serde::Serde::serialize(@case.material, ref serialized);
let serialized = array::ArrayTrait::span(@serialized);
end(gas, 'case serialize');
gas.end( "case serialize");
glihm marked this conversation as resolved.
Show resolved Hide resolved

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let values: Span<felt252> = case.values();
end(gas, 'case values');
gas.end("case values");

assert(serialized.len() == values.len(), 'serialized not equal');
let mut idx = 0;
Expand All @@ -312,15 +280,13 @@ fn bench_nested_struct() {
idx += 1;
};

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
database::set('cases', '42', values, case.layout());
end(gas, 'case db set');
gas.end( "case db set");
glihm marked this conversation as resolved.
Show resolved Hide resolved

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
database::get('cases', '42', case.layout());
end(gas, 'case db get');
gas.end( "case db get");
glihm marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Model, Copy, Drop, Serde)]
Expand Down Expand Up @@ -366,8 +332,7 @@ enum Weapon {
#[test]
#[available_gas(1000000000)]
fn bench_complex_struct() {
let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();

let char = Character {
caller: starknet::contract_address_const::<0x42>(),
Expand All @@ -391,35 +356,33 @@ fn bench_complex_struct() {
finished: true,
romances: 0x1234,
},
weapon: Weapon::DualWield((
Sword {
swordsmith: starknet::contract_address_const::<0x69>(),
damage: 0x12345678,
},
Sword {
swordsmith: starknet::contract_address_const::<0x69>(),
damage: 0x12345678,
}
)),
weapon: Weapon::DualWield(
(
Sword {
swordsmith: starknet::contract_address_const::<0x69>(), damage: 0x12345678,
},
Sword {
swordsmith: starknet::contract_address_const::<0x69>(), damage: 0x12345678,
}
)
),
gold: 0x12345678,
};
end(gas, 'chars init');
gas.end("chars init");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let mut serialized = ArrayTrait::new();
serde::Serde::serialize(@char.heigth, ref serialized);
serde::Serde::serialize(@char.abilities, ref serialized);
serde::Serde::serialize(@char.stats, ref serialized);
serde::Serde::serialize(@char.weapon, ref serialized);
serde::Serde::serialize(@char.gold, ref serialized);
let serialized = array::ArrayTrait::span(@serialized);
end(gas, 'chars serialize');
gas.end("chars serialize");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
let values: Span<felt252> = char.values();
end(gas, 'chars values');
gas.end("chars values");

assert(serialized.len() == values.len(), 'serialized not equal');

Expand All @@ -432,13 +395,11 @@ fn bench_complex_struct() {
idx += 1;
};

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
database::set('chars', '42', char.values(), char.layout());
end(gas, 'chars db set');
gas.end("chars db set");

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let gas = GasCounterImpl::start();
database::get('chars', '42', char.layout());
end(gas, 'chars db get');
gas.end("chars db get");
}
Loading
Loading