Skip to content

Commit

Permalink
long struct bench
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Zając committed Oct 24, 2023
1 parent 86be0d9 commit 541ac8b
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 53 deletions.
98 changes: 74 additions & 24 deletions benches.txt
Original file line number Diff line number Diff line change
@@ -1,50 +1,100 @@
bench empty: 0
case db get: 754572
case db set: 338782
bench empty: 0
case db get: 754172
case db get: 754172
case db set: 338382
case db set: 338382
case init: 770
case init: 770
case serialize: 5810
case values: 38520
char get macro: 11091794
chars db get: 10139064
chars db set: 5050844
char set call: 6139084
chars init: 1040
chars serialize: 39120
chars values: 42270
case serialize: 4610
case serialize: 4610
case values: 36120
case values: 36120
char get macro: 11009694
char get macro: 11009694
chars db get: 10136764
chars db get: 10136764
chars db set: 5028344
chars db set: 5028344
char set call: 6048284
char set call: 6048284
chars init: 770
chars init: 770
chars serialize: 18920
chars serialize: 18920
chars values: 22070
chars values: 22070
db del arr: 14240
db get arr: 1027288
db get half arr: 726058
dbi scan arr 1: 452874
dbi scan arr 2: 805378
dbi set arr 1st: 482814
dbi set arr 2nd: 482634
db set arr: 855808
foo get macro: 1039434
db del arr: 14240
db get arr: 1026188
db get arr: 1026188
db get half arr: 725458
db get half arr: 725458
dbi scan arr 1: 452574
dbi scan arr 1: 452574
dbi scan arr 2: 804778
dbi scan arr 2: 804778
dbi set arr 1st: 482514
dbi set arr 1st: 482514
dbi set arr 2nd: 482334
dbi set arr 2nd: 482334
db set arr: 854708
db set arr: 854708
foo get macro: 1021934
foo get macro: 1021934
foo init: 770
foo init: 770
foo serialize: 3620
foo set call: 835314
foo values: 13560
foo serialize: 2220
foo serialize: 2220
foo set call: 813314
foo set call: 813314
foo values: 12160
foo values: 12160
idx create 1000: 189333070
idx create 1000: 189333070
idx create 1st: 78280
idx create 1st: 78280
idx dlt !1000 0: 164350
idx dlt !1000 0: 164350
idx dlt 1000: 110300
idx dlt 1000: 110300
idx dlt !1000 >: 261360
idx dlt !1000 >: 261360
idx dlt last: 284480
idx dlt last: 284480
idx dlt !last: 285040
idx dlt !last: 285040
idx empty: 185890
idx empty: 185890
idx exists 1000: 14240
idx exists 1000: 14240
idx exists chk: 40520
idx exists chk: 40520
idx query 1000: 40716000
idx query 1000: 40716000
idx query 2nd: 187780
idx query 2nd: 187780
idx query one: 119120
idx query one: 119120
idx query two: 159580
idx query two: 159580
native prep: 11600
native prep: 11600
native prep of: 11600
native prep of: 11600
native read: 17880
native read: 17880
native read of: 17880
native read of: 17880
native write: 17260
native write: 17260
native writ of: 17260
native writ of: 17260
storage get: 36860
storage get: 36860
storage get mny: 297424
storage get mny: 297124
storage get mny: 297124
storage set: 36600
storage set: 36600
storage set mny: 284284
storage set mny: 283984
storage set mny: 283984
13 changes: 13 additions & 0 deletions crates/dojo-core/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "dojo"
version = "0.3.1"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.1"
99 changes: 71 additions & 28 deletions crates/dojo-core/src/benchmarks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,9 @@ use starknet::{contract_address_const, ContractAddress, ClassHash, get_caller_ad

use dojo::database;
use dojo::database::{storage, index};
use dojo::packing::{shl, shr};
use dojo::model::Model;
use dojo::world_test::Foo;

const GAS_OFFSET: felt252 = 0x1_000000_000000_000000_000000_000000; // 15 bajtów


fn end(start: u128, name: felt252) {
let gas_after = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let mut name: u256 = name.into();

// overwriting zeros with spaces
let mut char = 0;
loop {
if char == 15 {
break;
}
// if given byte is zero
if shl(0xff, 8 * char) & name == 0 {
name = name | shl(0x20, 8 * char); // set space
}
char += 1;
};

let name: felt252 = (name % GAS_OFFSET.into()).try_into().unwrap();
let used_gas = (start - gas_after - 1770).into() * GAS_OFFSET;
(used_gas + name).print();
}
use dojo::test_utils::end;


#[test]
Expand Down Expand Up @@ -334,6 +308,76 @@ fn bench_simple_struct() {
assert(serialized.at(1) == values.at(1), 'serialized differ at 1');
}

#[derive(Model, Copy, Drop, Serde)]
struct PositionWithQuaterions {
#[key]
id: felt252,
x: felt252,
y: felt252,
z: felt252,
a: felt252,
b: felt252,
c: felt252,
d: felt252,
}

#[test]
#[available_gas(1000000000)]
fn test_struct_with_many_fields() {
let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();

let mut pos = PositionWithQuaterions {
id: 0x123456789abcdef,
x: 0x123456789abcdef,
y: 0x123456789abcdef,
z: 0x123456789abcdef,
a: 0x123456789abcdef,
b: 0x123456789abcdef,
c: 0x123456789abcdef,
d: 0x123456789abcdef,
};
end(gas, 'pos init');

let gas = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let mut serialized = ArrayTrait::new();
serde::Serde::serialize(@pos.x, ref serialized);
serde::Serde::serialize(@pos.y, ref serialized);
serde::Serde::serialize(@pos.z, ref serialized);
serde::Serde::serialize(@pos.a, ref serialized);
serde::Serde::serialize(@pos.b, ref serialized);
serde::Serde::serialize(@pos.c, ref serialized);
serde::Serde::serialize(@pos.d, ref serialized);
let serialized = array::ArrayTrait::span(@serialized);
end(gas, 'pos serialize');

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

assert(serialized.len() == values.len(), 'serialized not equal');
let mut idx = 0;
loop {
if idx == serialized.len() {
break;
}
assert(serialized.at(idx) == values.at(idx), 'serialized differ');
idx += 1;
};

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

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


#[derive(Introspect, Copy, Drop, Serde)]
struct Sword {
Expand Down Expand Up @@ -522,5 +566,4 @@ fn bench_complex_struct() {
gas::withdraw_gas().unwrap();
database::get('chars', '42', 0, char.packed_size(), char.layout());
end(gas, 'chars db get');

}
35 changes: 34 additions & 1 deletion crates/dojo-core/src/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use array::{ArrayTrait, SpanTrait};
use traits::TryInto;
use option::OptionTrait;
use core::{result::ResultTrait, traits::Into};

use debug::PrintTrait;

use dojo::executor::executor;
use dojo::world::{world, IWorldDispatcher, IWorldDispatcherTrait};
use dojo::packing::{shl, shr};

/// Deploy classhash with calldata for constructor
///
Expand Down Expand Up @@ -68,3 +69,35 @@ fn spawn_test_world(models: Array<felt252>) -> IWorldDispatcher {

world
}


const GAS_OFFSET: felt252 = 0x1_000000_000000_000000_000000_000000; // 15 bajtów

/// Measures gas used after previous measurement and prints it
///
/// # Arguments
///
/// * `start` - gas before measurement
/// * `name` - name of test, at most 15 bytes, will be padded with spaces
fn end(start: u128, name: felt252) {
let gas_after = testing::get_available_gas();
gas::withdraw_gas().unwrap();
let mut name: u256 = name.into();

// overwriting zeros with spaces
let mut char = 0;
loop {
if char == 15 {
break;
}
// if given byte is zero
if shl(0xff, 8 * char) & name == 0 {
name = name | shl(0x20, 8 * char); // set space
}
char += 1;
};

let name: felt252 = (name % GAS_OFFSET.into()).try_into().unwrap();
let used_gas = (start - gas_after - 1770).into() * GAS_OFFSET;
(used_gas + name).print();
}
26 changes: 26 additions & 0 deletions crates/dojo-defi/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "cubit"
version = "1.2.0"
source = "git+https://github.com/influenceth/cubit#2ccb2536dffa3f15ebd38b755c1be65fde1eab0c"

[[package]]
name = "dojo"
version = "0.3.1"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_defi"
version = "0.3.1"
dependencies = [
"cubit",
"dojo",
]

[[package]]
name = "dojo_plugin"
version = "0.3.1"
20 changes: 20 additions & 0 deletions crates/dojo-erc/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "dojo"
version = "0.3.1"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_erc"
version = "0.3.1"
dependencies = [
"dojo",
]

[[package]]
name = "dojo_plugin"
version = "0.3.1"
20 changes: 20 additions & 0 deletions crates/dojo-primitives/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "dojo"
version = "0.3.1"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.1"

[[package]]
name = "dojo_primitives"
version = "0.3.1"
dependencies = [
"dojo",
]

0 comments on commit 541ac8b

Please sign in to comment.