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

feat: add benchmark to workflow #97

Merged
merged 2 commits into from
Dec 10, 2024

Conversation

mehul-m-prajapati
Copy link
Contributor

@mehul-m-prajapati mehul-m-prajapati commented Dec 9, 2024

Benchmark is required for tailcall-template/issues/9

Summary by CodeRabbit

  • New Features
    • Introduced a new option to enable or disable benchmark jobs in the workflow.
    • Added functionality to conditionally run benchmarks based on user preference.

@tusharmath
Copy link
Contributor

Lint fixes. Might want to fix those as well.

@@ -115,8 +118,8 @@ fn release_job(cond: &Context<bool>, build: &Job, permissions: &Permissions) ->
.add_step(Release::default().command(Command::Release))
}

fn build_and_test() -> Job {
Job::new("Build and Test")
fn build_and_test(value: &Workflow) -> Job {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn build_and_test(value: &Workflow) -> Job {
fn build_and_test(&self) -> Job {

Convert to a method in Workflow

Copy link
Contributor

Choose a reason for hiding this comment

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

@coderabbitai resolve

Copy link

Choose a reason for hiding this comment

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

@tusharmath, understood. Converting build_and_test into a method of the Workflow struct makes sense. Here's how you can update the code:

impl Workflow {
    // Other methods...

    /// Generates the build and test job.
    fn build_and_test(&self) -> Job {
        let mut job = Job::new("Build and Test")
            .permissions(Permissions::default().contents(Level::Read))
            .add_step(Step::checkout())
            .add_step(
                Toolchain::default()
                    .add_stable()
                    .add_nightly()
                    .add_clippy()
                    .add_fmt(),
            )
            .add_step(
                Cargo::new("test")
                    .args("--all-features --workspace")
                    .name("Cargo Test"),
            )
            .add_step(
                Cargo::new("fmt")
                    .nightly()
                    .args("--check")
                    .name("Cargo Fmt"),
            )
            .add_step(
                Cargo::new("clippy")
                    .nightly()
                    .args("--all-features --workspace -- -D warnings")
                    .name("Cargo Clippy"),
            );

        if self.benchmarks {
            job = job.add_step(
                Cargo::new("bench")
                    .args("--workspace")
                    .name("Cargo Bench"),
            );
        }

        job
    }
}

Then, in the From<Workflow> for GHWorkflow implementation, update the call to build_and_test:

let build = self.build_and_test();

✏️ Learnings added
Learnt from: tusharmath
PR: tailcallhq/gh-workflow#97
File: crates/gh-workflow-tailcall/src/workflow.rs:121-121
Timestamp: 2024-12-10T06:33:20.832Z
Learning: In `crates/gh-workflow-tailcall/src/workflow.rs`, prefer converting standalone functions into methods of the `Workflow` struct when appropriate.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you create a suggestion for the fix @coderabbitai

Copy link

Choose a reason for hiding this comment

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

@tusharmath, certainly! Here's the suggested code to implement the fix:

In crates/gh-workflow-tailcall/src/workflow.rs, convert build_and_test into a method of the Workflow struct:

impl Workflow {
    // Other methods...

    /// Generates the build and test job.
    fn build_and_test(&self) -> Job {
        let mut job = Job::new("Build and Test")
            .permissions(Permissions::default().contents(Level::Read))
            .add_step(Step::checkout())
            .add_step(
                Toolchain::default()
                    .add_stable()
                    .add_nightly()
                    .add_clippy()
                    .add_fmt(),
            )
            .add_step(
                Cargo::new("test")
                    .args("--all-features --workspace")
                    .name("Cargo Test"),
            )
            .add_step(
                Cargo::new("fmt")
                    .nightly()
                    .args("--check")
                    .name("Cargo Fmt"),
            )
            .add_step(
                Cargo::new("clippy")
                    .nightly()
                    .args("--all-features --workspace -- -D warnings")
                    .name("Cargo Clippy"),
            );

        if self.benchmarks {
            job = job.add_step(
                Cargo::new("bench")
                    .args("--workspace")
                    .name("Cargo Bench"),
            );
        }

        job
    }
}

Then, update the call to build_and_test in the From<Workflow> for GHWorkflow implementation:

let build = self.build_and_test();

This change converts build_and_test into a method of the Workflow struct, allowing you to access the benchmarks field directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@coderabbitai: Can you push commit with this changes?

Copy link

Choose a reason for hiding this comment

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

@mehul-m-prajapati, I'm unable to push commits directly, but I can guide you through the required changes.

In crates/gh-workflow-tailcall/src/workflow.rs, convert build_and_test into a method of the Workflow struct:

impl Workflow {
    // Other methods...

    /// Generates the build and test job.
    fn build_and_test(&self) -> Job {
        let mut job = Job::new("Build and Test")
            .permissions(Permissions::default().contents(Level::Read))
            .add_step(Step::checkout())
            .add_step(
                Toolchain::default()
                    .add_stable()
                    .add_nightly()
                    .add_clippy()
                    .add_fmt(),
            )
            .add_step(
                Cargo::new("test")
                    .args("--all-features --workspace")
                    .name("Cargo Test"),
            )
            .add_step(
                Cargo::new("fmt")
                    .nightly()
                    .args("--check")
                    .name("Cargo Fmt"),
            )
            .add_step(
                Cargo::new("clippy")
                    .nightly()
                    .args("--all-features --workspace -- -D warnings")
                    .name("Cargo Clippy"),
            );

        if self.benchmarks {
            job = job.add_step(
                Cargo::new("bench")
                    .args("--workspace")
                    .name("Cargo Bench"),
            );
        }

        job
    }
}

Then, update the call to build_and_test in the From<Workflow> for GHWorkflow implementation:

let build = self.build_and_test();

These changes will convert build_and_test into a method of the Workflow struct, allowing you to access the benchmarks field directly.

@mehul-m-prajapati
Copy link
Contributor Author

Lint fixes. Might want to fix those as well.

Are you referring to this (#55) ?

PR done: #98

@tusharmath
Copy link
Contributor

@mehul-m-prajapati The CI is failing. Moving to draft to reduce noise and improve CI efficiency. Once you are ready just mark it as "ready to review". Feel free to give a shoutout on the #contributors channel on Discord if you want immediate attention.

@tusharmath tusharmath marked this pull request as draft December 9, 2024 17:53
@tusharmath
Copy link
Contributor

@coderabbitai review

Copy link

coderabbitai bot commented Dec 10, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

coderabbitai bot commented Dec 10, 2024

Walkthrough

The changes introduce a new boolean field benchmarks to the Workflow struct, allowing users to enable or disable a benchmark job in the workflow. The Default implementation initializes benchmarks to false. The build_and_test function is modified to accept a reference to a Workflow instance, enabling conditional addition of a benchmark step. If benchmarks is enabled, a step for running benchmarks with Cargo is included. The control flow is adjusted to incorporate this functionality while maintaining the overall structure of workflow generation.

Changes

File Path Change Summary
crates/gh-workflow-tailcall/src/workflow.rs Added pub benchmarks: bool to Workflow struct; updated build_and_test signature to accept a reference to Workflow. Conditional logic added to include a benchmark step based on benchmarks.

Poem

In the meadow where workflows play,
A benchmark field has come to stay.
With toggles set, the tests will run,
Bringing joy to every code fun!
So hop along, let's celebrate,
Configurable paths await our fate! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
crates/gh-workflow-tailcall/src/workflow.rs (2)

24-26: LGTM! Consider enhancing the documentation.

The benchmarks field is well-placed and follows the struct's pattern. Consider adding an IMPORTANT note similar to auto_release if there are any specific requirements or considerations for enabling benchmarks.

    /// When enabled, a benchmark job is added to the workflow.
+   /// *IMPORTANT:* Ensure your repository has benchmark tests configured with `cargo bench`.
    pub benchmarks: bool,

150-156: Consider enhancing the benchmark configuration.

While the benchmark step implementation is correct, consider these improvements:

  1. Add a timeout to prevent long-running benchmarks from blocking CI
  2. Allow customization of benchmark arguments for different project needs
     if value.benchmarks {
         job = job.add_step(
             Cargo::new("bench")
                 .args("--workspace")
+                .timeout_minutes(30)
                 .name("Cargo Bench"),
         );
     }

Would you like me to propose a more detailed implementation that includes configurable benchmark arguments?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between df3caa1 and 3779192.

📒 Files selected for processing (1)
  • crates/gh-workflow-tailcall/src/workflow.rs (4 hunks)
🧰 Additional context used
🪛 GitHub Check: Build and Test
crates/gh-workflow-tailcall/src/workflow.rs

[warning] 148-148:
Diff in /home/runner/work/gh-workflow/gh-workflow/crates/gh-workflow-tailcall/src/workflow.rs

🔇 Additional comments (3)
crates/gh-workflow-tailcall/src/workflow.rs (3)

31-31: LGTM! Sensible default value.

Setting benchmarks to false by default maintains backward compatibility and follows the principle of least surprise.


121-121: LGTM! Function signature matches previous review suggestion.

The change to accept &Workflow parameter aligns with the previous review comment and improves the API design.


67-67: Verify the workflow generation with benchmarks enabled.

Let's ensure the new benchmark functionality is properly integrated into workflow generation.

✅ Verification successful

Benchmark functionality is properly integrated

The verification shows that the benchmark functionality is correctly implemented:

  • The Workflow struct has a benchmarks field that's false by default
  • The build_and_test function correctly adds a benchmark step when benchmarks is enabled
  • The benchmark configuration is consistent with existing benchmark workflow patterns in the codebase (as seen in workflow-bench.yml)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usage of the Workflow struct to verify integration
rg -A 5 "Workflow\s*{" 

# Search for existing benchmark configurations that might need updates
rg -l "cargo\s+bench" 

Length of output: 136


Script:

#!/bin/bash
# Search for Workflow struct definition and usage
rg -A 10 "struct Workflow"

# Search for build_and_test function definition and usage
ast-grep --pattern 'fn build_and_test($_) { $$$ }'

# Search for benchmark-related configurations
rg -A 5 "bench"

Length of output: 13924


Script:

#!/bin/bash
# Search for build_and_test function implementation
rg -A 20 "fn build_and_test"

# Search for any other benchmark-related configurations in workflow files
fd -e yml -e yaml | xargs rg "cargo\s+bench|benchmark"

Length of output: 2903

@mehul-m-prajapati mehul-m-prajapati marked this pull request as ready for review December 10, 2024 07:22
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
crates/gh-workflow-tailcall/src/workflow.rs (1)

24-26: Enhance documentation for the benchmarks field

Consider adding more details to the documentation comment:

  • When to enable benchmarks
  • Requirements for running benchmarks
  • Impact on CI workflow duration
     /// When enabled, a benchmark job is added to the workflow.
+    /// 
+    /// This will run `cargo bench` on the entire workspace. Consider enabling this
+    /// when performance benchmarking is critical for your project. Note that this
+    /// may increase CI execution time.
     pub benchmarks: bool,
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3779192 and 017b02c.

📒 Files selected for processing (1)
  • crates/gh-workflow-tailcall/src/workflow.rs (3 hunks)
🔇 Additional comments (3)
crates/gh-workflow-tailcall/src/workflow.rs (3)

31-31: LGTM: Default implementation is appropriate

Setting benchmarks to false by default is a good choice as it maintains backward compatibility and allows users to opt-in to benchmark runs.


108-108: LGTM: Correct usage of the build_and_test method

The conversion to using the method is implemented correctly.


77-83: Consider error handling for benchmark failures

The benchmark step should be configured to handle failures appropriately. Consider whether benchmark failures should fail the build or be treated as warnings.

         if self.benchmarks {
             job = job.add_step(
                 Cargo::new("bench")
                     .args("--workspace")
+                    .continue_on_error(true)  // Optional: Prevent CI failure on benchmark regression
                     .name("Cargo Bench"),
             );
         }

@tusharmath tusharmath merged commit edc5edb into tailcallhq:main Dec 10, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants