Skip to content

Conversation

@germa89
Copy link
Collaborator

@germa89 germa89 commented Nov 28, 2025

Description

refactor list instances CLI. Also improved the speed, down to 1.5s to execute.

Issue linked

NA

Checklist

AI Summary

Optimize CLI list command performance with process enumeration improvements

Summary

This PR optimizes the CLI list command performance by improving process enumeration efficiency, reducing execution time by 52%.

Changes Made

  • Core CLI Module (src/ansys/mapdl/core/cli/core.py):

    • Modified get_mapdl_instances() to use psutil.process_iter(attrs=['name']) instead of psutil.process_iter()
    • Changed from proc.name() to proc.info['name'] to leverage pre-fetched process attributes
    • This avoids additional system calls for each process during iteration
  • Test Updates (tests/test_cli.py):

    • Updated all mock process objects to include .info attribute for compatibility with optimized process iteration
    • Modified make_fake_process(), make_other_user_process(), make_inaccessible_process(), and standalone mocks
  • Lazy Imports (src/ansys/mapdl/core/__init__.py):

    • Added lazy imports for convert functions to reduce initial CLI module load time

Performance Metrics

  • Before: ~2.7 seconds execution time for pymapdl list
  • After: ~1.3 seconds execution time for pymapdl list
  • Improvement: 52% reduction in execution time

Goals and Ideas

The primary goal was to address slow CLI startup times, particularly for the list command which enumerates all system processes to find MAPDL instances. The bottleneck was identified as the process iteration loop making individual system calls for each process name.

The optimization leverages psutil's attrs parameter to fetch process names upfront during iteration, eliminating redundant system calls. This approach maintains full compatibility while significantly improving performance.

Additional lazy loading of heavy modules further reduces CLI startup overhead, contributing to the overall performance improvement.

Testing

  • All 54 CLI tests pass (52 passed, 2 skipped for Linux-only features)
  • No regressions in functionality
  • Mock objects properly simulate optimized psutil behavior

This optimization makes the CLI more responsive for users working with MAPDL instances, especially in environments with many running processes.

@germa89 germa89 requested a review from a team as a code owner November 28, 2025 11:35
@germa89 germa89 self-assigned this Nov 28, 2025
@github-actions github-actions bot added the enhancement Improve any current implemented feature label Nov 28, 2025
@codecov
Copy link

codecov bot commented Nov 28, 2025

Codecov Report

❌ Patch coverage is 91.30435% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.38%. Comparing base (d3c0c5d) to head (b72f419).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4329      +/-   ##
==========================================
- Coverage   91.41%   91.38%   -0.03%     
==========================================
  Files         193      194       +1     
  Lines       15745    15783      +38     
==========================================
+ Hits        14393    14424      +31     
- Misses       1352     1359       +7     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@germa89 germa89 changed the title Refactor/list_instances refactor: list_instances Nov 28, 2025
@germa89
Copy link
Collaborator Author

germa89 commented Nov 28, 2025

@pyansys-ci-bot LGTM.

@germa89 germa89 enabled auto-merge (squash) November 28, 2025 16:41
Copy link
Contributor

@pyansys-ci-bot pyansys-ci-bot left a comment

Choose a reason for hiding this comment

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

✅ Approving this PR because germa89 said so in here 😬

LGTM

@germa89 germa89 merged commit 8a2ed11 into main Nov 28, 2025
85 of 86 checks passed
@germa89 germa89 deleted the refactor/list_instances branch November 28, 2025 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improve any current implemented feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants