Skip to content

Conversation

@chungquantin
Copy link
Contributor

@chungquantin chungquantin commented Feb 12, 2025

Screenshot 2025-02-18 at 21 18 09

Scenarios

Context: The pop bench pallet command is run at the root directory of the parachain project

Stage 1: Locating and building a runtime binary

  • Scenario 1: If runtime_path (Path to the runtime, hard-coded to "runtime" folder) is not a Rust project, list all directories in the runtime_path (similar to pop-node which has multiple runtimes inside the runtime folder). After user select the runtime, we check if the runtime binary exists or not. If not, we build the runtime with runtime-benchmarks feature.
list all directories -> select runtime -> check if binary exists -> build runtime with `runtime-benchmarks` feature.
  • Scenario 2: If runtime_path is a Rust project, located inside the runtime folder, which means it is a runtime codebase (Note: There could be a chance that this is a workspace?). We will throw No runtime found if the workspace is not valid. We check if the runtime binary exists or not. If not, we build the runtime with runtime-benchmarks feature.
check if binary exists -> build runtime with `runtime-benchmarks` feature.
  • Scenario 3: If no runtime found, we throws No runtime found. (See diagram).

Stage 2: Select genesis builder policy and input the preset.

After the runtime binary is ready, we prompts user to select the genesis builder (There are two options currently: none and runtime, more details can be found here). As we only support the runtime, no chain spec is provided so other genesis builder policies are not valid.

By calling sc-chain-spec::GenesisConfigBuilderRuntimeCaller get_preset_names method, we list all genesis builder presets and expect the user to select one of them. If there is no builder preset found, the CLI throws No preset found (can be reproduced with pop-node).

  • If --runtime= is provided, we don't locate the runtime.
  • If --genesis-builder= is provided, we don't prompt user to select the genesis builder policy.
  • If --genesis-config-preset= is provided, we don't prompt user to input the genesis config preset.

How to test the PR?

Stage 1 | Scenario 1: runtime folder is the runtime.

  • Go to ./base-parachain or any parachain template.
  • Run pop bench pallet --pallet=pallet_timestamp --extrinsic= (No runtime provided)

Stage 1 | Scenario 2: runtime contains multiple runtime folders.

  • Go to ./pop-node or any parachain that has multiple runtimes
  • Run pop bench pallet --pallet=pallet_timestamp --extrinsic= (No runtime provided)

Stage 2

genesis-builder = runtime with available presets.

  • Go to ./base-parachain or any parachain template.
  • Run pop bench pallet --pallet=pallet_timestamp --extrinsic= (No runtime provided)
  • You will be prompted to select the preset.

image

genesis-builder = runtime with unavailable presets.

  • Go to ./pop-node or any parachain template.
  • Run pop bench pallet --pallet=pallet_timestamp --extrinsic= (No runtime provided)
  • Err thrown because there is no available genesis builder presets.
Screenshot 2025-02-21 at 09 47 01

Changelog

  • Select runtime, genesis builder policy and genesis config preset.
  • Add new method build_project to build any Rust projects with features and target, required for the tests of this PR. The method is shared with build_parachain.
  • Add new method build_binary_path to locate the binary path of the provided Rust project. The method is shared with binary_path method.
  • Implement new feature to automatically locate existing <runtime-name>.wasm to benchmark. The path of the runtime WASM blob follows:
./target/<PROFILE>/wbuild/<name-of-runtime>/<name_of_runtime>.wasm
  • Add unit tests for new methods. To test the build_project method for the runtime, we build the mock project with wasm32-unknown-unknown as target and locates the .wasm file.

[sc-2931]

@codecov
Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 75.90674% with 93 lines in your changes missing coverage. Please review.

Please upload report for BASE (chungquantin/feat-pop_bench@5a33cb5). Learn more about missing BASE report.

Files with missing lines Patch % Lines
crates/pop-cli/src/commands/bench/mod.rs 75.90% 30 Missing and 30 partials ⚠️
crates/pop-parachains/src/build.rs 75.55% 4 Missing and 18 partials ⚠️
crates/pop-parachains/src/bench.rs 76.59% 1 Missing and 10 partials ⚠️
@@                      Coverage Diff                       @@
##             chungquantin/feat-pop_bench     #411   +/-   ##
==============================================================
  Coverage                               ?   75.35%           
==============================================================
  Files                                  ?       67           
  Lines                                  ?    14399           
  Branches                               ?    14399           
==============================================================
  Hits                                   ?    10850           
  Misses                                 ?     2144           
  Partials                               ?     1405           
Files with missing lines Coverage Δ
crates/pop-parachains/src/bench.rs 78.00% <76.59%> (ø)
crates/pop-parachains/src/build.rs 86.53% <75.55%> (ø)
crates/pop-cli/src/commands/bench/mod.rs 76.33% <75.90%> (ø)

@chungquantin chungquantin changed the title feat: auto detect wasm blob and build runtime feat: benchmark existing <runtime-name>.wasm or trigger build if needed Feb 13, 2025
@chungquantin chungquantin added the ready-for-final-review The PR is ready for final review label Feb 13, 2025
@chungquantin chungquantin self-assigned this Feb 13, 2025
Copy link
Collaborator

@AlexD10S AlexD10S left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks very clean and well-structured, great job!

However, I encountered a couple of issues during manual testing:

  • If the node has already been built without the --runtime-benchmarks feature, it fails to build again because it detects the existing runtime. It throws an error (from benchmarking-cli), yet still displays a success message:
⚙  Benchmarking and generating weight file....
│  
[2025-02-15T07:41:13Z INFO  pallet_collator_selection::pallet] assembling new collators for new session 0 at #0
[2025-02-15T07:41:13Z INFO  pallet_collator_selection::pallet] assembling new collators for new session 1 at #0
[2025-02-15T07:41:13Z INFO  polkadot_sdk_frame::benchmark::pallet] Loading WASM from file
└  Failed to run benchmarking: Invalid input: Could not call runtime API to Did not find the benchmarking metadata. This could mean that you either did not build the node correctly with the `--features runtime-benchmarks` flag, or the chain spec that you are using was not created by a node that was compiled with the flag: Other: Exported method Benchmark_benchmark_metadata is not found
└  Benchmark completed successfully!

Probably we can't verify whether the runtime has been build with --runtime-benchmarks at this point right? It would be ideal if we could, but if not, we should at least prevent the misleading success message. I can see here, it shows a message with the error but continue the execution: https://github.com/r0gue-io/pop-cli/blob/chugnquantin/feat-build_runtime/crates/pop-cli/src/commands/bench/mod.rs#L68

  • The second issue relates to locating the WASM file, see my comment below.

Out of scope for this PR, but once the benchmarking logic is merged into main, we could consider adding a flag in pop build to handle building with the --runtime-benchmarks feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-final-review The PR is ready for final review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants