Skip to content

Commit

Permalink
Merge pull request #1450 from demergent-labs/stable_b_tree_map_perfor…
Browse files Browse the repository at this point in the history
…mance_tests

Stable b tree map performance tests
  • Loading branch information
lastmjs authored Dec 1, 2023
2 parents 69a0395 + f0337c7 commit dca3fa4
Show file tree
Hide file tree
Showing 30 changed files with 299 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:
"examples/robust_imports",
"examples/simple_erc20",
"examples/simple_user_accounts",
"examples/stable_json",
"examples/stable_b_tree_map_instruction_threshold",
"examples/stable_memory",
"examples/stable_structures",
"examples/timers",
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions examples/stable_b_tree_map_instruction_threshold/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"canisters": {
"stable_b_tree_map_instruction_threshold": {
"type": "custom",
"main": "src/index.ts",
"candid": "src/index.did",
"build": "npx azle stable_b_tree_map_instruction_threshold",
"wasm": ".azle/stable_b_tree_map_instruction_threshold/stable_b_tree_map_instruction_threshold.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/stable_b_tree_map_instruction_threshold",
"node_compatibility": true
}
}
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
service: () -> {
insertLargeRecord: (nat32) -> ();
insertMediumRecord: (nat32) -> ();
insertSmallRecord: (nat32) -> ();
valuesLargeRecord: (nat32) -> (vec record {id:text; age:nat; signature:vec nat8; internetIdentity:principal; username:text; mediumRecord:record {id:text; age:nat; internetIdentity:principal; username:text}; friends:vec text}) query;
valuesMediumRecord: (nat32) -> (vec record {id:text; age:nat; internetIdentity:principal; username:text}) query;
valuesSmallRecord: (nat32) -> (vec record {id:principal}) query;
}
111 changes: 111 additions & 0 deletions examples/stable_b_tree_map_instruction_threshold/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
blob,
Canister,
nat,
nat32,
Principal,
query,
Record,
StableBTreeMap,
stableJson,
text,
update,
Vec,
Void
} from 'azle';
import { v4 } from 'uuid';

const SmallRecord = Record({
id: Principal
});
type SmallRecord = typeof SmallRecord.tsType;

let smallRecordMap = StableBTreeMap<string, SmallRecord>(
stableJson,
stableJson,
0
);

const MediumRecord = Record({
id: text,
username: text,
age: nat,
internetIdentity: Principal
});
type MediumRecord = typeof MediumRecord.tsType;

let mediumRecordMap = StableBTreeMap<string, MediumRecord>(
stableJson,
stableJson,
1
);

const LargeRecord = Record({
id: text,
username: text,
age: nat,
internetIdentity: Principal,
signature: blob,
friends: Vec(text),
mediumRecord: MediumRecord
});
type LargeRecord = typeof LargeRecord.tsType;

let largeRecordMap = StableBTreeMap<string, LargeRecord>(
stableJson,
stableJson,
2
);

export default Canister({
insertSmallRecord: update([nat32], Void, (numToInsert) => {
for (let i = 0; i < numToInsert; i++) {
const id = v4();

smallRecordMap.insert(id, {
id: Principal.fromText('aaaaa-aa')
});
}
}),
valuesSmallRecord: query([nat32], Vec(SmallRecord), (numToReturn) => {
return smallRecordMap.values(0, numToReturn);
}),
insertMediumRecord: update([nat32], Void, (numToInsert) => {
for (let i = 0; i < numToInsert; i++) {
const id = v4();

mediumRecordMap.insert(id, {
id,
username: `lastmjs${i}`,
age: BigInt(i),
internetIdentity: Principal.fromText('aaaaa-aa')
});
}
}),
valuesMediumRecord: query([nat32], Vec(MediumRecord), (numToReturn) => {
return mediumRecordMap.values(0, numToReturn);
}),
insertLargeRecord: update([nat32], Void, (numToInsert) => {
for (let i = 0; i < numToInsert; i++) {
const id = v4();

largeRecordMap.insert(id, {
id,
username: `lastmjs${i}`,
age: BigInt(i),
internetIdentity: Principal.fromText('aaaaa-aa'),
signature: Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
friends: [v4(), v4(), v4(), v4()],
mediumRecord: {
id,
username: `lastmjs${i}`,
age: BigInt(i),
internetIdentity: Principal.fromText('aaaaa-aa')
}
});
}
}),
valuesLargeRecord: query([nat32], Vec(LargeRecord), (numToReturn) => {
return largeRecordMap.values(0, numToReturn);
})
});
20 changes: 20 additions & 0 deletions examples/stable_b_tree_map_instruction_threshold/test/pretest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { execSync } from 'child_process';

