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

Use cached testbedInfo and host_vars in inventory in dut selection fixtures #2811

Merged
merged 6 commits into from Feb 6, 2021

Conversation

sanmalho-git
Copy link
Contributor

@sanmalho-git sanmalho-git commented Jan 14, 2021

Description of PR

Summary:
Fixes # (issue)

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • Test case(new/improvement)

Approach

What is the motivation for this PR?

pytest_generate_tests dynamically adds enum_* fixtures that select DUTs on which the tests are to be parameterized. In order to select the DUTs on which the tests are to run, we need to get the TestbedInfo and also all variables defined in the inventory file for each DUT. These operations are time consuming - like creating TestbedInfo taking 1-9 seconds, and getting variables from inventory taking 3-5 seconds.

pytest_generate_tests is called for all the tests/fixtures that needs to run in a pytest session. This was adding a long time when trying to execute many tests.

This issue was reported in issue #2790 - pytest_generate_tests in tests/conftest.py takes much more time than before

How did you do it?

To fix this, we need to create the TestbedInfo only once, store it, and then use the stored value in the next execution of pytest_generate_tests, rather than re-creating TestbedInfo. Similary, the variables for all the DUTs in the testbed should be read once from the inventory files, stored, and the re-used in the next execution of pytest_generate_tests.

PR #2789 Added caching capability to store any facts using pickle.
PR #2856 added caching capability for TestbedInfo
PR #2873 added caching capability for variables for DUTs in the inventory files.

We use the cached TestbedInfo and DUT variables in the selection of DUTs in pytest_generate_tests.

How did you verify/test it?

Ran tests that use the dut selection fixtures and validated that the delay is seen only once, and not in every call to pytest_generate_tests.

Validated the execution time reduced significantly when using caching when executing iface_namingmode test cases which have ~30 tests.

Any platform specific information?

Supported testbed topology if it's a new test case?

Documentation

@lgtm-com
Copy link

lgtm-com bot commented Jan 14, 2021

This pull request introduces 6 alerts when merging 117b342 into 3c9d9b8 - view on LGTM.com

new alerts:

  • 3 for Unused local variable
  • 2 for Unused import
  • 1 for Wrong number of arguments in a call

@sanmalho-git
Copy link
Contributor Author

retest vsimage please

tests/conftest.py Outdated Show resolved Hide resolved
@sanmalho-git
Copy link
Contributor Author

retest vsimage please

@lgtm-com
Copy link

lgtm-com bot commented Jan 18, 2021

This pull request introduces 2 alerts when merging dd06eb0 into 86d9894 - view on LGTM.com

new alerts:

  • 1 for Unused local variable
  • 1 for Variable defined multiple times

@lguohan lguohan added this to In progress in Chassis via automation Jan 20, 2021
@wangxin
Copy link
Collaborator

wangxin commented Jan 25, 2021

@sanmalho-git After some investigation, I feel that the previously added caching function can be used here to avoid global variables. I have enhanced the caching function and enabled caching of TestbedInfo object in PR #2856.

With the change of PR #2856, the performance of pytest_generate_tests is significantly improved and there is no need to introduce global variables into conftest.py. Well, I am not completely correct about the statement of global variables. My fix still introduced a global variable cache. This variable holds an singleton instance of tests.common.cache::FactsCache.

I have plan to enable cache of more information, like host visible variables, then the performance of pytest_generate_tests should can be further improved. I'll submit more PRs to address that.

@sanmalho-git
Copy link
Contributor Author

@wangxin Agreed. Will wait for your PR to be merged and then will modify the code to use the newly introduced caching for the TestbedInfo.

@yxieca
Copy link
Collaborator

yxieca commented Jan 28, 2021

@sanmalho-git can you resolve the merge conflict and ping us for another review?

@sanmalho-git
Copy link
Contributor Author

@yxieca I was waiting for PR# 2873 from Xin to incorporate his changes as well. Once that is merged, will do the rebase and resolve conflicts.

@wangxin
Copy link
Collaborator

wangxin commented Jan 29, 2021

PR #2873 was merged. This PR should be unblocked now.

@sanmalho-git
Copy link
Contributor Author

retest vsimage please

tests/conftest.py Show resolved Hide resolved
tests/conftest.py Show resolved Hide resolved
This is a fix for issue sonic-net#2790 - pytest_generate_tests in tests/conftest.py takes much more time than before

The reason for the issue was that creating the TestbedInfo takes 1-9 seconds, and was part of
pytest_generate_tests. Since pytest_generate_tests is called for all the tests/fixtures, this was
adding a long time when trying to execute many tests.

For some of the other dut selection fixtures we were also getting variables for the duts
from the inventory files - which also takes a long time - adding to the execution delay.

To fix is to use create the TestbedInfo and getting the variables from the inventory files
for all the DUTS only once - by storing it in global variables. If these global variables
are not set, then we would set them. Thus, creating TestbedInfo and getting variables only once
per pytest run
…hen any of the 'enum_*' fixtures are needed.
…_rand_one_per_hwsku_hostname' fixture.

With caching of facts, the tbInfo and vars in inventory for a host are cached. Instead of using
global variables '_dut_in_testbeds', _dut_vars, we would call the methods again, but since it is
cached, it is not re-creating testbed info or reading the vars from the inventory.

Added enum_rand_one_per_hwsku_hostname fixture to pytest_generate_tests. This is needed
as there are tests like platform tests (test_show_platform_syseeprom) that need to run
on both the supervisor and linecard of a T2 chassis.
Chassis automation moved this from In progress to Reviewer approved Feb 2, 2021
@wangxin
Copy link
Collaborator

wangxin commented Feb 3, 2021

@sanmalho-git Can you update the PR description to match the actual code change? Then we can move forward with merging it.

@sanmalho-git sanmalho-git changed the title Optimizing dut selection fixtures in pytest_generate_tests Used cached testbedInfo and host_vars in inventory in dut selection fixtures Feb 3, 2021
@sanmalho-git sanmalho-git changed the title Used cached testbedInfo and host_vars in inventory in dut selection fixtures Use cached testbedInfo and host_vars in inventory in dut selection fixtures Feb 3, 2021
@sanmalho-git
Copy link
Contributor Author

Changed the title

@wangxin
Copy link
Collaborator

wangxin commented Feb 4, 2021

@sanmalho-git Can you also update the content under What is the motivation for this PR? and How did you do it?? We generally use these information as commit message while merging PRs. The commit message needs to be accurate.

@wangxin wangxin merged commit 7eaf3b0 into sonic-net:master Feb 6, 2021
Chassis automation moved this from Reviewer approved to Done Feb 6, 2021
@sanmalho-git sanmalho-git deleted the fixtures branch February 8, 2021 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants