tmt icon indicating copy to clipboard operation
tmt copied to clipboard

Enabling Perflint checks

Open martinhoyer opened this issue 1 year ago • 8 comments

Will probably have negligible performance improvements, but it seems like a low-hanging fruit.

Fixed PERF401 and PERF403 issues, and 2 of the PERF203 issues. The remaining PERF203 issues are more complex and might require more significant refactoring, which could affect readability and functionality.

martinhoyer avatar Dec 04 '24 18:12 martinhoyer

@martinhoyer Huston, a little problem - https://stackoverflow.com/questions/34566806/why-use-contextlib-suppress-as-opposed-to-try-except-with-pass

I've checked same timeit command on python3.13 (my laptop) and it indeed is much worse with suppress. So I'd say different approach is necessary if the whole point is to get better performance :/

lukaszachy avatar Dec 05 '24 08:12 lukaszachy

@martinhoyer Huston, a little problem - https://stackoverflow.com/questions/34566806/why-use-contextlib-suppress-as-opposed-to-try-except-with-pass

I've checked same timeit command on python3.13 (my laptop) and it indeed is much worse with suppress. So I'd say different approach is necessary if the whole point is to get better performance :/

Nice, good info. Thanks!

Actually the supress wasn't added as part of performance optimization. The perflint is complaining about try except being inside the loop.
We can do the "same" changes without the supress, by moving the loops within try-except(where applicable).

btw, have you run the timeits with some of the changed code here? Just curious if it's also slower.

martinhoyer avatar Dec 05 '24 08:12 martinhoyer

Out of curiosity, I've tried to solve the remaining PERF203 and tested modifying this function:

    for i in range(attempts):
        try:
            return func(*args, **kwargs)
        except Exception as exc:
            exceptions.append(exc)
            logger.debug(
                'retry',
                f"{label} failed, {attempts - i} retries left, "
                f"trying again in {interval:.2f} seconds.")
            logger.fail(str(exc))
            time.sleep(interval)
    raise RetryError(label, causes=exceptions)

to

    retries_left = attempts

    while retries_left > 0:
        retries_left -= 1
        try:
            return func(*args, **kwargs)
        except Exception as exc:
            exceptions.append(exc)
            logger.debug(
                'retry',
                f"{label} failed, {retries_left} retries left, "
                f"trying again in {interval:.2f} seconds.")
            logger.fail(str(exc))

            if retries_left > 0:
                time.sleep(interval)

    raise RetryError(label, causes=exceptions)

An the test results over a milion iterations: Average 1: 0.0000056165 seconds Average 2: 0.0000056047 seconds

image

Anyway, I'm going to do something useful.

martinhoyer avatar Dec 05 '24 11:12 martinhoyer

Rebase will be a PITA...

happz avatar Apr 11 '25 10:04 happz

Rebase will be a PITA...

Indeed, I've started from scratch on top of https://github.com/teemtee/tmt/pull/3662 Marking as draft until that one is merged.

martinhoyer avatar Apr 11 '25 16:04 martinhoyer

@coderabbitai full review

martinhoyer avatar Apr 11 '25 17:04 martinhoyer

📝 Walkthrough

Walkthrough

The changes in this update primarily refactor multiple sections of the codebase to replace explicit for-loops used for constructing lists or dictionaries with more concise list or dictionary comprehensions. This affects test files, utility modules, and various components within the core logic, such as result processing, provisioning, reporting, and installation steps. Additionally, the linting configuration is updated to enable performance-related checks while excluding a specific rule. Two new helper methods are introduced in the installation logic to encapsulate package installation attempts. No changes are made to the core logic or control flow, and the functional behavior of the code remains the same.

Changes

File(s) Change Summary
pyproject.toml Updated [tool.ruff.lint] to enable "PERF" checks and ignore "PERF203" for performance linting configuration.
tests/unit/provision/testcloud/test_hw.py
tests/unit/test_config.py
tests/unit/test_utils.py
Refactored list construction in tests from explicit for-loops to list comprehensions for conciseness; no logic changes.
tmt/convert.py
tmt/frameworks/beakerlib.py
tmt/result.py
tmt/steps/discover/init.py
tmt/steps/execute/init.py
tmt/steps/provision/init.py
tmt/steps/provision/mrack.py
tmt/steps/report/reportportal.py
tmt/utils/git.py
Refactored various list and dictionary constructions from for-loops to comprehensions for improved readability and conciseness; logic unchanged.
tmt/steps/prepare/install.py Introduced two helper methods for package installation attempts, refactored loops to use these helpers and comprehensions; improved modularity.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant InstallRpmOstree
    participant PackageManager

    User->>InstallRpmOstree: install_local(packages)
    loop for each pkg in packages
        InstallRpmOstree->>InstallRpmOstree: try_install_local_package(pkg)
        alt success
            Note right of InstallRpmOstree: Add pkg to installed list
        else failure
            InstallRpmOstree->>User: Warn about installation failure
        end
    end

    User->>InstallRpmOstree: install_from_repository(recommended_packages)
    loop for each pkg in recommended_packages
        InstallRpmOstree->>InstallRpmOstree: try_install_recommended_package(pkg)
        alt success
            Note right of InstallRpmOstree: Continue
        else failure
            InstallRpmOstree->>User: Warn about installation failure
        end
    end

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 sequence diagram to generate a sequence diagram of the changes in this PR.
  • @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.

coderabbitai[bot] avatar Apr 11 '25 17:04 coderabbitai[bot]

@coderabbitai full review

martinhoyer avatar Apr 29 '25 15:04 martinhoyer

~~@martinhoyer please, check out the failed pre-commit check. I tried, but I can't convince my gh to work with Github to even checkout your branch :/~~

Nevermind, I was trying to use gh in a Gitlab repo...

happz avatar May 15 '25 13:05 happz