Skip to content

test(di): avoid pytest loader when importing modules #12812

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

Merged
merged 1 commit into from
Mar 20, 2025

Conversation

P403n1x87
Copy link
Contributor

@P403n1x87 P403n1x87 commented Mar 20, 2025

We avoid the use of the pytest loader when importing a module in one of the DI tests for decorated function discovery. Because the current implementation of the pytest loader does not implement the get_code method, we cannot wrap around it to get the code object for the module before it is executed. As a workaround in tests, we temporarily remove the pytest loader so that we can import the module under "normal" conditions. Once the test is done we restore the state of the sys.meta_path list.

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

We avoid the use of the pytest loader when importing a module in one of
the DI tests for decorated function discovery. Because the current
implementation of the pytest loader does not implement the get_code
method, we cannot wrap around it to get the code object for the module
before it is executed. As a workaround in tests, we temporarily remove
the pytest loader so that we can import the module under "normal"
conditions. Once the test is done we restore the state of the
sys.meta_path list.
@P403n1x87 P403n1x87 added changelog/no-changelog A changelog entry is not required for this PR. Dynamic Instrumentation Dynamic Instrumentation/Live Debugger labels Mar 20, 2025
@P403n1x87 P403n1x87 requested a review from a team as a code owner March 20, 2025 11:32
@P403n1x87 P403n1x87 enabled auto-merge (squash) March 20, 2025 11:32
Copy link
Contributor

CODEOWNERS have been resolved as:

tests/debugging/function/test_discovery.py                              @DataDog/debugger-python

Copy link
Contributor

Bootstrap import analysis

Comparison of import times between this PR and main.

Summary

The average import time in this PR is: 244 ± 5 ms.

The average import time in main is: 244 ± 5 ms.

The import time difference between this PR and main is: 0.5 ± 0.2 ms.

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 1.776 ms (0.73%)
ddtrace.bootstrap.sitecustomize 1.183 ms (0.48%)
ddtrace.bootstrap.preload 1.183 ms (0.48%)
ddtrace.internal.products 1.183 ms (0.48%)
ddtrace.internal.remoteconfig.client 0.592 ms (0.24%)
ddtrace 0.593 ms (0.24%)

@pr-commenter
Copy link

pr-commenter bot commented Mar 20, 2025

Benchmarks

Benchmark execution time: 2025-03-20 12:18:13

Comparing candidate commit 246d56b in PR branch test/di-no-pytest-loader with baseline commit 03bee06 in branch main.

Found 3 performance improvements and 1 performance regressions! Performance is the same for 494 metrics, 2 unstable metrics.

scenario:iast_aspects-ospathdirname_aspect

  • 🟩 execution_time [-412.958ns; -349.187ns] or [-10.007%; -8.462%]

scenario:iast_aspects-ospathsplit_aspect

  • 🟩 execution_time [-507.920ns; -431.431ns] or [-11.476%; -9.748%]

scenario:iast_aspects-ospathsplitdrive_aspect

  • 🟩 execution_time [-285.995ns; -249.687ns] or [-9.398%; -8.205%]

scenario:iast_aspects-rsplit_aspect

  • 🟥 execution_time [+132.113ns; +156.917ns] or [+7.722%; +9.172%]

@P403n1x87 P403n1x87 merged commit f4f76ae into main Mar 20, 2025
281 of 283 checks passed
@P403n1x87 P403n1x87 deleted the test/di-no-pytest-loader branch March 20, 2025 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog/no-changelog A changelog entry is not required for this PR. Dynamic Instrumentation Dynamic Instrumentation/Live Debugger
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants