Browse Source

:white_check_mark: Adding CU measurement in tests

Danilo Guanabara 2 months ago
parent
commit
f66535056e

+ 9 - 14
Anchor.toml

@@ -10,30 +10,25 @@ world = "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n"
 [programs.localnet]
 bolt-component = "CmP2djJgABZ4cRokm4ndxuq6LerqpNHLBsaUv2XKEJua"
 bolt-system = "7X4EFsDJ5aYTcEjKzJ94rD8FRKgQeXC89fkpeTS4KaqP"
+component-large = "FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX"
+component-small = "9yBADAhoTWCkNRB6hbfpwUgPpxyJiF9uEiWVPR6k7A4y"
 position = "Fn1JzzEdyb55fsyduWS94mYHizGhJZuhvjX6DVvrmGbQ"
 system-apply-velocity = "6LHhFVwif6N9Po3jHtSmMVtPjF6zRfL3xMosSzcrQAS8"
 system-fly = "HT2YawJjkNmqWcLNfPAMvNsLdWwPvvvbKA5bpMw4eUpq"
 system-simple-movement = "FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA"
+system-with-few-components = "A3kNNSgmkTNA5V1qtnrbtNeqKrYHNxUMCTkqTDaQzE97"
+system-with-many-components = "Hi4sMEb3uXhWCiLyrF7t3Z384an7YZsTj774cabAAPQB"
 velocity = "CbHEFbSQdRN4Wnoby9r16umnJ1zWbULBHg4yqzGQonU1"
 
 [registry]
 url = "https://api.apr.dev"
 
 [provider]
-cluster = "Localnet"
+cluster = "localnet"
 wallet = "./tests/fixtures/provider.json"
 
 [workspace]
-members = [
-    "crates/programs/bolt-component",
-    "crates/programs/bolt-system",
-    "crates/programs/world",
-    "examples/component-position",
-    "examples/component-velocity",
-    "examples/system-apply-velocity",
-    "examples/system-fly",
-    "examples/system-simple-movement"
-]
+members = ["crates/programs/bolt-component", "crates/programs/bolt-system", "crates/programs/world", "examples/component-position", "examples/component-velocity", "examples/system-apply-velocity", "examples/system-fly", "examples/system-simple-movement", "examples/system-with-many-components", "examples/system-with-few-components", "examples/component-large", "examples/component-small"]
 
 [scripts]
 test = "tests/script.sh"
@@ -49,8 +44,8 @@ program = "tests/fixtures/delegation.so"
 upgradeable = false
 
 [[test.genesis]]
-address="KeyspM2ssCJbqUhQ4k7sveSiY4WjnYsrXkC8oDbwde5"
-program="tests/fixtures/session_keys.so"
+address = "KeyspM2ssCJbqUhQ4k7sveSiY4WjnYsrXkC8oDbwde5"
+program = "tests/fixtures/session_keys.so"
 upgradeable = false
 
 [test.validator]
@@ -76,4 +71,4 @@ filename = "tests/fixtures/vault1.json"
 
 [[test.validator.account]]
 address = "7JrkjmZPprHwtuvtuGTXp9hwfGYFAQLnLeFM52kqAgXg"
-filename = "tests/fixtures/vault2.json"
+filename = "tests/fixtures/vault2.json"

+ 32 - 0
Cargo.lock

@@ -1258,6 +1258,20 @@ dependencies = [
  "memchr",
 ]
 
+[[package]]
+name = "component-large"
+version = "0.2.4"
+dependencies = [
+ "bolt-lang",
+]
+
+[[package]]
+name = "component-small"
+version = "0.2.4"
+dependencies = [
+ "bolt-lang",
+]
+
 [[package]]
 name = "concurrent-queue"
 version = "2.5.0"
@@ -5903,6 +5917,24 @@ dependencies = [
  "serde",
 ]
 
+[[package]]
+name = "system-with-few-components"
+version = "0.2.4"
+dependencies = [
+ "bolt-lang",
+ "component-large",
+ "serde",
+]
+
+[[package]]
+name = "system-with-many-components"
+version = "0.2.4"
+dependencies = [
+ "bolt-lang",
+ "component-small",
+ "serde",
+]
+
 [[package]]
 name = "tar"
 version = "0.4.43"

+ 3 - 0
Cargo.toml

@@ -8,6 +8,7 @@ members = [
     "crates/programs/world",
     "crates/types",
     "examples/*",
+    "examples/system-with-many-components",
 ]
 
 [workspace.package]
@@ -34,6 +35,8 @@ bolt-utils = { path = "crates/bolt-lang/utils", version = "=0.2.4" }
 world = { path = "crates/programs/world", features = ["cpi"], version = "=0.2.4"}
 bolt-system = { path = "crates/programs/bolt-system", features = ["cpi"], version = "=0.2.4"}
 bolt-component = { path = "crates/programs/bolt-component", features = ["cpi"], version = "=0.2.4"}
+component-large = { path = "examples/component-large", features = ["cpi"], version = "=0.2.4"}
+component-small = { path = "examples/component-small", features = ["cpi"], version = "=0.2.4"}
 
 ## External crates
 session-keys       = { version = "^2", features = ["no-entrypoint"] }

+ 73 - 2
clients/typescript/test/framework.ts

@@ -13,6 +13,10 @@ import { type SystemSimpleMovement } from "../../../target/types/system_simple_m
 import { type SystemFly } from "../../../target/types/system_fly";
 import { type SystemApplyVelocity } from "../../../target/types/system_apply_velocity";
 import { Connection, Keypair, PublicKey } from "@solana/web3.js";