async function pretest() {
execSync(
`dfx canister uninstall-code stable_b_tree_map_instruction_threshold || true`,
{
stdio: 'inherit'
}
);

execSync(`dfx deploy stable_b_tree_map_instruction_threshold`, {
stdio: 'inherit'
});

execSync(`dfx generate stable_b_tree_map_instruction_threshold`, {
stdio: 'inherit'
});
}

pretest();
14 changes: 14 additions & 0 deletions examples/stable_b_tree_map_instruction_threshold/test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getCanisterId, runTests } from 'azle/test';
import { createActor } from './dfx_generated/stable_b_tree_map_instruction_threshold';
import { getTests } from './tests';

const stableBTreeMapInstructionThresholdCanister = createActor(
getCanisterId('stable_b_tree_map_instruction_threshold'),
{
agentOptions: {
host: 'http://127.0.0.1:8000'
}
}
);

runTests(getTests(stableBTreeMapInstructionThresholdCanister));
61 changes: 61 additions & 0 deletions examples/stable_b_tree_map_instruction_threshold/test/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { ActorSubclass } from '@dfinity/agent';
import { Test } from 'azle/test';
import { _SERVICE } from './dfx_generated/stable_b_tree_map_instruction_threshold/stable_b_tree_map_instruction_threshold.did';

export function getTests(
stableBTreeMapInstructionThresholdCanister: ActorSubclass<_SERVICE>
): Test[] {
return [
{
name: 'test SmallRecord',
test: async () => {
await stableBTreeMapInstructionThresholdCanister.insertSmallRecord(
10_000
);

const result =
await stableBTreeMapInstructionThresholdCanister.valuesSmallRecord(
6_000
);

return {
Ok: result.length === 6_000
};
}
},
{
name: 'test MediumRecord',
test: async () => {
await stableBTreeMapInstructionThresholdCanister.insertMediumRecord(
5_000
);

const result =
await stableBTreeMapInstructionThresholdCanister.valuesMediumRecord(
1_000
);

return {
Ok: result.length === 1_000
};
}
},
{
name: 'test LargeRecord',
test: async () => {
await stableBTreeMapInstructionThresholdCanister.insertLargeRecord(
2_000
);

const result =
await stableBTreeMapInstructionThresholdCanister.valuesLargeRecord(
500
);

return {
Ok: result.length === 500
};
}
}
];
}
File renamed without changes.
16 changes: 0 additions & 16 deletions examples/stable_json/dfx.json

This file was deleted.

4 changes: 0 additions & 4 deletions examples/stable_json/src/index.did

This file was deleted.

53 changes: 0 additions & 53 deletions examples/stable_json/src/index.ts

This file was deleted.

19 changes: 0 additions & 19 deletions examples/stable_json/test/pretest.ts

This file was deleted.

11 changes: 0 additions & 11 deletions examples/stable_json/test/test.ts

This file was deleted.

8 changes: 0 additions & 8 deletions examples/stable_json/test/tests.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ pub fn native_function<'a>(
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let memory_id: usize = args
let memory_id_string: String = args
.get(0)
.expect("stable_b_tree_map_contains_key argument 0 is undefined")
.to_js_value()?
.try_into()?;
let memory_id: u8 = memory_id_string.parse()?;

let key: Vec<u8> = args
.get(1)
Expand All @@ -25,8 +26,7 @@ pub fn native_function<'a>(
.with(|stable_b_tree_maps| {
let stable_b_tree_maps = stable_b_tree_maps.borrow();

stable_b_tree_maps[&(memory_id as u8)]
.contains_key(&AzleStableBTreeMapKey { bytes: key })
stable_b_tree_maps[&memory_id].contains_key(&AzleStableBTreeMapKey { bytes: key })
})
.into();

Expand Down
Loading

0 comments on commit dca3fa4

Please sign in to comment.