Skip to content

tests: add cProfile performance tests for plugin framework#2062

Merged
crivetimihai merged 3 commits intomainfrom
feat/plugin_framework_profiling
Jan 14, 2026
Merged

tests: add cProfile performance tests for plugin framework#2062
crivetimihai merged 3 commits intomainfrom
feat/plugin_framework_profiling

Conversation

@araujof
Copy link
Copy Markdown
Member

@araujof araujof commented Jan 13, 2026

Summary

This PR introduces comprehensive performance profiling tooling for the plugin framework using Python's cProfile. The new test suite enables systematic profiling of individual plugin hooks to identify performance bottlenecks and measure optimization impact.

Closes #2061

Changes

New Files

  • tests/performance/test_plugins_performance.py

    • Main profiling script using cProfile
    • Profiles each enabled plugin's hooks individually (1000 iterations per hook)
    • Generates .prof files for detailed analysis
    • Prints summary table showing average execution time per plugin per hook
    • Supports --details flag for immediate detailed output
  • tests/performance/PLUGIN_PROFILING.md

    • Complete usage guide and documentation
    • Analysis techniques and examples
    • Optimization strategies and best practices
    • Troubleshooting guide
  • tests/performance/plugins/config.yaml

    • Test configuration with all plugins enabled
    • Used by performance profiler for consistent testing
  • tests/performance/utils/analyze_profiles.py

    • Utility functions to analyze and compare plugins performance profiles

Modified Files

  • tests/performance/Makefile

    • Added convenience targets for running plugin performance tests
    • Added profile analysis commands
  • tests/performance/README.md

    • Updated with plugin profiling documentation reference

Features

Performance Profiling

  • Profiles invoke_hook() calls only (plugin loading excluded)
  • Generates one .prof file per plugin-hook combination
  • Calculates average execution time using actual call counts from profiler
  • Summary table with plugins as rows, hook types as columns
  • Empty cells for unimplemented hooks
  • Uses plugins/config.yaml for plugin configuration

Output Modes

Default (summary only):

python tests/performance/test_plugins_performance.py

Detailed (with per-hook profiling output):

python tests/performance/test_plugins_performance.py --details

Sample Output

Plugin                       P:post       P:pre      T:post       T:pre
-----------------------------------------------------------------------
PIIFilterPlugin             0.058ms     0.103ms     0.078ms     0.632ms
ReplaceBadWordsPlugin       0.056ms     0.103ms     0.078ms     0.635ms
SPARCStaticValidator              —           —           —     0.625ms
VaultPlugin                       —           —           —     0.636ms

Legend:

  • P: = Prompt hooks (pre/post)
  • T: = Tool hooks (pre/post invoke)
  • R: = Resource hooks (pre/post fetch)
  • = Hook not implemented by plugin

Usage

Run Performance Tests

# From project root
LOG_LEVEL=ERROR \
python tests/performance/test_plugins_performance.py 2>/dev/null

Analyze Specific Profile

python -c "import pstats; \
  p = pstats.Stats('prof/PIIFilterPlugin_tool_pre_invoke.prof'); \
  p.strip_dirs().sort_stats('cumulative').print_stats(20)"

Visualize with SnakeViz

pip install snakeviz
snakeviz prof/PIIFilterPlugin_tool_pre_invoke.prof

Create call graph

gprof2dot -f pstats plugins/prof/SecretsDetection_prompt_pre_fetch.prof | dot -Tsvg -o plugins/prof/SecretsDetection_prompt_pre_fetch.svg 
image

Create flame graph

flameprof plugins/prof/PIIFilterPlugin_tool_pre_invoke.prof > plugins/prof/flamegraph.svg
image

Benefits

  1. Identify Bottlenecks: Pinpoint slow functions in plugin execution
  2. Measure Optimizations: Compare before/after profiles quantitatively
  3. Regression Detection: Track performance over time in CI/CD
  4. Plugin Development: Optimize new plugins during development
  5. Framework Optimization: Identify framework overhead vs plugin work

Documentation

Complete documentation available in:

  • tests/performance/PLUGIN_PROFILING.md

@araujof araujof added this to the Release 1.0.0-BETA-2 milestone Jan 13, 2026
@araujof araujof self-assigned this Jan 13, 2026
@araujof araujof added performance Performance related items plugins labels Jan 13, 2026
@araujof araujof requested a review from terylt January 13, 2026 04:34
Copy link
Copy Markdown
Collaborator

@terylt terylt left a comment

Choose a reason for hiding this comment

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

LGTM

@crivetimihai crivetimihai self-assigned this Jan 14, 2026
araujof and others added 3 commits January 14, 2026 00:41
Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>
Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>
- Add noqa E402 comments for intentional late imports after sys.path
- Fix type annotation: use Any instead of any (lowercase was incorrect)
- Remove unnecessary f-string prefixes from static strings

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai crivetimihai force-pushed the feat/plugin_framework_profiling branch from 03bbb89 to 02cb79e Compare January 14, 2026 00:46
@crivetimihai crivetimihai merged commit 0c9252a into main Jan 14, 2026
36 of 38 checks passed
@crivetimihai crivetimihai deleted the feat/plugin_framework_profiling branch January 14, 2026 00:48
kcostell06 pushed a commit to kcostell06/mcp-context-forge that referenced this pull request Feb 24, 2026
* tests: add cProfile performance tests for plugins

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* tests: add comparison script for plugin framework profiles

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* fix: address linting issues in plugin profiling tests

- Add noqa E402 comments for intentional late imports after sys.path
- Fix type annotation: use Any instead of any (lowercase was incorrect)
- Remove unnecessary f-string prefixes from static strings

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

---------

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Co-authored-by: Mihai Criveti <crivetimihai@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance related items plugins

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PERFORMANCE]: Add performance test profiling and guideline for plugins

3 participants