+import { SystemWithManyComponents } from "../../../target/types/system_with_many_components";
+import { Large } from "../../../target/types/large";
+import { Small } from "../../../target/types/small";
+import { SystemWithFewComponents } from "../../../target/types/system_with_few_components";
 
 export class Framework {
   provider: anchor.AnchorProvider;
@@ -23,7 +27,10 @@ export class Framework {
   systemSimpleMovement: anchor.Program<SystemSimpleMovement>;
   systemFly: anchor.Program<SystemFly>;
   systemApplyVelocity: anchor.Program<SystemApplyVelocity>;
-
+  systemWithManyComponents: anchor.Program<SystemWithManyComponents>;
+  systemWithFewComponents: anchor.Program<SystemWithFewComponents>;
+  componentLarge: anchor.Program<Large>;
+  componentSmall: anchor.Program<Small>;
   worldPda: PublicKey;
   worldId: BN;
 
@@ -37,8 +44,9 @@ export class Framework {
   acceleratedComponentPositionPda: PublicKey;
   componentPositionEntity1Pda: PublicKey;
   componentVelocityEntity1Pda: PublicKey;
-
+  componentLargeEntity1Pda: PublicKey;
   componentPositionEntity4Pda: PublicKey;
+  componentSmallEntity1Pda: PublicKey;
 
   constructor() {
     this.secondAuthority = Keypair.generate().publicKey;
@@ -48,6 +56,10 @@ export class Framework {
     this.systemSimpleMovement = anchor.workspace.SystemSimpleMovement;
     this.systemFly = anchor.workspace.SystemFly;
     this.systemApplyVelocity = anchor.workspace.SystemApplyVelocity;
+    this.systemWithManyComponents = anchor.workspace.SystemWithManyComponents;
+    this.systemWithFewComponents = anchor.workspace.SystemWithFewComponents;
+    this.componentLarge = anchor.workspace.Large;
+    this.componentSmall = anchor.workspace.Small;
 
     this.provider = anchor.AnchorProvider.local();
     anchor.setProvider(this.provider);
@@ -57,4 +69,63 @@ export class Framework {
       anchor.Wallet.local(),
     );
   }
+
+
+  consume(line: string): number {
+    let consumed = line.split(" consumed ")[1].split(" of ")[0];
+    return parseInt(consumed);
+  }
+
+  report(log: string[]) {
+    var cpi: number[] = [];
+    var worldReport: number = 0;
+    for (let index in log) {
+      let line = log[index];
+      if (line.includes(" consumed ")) {        
+        if (!line.includes("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n")) {
+          cpi.push(this.consume(line));
+        } else {
+          worldReport = this.consume(line);
+        }
+      }
+    }
+    let total = cpi.reduce((a, b) => a + b, 0);
+    let numberOfInstructions = cpi.length;
+    console.log(`Total CPI Consumed: ${total}`);
+    console.log(`Number of Instructions: ${numberOfInstructions}`);
+    console.log(`World Report: ${worldReport}`);
+  }
 }
+
+// example input
+// [
+//   'Program WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n invoke [1]',
+//   'Program log: Instruction: Apply',
+//   'Program A3kNNSgmkTNA5V1qtnrbtNeqKrYHNxUMCTkqTDaQzE97 invoke [2]',
+//   'Program log: Instruction: BoltExecute',
+//   'Program A3kNNSgmkTNA5V1qtnrbtNeqKrYHNxUMCTkqTDaQzE97 consumed 3679 of 191671 compute units',
+//   'Program return: A3kNNSgmkTNA5V1qtnrbtNeqKrYHNxUMCTkqTDaQzE97 BQAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAei0UnvLCu8bsxLLo92w4UVq1z4LrLIGvtTpkos13GLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB6LRSe8sK7xuzEsuj3bDhRWrXPgussga+1OmSizXcYtAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHotFJ7ywrvG7MSy6PdsOFFatc+C6yyBr7U6ZKLNdxi0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAei0UnvLCu8bsxLLo92w4UVq1z4LrLIGvtTpkos13GLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB6LRSe8sK7xuzEsuj3bDhRWrXPgussga+1OmSizXcYs=',
+//   'Program A3kNNSgmkTNA5V1qtnrbtNeqKrYHNxUMCTkqTDaQzE97 success',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX invoke [2]',
+//   'Program log: Instruction: Update',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX consumed 4732 of 183865 compute units',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX success',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX invoke [2]',
+//   'Program log: Instruction: Update',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX consumed 4732 of 176143 compute units',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX success',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX invoke [2]',
+//   'Program log: Instruction: Update',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX consumed 4732 of 168421 compute units',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX success',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX invoke [2]',
+//   'Program log: Instruction: Update',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX consumed 4732 of 160699 compute units',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX success',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX invoke [2]',
+//   'Program log: Instruction: Update',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX consumed 4732 of 152977 compute units',
+//   'Program FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX success',
+//   'Program WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n consumed 52251 of 200000 compute units',
+//   'Program WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n success'
+// ]

+ 75 - 5
clients/typescript/test/intermediate-level/ecs.ts

@@ -253,11 +253,13 @@ export function ecs(framework: Framework) {
         applySystem.transaction,
       );
 
-      let transactionResponse =
-        await framework.provider.connection.getTransaction(signature, {
-          commitment: "confirmed",
-        });
-      console.log(transactionResponse?.meta?.logMessages); // Reference CU is 27771
+      let transactionResponse: any;
+      do {
+        transactionResponse =
+          await framework.provider.connection.getTransaction(signature, {
+            commitment: "confirmed",
+          });
+      } while (transactionResponse?.meta?.logMessages === undefined);
 
       const position =
         await framework.exampleComponentPosition.account.position.fetch(
@@ -268,6 +270,74 @@ export function ecs(framework: Framework) {
       expect(position.z.toNumber()).to.equal(300);
     });
 
+    it("Initialize Large Component on Entity 1", async () => {
+      const initializeComponent = await InitializeComponent({
+        payer: framework.provider.wallet.publicKey,
+        entity: framework.entity1Pda,
+        componentId: framework.componentLarge.programId,
+      });
+      await framework.provider.sendAndConfirm(initializeComponent.transaction);
+      framework.componentLargeEntity1Pda = initializeComponent.componentPda; // Saved for later
+    });
+
+    it("Initialize Small Component on Entity 1", async () => {
+      const initializeComponent = await InitializeComponent({
+        payer: framework.provider.wallet.publicKey,
+        entity: framework.entity1Pda,
+        componentId: framework.componentSmall.programId,
+      });
+      await framework.provider.sendAndConfirm(initializeComponent.transaction);
+      framework.componentSmallEntity1Pda = initializeComponent.componentPda; // Saved for later
+    });
+
+    it("Apply System With Few Components on Entity 1", async () => {
+      const applySystem = await ApplySystem({
+        authority: framework.provider.wallet.publicKey,
+        systemId: framework.systemWithFewComponents.programId,
+        world: framework.worldPda,
+        entities: new Array(5).fill(0).map(() => ({
+          entity: framework.entity1Pda,
+          components: [{ componentId: framework.componentLarge.programId }],
+        })),
+      });
+      let signature = await framework.provider.sendAndConfirm(
+        applySystem.transaction,
+      );
+
+      let transactionResponse: any;
+      do {
+        transactionResponse =
+        await framework.provider.connection.getTransaction(signature, {
+          commitment: "confirmed",
+        });
+      } while (transactionResponse?.meta?.logMessages === undefined);
+      framework.report(transactionResponse?.meta?.logMessages);
+    });
+
+    it("Apply System With Many Components on Entity 1", async () => {
+      const applySystem = await ApplySystem({
+        authority: framework.provider.wallet.publicKey,
+        systemId: framework.systemWithManyComponents.programId,
+        world: framework.worldPda,
+        entities: new Array(10).fill(0).map(() => ({
+          entity: framework.entity1Pda,
+          components: [{ componentId: framework.componentSmall.programId }],
+        })),
+      });
+      let signature = await framework.provider.sendAndConfirm(
+        applySystem.transaction,
+      );
+
+      let transactionResponse: any;
+      do {
+        transactionResponse =
+        await framework.provider.connection.getTransaction(signature, {
+          commitment: "confirmed",
+        });
+      } while (transactionResponse?.meta?.logMessages === undefined);
+      framework.report(transactionResponse?.meta?.logMessages);
+    });
+
     it("Apply Fly System on Entity 4", async () => {
       const applySystem = await ApplySystem({
         authority: framework.provider.wallet.publicKey,

+ 1 - 1
clients/typescript/test/low-level/permissioning/world.ts

@@ -103,7 +103,7 @@ export function world(framework) {
         .instruction();
       let transaction = new anchor.web3.Transaction().add(instruction);
       await framework.provider.sendAndConfirm(transaction, [], {
-        skipPreflight: true
+        skipPreflight: true,
       });
 
       // Get World and check permissionless and systems

+ 22 - 0
examples/component-large/Cargo.toml

@@ -0,0 +1,22 @@
+[package]
+name = "component-large"
+version.workspace = true
+edition.workspace = true
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "component_large"
+
+[features]
+no-entrypoint = []
+no-idl = []
+no-log-ix-name = []
+cpi = ["no-entrypoint"]
+default = []
+idl-build = ["bolt-lang/idl-build"]
+anchor-debug = ["bolt-lang/anchor-debug"]
+custom-heap = []
+custom-panic = []
+
+[dependencies]
+bolt-lang.workspace = true

+ 2 - 0
examples/component-large/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 17 - 0
examples/component-large/src/lib.rs

@@ -0,0 +1,17 @@
+use bolt_lang::*;
+
+declare_id!("FJjiJoz799Q6NqYffXbsFFj1pBmwsQZgcoizCfWvM5HX");
+
+#[component]
+pub struct Large {
+    pub value: [u8; 32],
+}
+
+impl Default for Large {
+    fn default() -> Self {
+        Self {
+            bolt_metadata: BoltMetadata::default(),
+            value: [0; 32],
+        }
+    }
+}

+ 23 - 0
examples/component-small/Cargo.toml

@@ -0,0 +1,23 @@
+[package]
+name = "component-small"
+version = "0.2.4"
+description = "Created with Bolt"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "component_small"
+
+[features]
+no-entrypoint = []
+no-idl = []
+no-log-ix-name = []
+cpi = ["no-entrypoint"]
+default = []
+idl-build = ["bolt-lang/idl-build"]
+anchor-debug = ["bolt-lang/anchor-debug"]
+custom-heap = []
+custom-panic = []
+
+[dependencies]
+bolt-lang.workspace = true

+ 2 - 0
examples/component-small/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 9 - 0
examples/component-small/src/lib.rs

@@ -0,0 +1,9 @@
+use bolt_lang::*;
+
+declare_id!("9yBADAhoTWCkNRB6hbfpwUgPpxyJiF9uEiWVPR6k7A4y");
+
+#[component]
+#[derive(Default)]
+pub struct Small {
+  // Just the BoltMetadata which is 32 bytes
+} 

+ 26 - 0
examples/system-with-few-components/Cargo.toml

@@ -0,0 +1,26 @@
+[package]
+name = "system-with-few-components"
+version = "0.2.4"
+description = "Created with Bolt"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "system_with_few_components"
+
+[features]
+no-entrypoint = []
+no-idl = []
+no-log-ix-name = []
+cpi = ["no-entrypoint"]
+default = []
+idl-build = ["bolt-lang/idl-build"]
+anchor-debug = ["bolt-lang/anchor-debug"]
+custom-heap = []
+custom-panic = []
+
+
+[dependencies]
+bolt-lang.workspace = true
+serde.workspace = true 
+component-large.workspace = true

+ 2 - 0
examples/system-with-few-components/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 23 - 0
examples/system-with-few-components/src/lib.rs

@@ -0,0 +1,23 @@
+use bolt_lang::*;
+use component_large::Large;
+
+declare_id!("A3kNNSgmkTNA5V1qtnrbtNeqKrYHNxUMCTkqTDaQzE97");
+
+
+#[system]
+pub mod system_with_few_components {
+
+    pub fn execute(ctx: Context<Components>, _args_p: Vec<u8>) -> Result<Components> {
+        Ok(ctx.accounts)
+    }
+
+    #[system_input]
+    pub struct Components {
+        pub large1: Large,
+        pub large2: Large,
+        pub large3: Large,
+        pub large4: Large,
+        pub large5: Large,
+    }
+
+}

+ 25 - 0
examples/system-with-many-components/Cargo.toml

@@ -0,0 +1,25 @@
+[package]
+name = "system-with-many-components"
+version.workspace = true
+edition.workspace = true
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "system_with_many_components"
+
+[features]
+no-entrypoint = []
+no-idl = []
+no-log-ix-name = []
+cpi = ["no-entrypoint"]
+default = []
+idl-build = ["bolt-lang/idl-build"]
+anchor-debug = ["bolt-lang/anchor-debug"]
+custom-heap = []
+custom-panic = []
+
+
+[dependencies]
+bolt-lang.workspace = true
+component-small.workspace = true
+serde.workspace = true

+ 2 - 0
examples/system-with-many-components/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 27 - 0
examples/system-with-many-components/src/lib.rs

@@ -0,0 +1,27 @@
+use bolt_lang::*;
+use component_small::Small;
+
+declare_id!("Hi4sMEb3uXhWCiLyrF7t3Z384an7YZsTj774cabAAPQB");
+
+#[system]
+pub mod system_with_many_components {
+
+    pub fn execute(ctx: Context<Components>, _args_p: Vec<u8>) -> Result<Components> {
+        Ok(ctx.accounts)
+    }
+
+    #[system_input]
+    pub struct Components {
+        pub small1: Small,
+        pub small2: Small,
+        pub small3: Small,
+        pub small4: Small,
+        pub small5: Small,
+        pub small6: Small,
+        pub small7: Small,
+        pub small8: Small,
+        pub small9: Small,
+        pub small10: Small,
+    }
+
+}