Skip to content

Comments

feat: add runtime statistics tracking and integration tests for PluginStats#237

Merged
lijingrs merged 1 commit intomofa-org:feature/mofa-rsfrom
LuigiGonnella:feat/rhai-plugin-stats-tests
Feb 23, 2026
Merged

feat: add runtime statistics tracking and integration tests for PluginStats#237
lijingrs merged 1 commit intomofa-org:feature/mofa-rsfrom
LuigiGonnella:feat/rhai-plugin-stats-tests

Conversation

@LuigiGonnella
Copy link
Contributor

📋 Summary

Add per-plugin runtime statistics to the Rhai plugin runtime. Introduces a thread-safe PluginStats struct to track total calls, failed calls, and cumulative latency, along with accessors and JSON snapshot support.

This enables monitoring plugin usage, debugging failures, and measuring performance in a production-ready way.


🔗 Related Issues

Closes #235

Related to #173


🧠 Context

Currently, Rhai plugins do not expose any runtime statistics, making it difficult to measure plugin performance, detect errors, or optimize execution.

This PR solves the problem by:

  • Recording call counts and failures in AtomicU64 counters
  • Measuring total latency and providing average latency calculations
  • Exposing statistics safely to external callers via a stats() accessor
  • Adding unit and integration tests to validate functionality

🛠️ Changes

  • Introduce PluginStats struct with counters, record(), avg_latency_ms(), and to_map()
  • Attach PluginStats to RhaiPlugin and pub use it from rhai_runtime/mod.rs
  • Add stats() accessor on RhaiPlugin returning Arc<PluginStats>
  • Unit tests in crates/mofa-plugins/src/rhai_runtime/plugin.rs for counter increments, average latency, zero-call guard
  • Integration test in crates/mofa-plugins/tests/rhai_integration_tests.rs driving plugin invocations and asserting stats

🧪 How you Tested

  1. Unit tests verify PluginStats counters increment correctly and average latency calculations
  2. Integration test drives a sample plugin through multiple calls and validates stats snapshot
  3. Verified thread-safety by simulating concurrent plugin calls (atomic counters)

📸 Screenshots / Logs (if applicable)

N/A – metrics are exposed via JSON snapshot in tests


⚠️ Breaking Changes

  • No breaking changes
  • Breaking change (describe below)

If breaking: N/A


🧹 Checklist

Code Quality

  • Code follows Rust idioms and project conventions
  • cargo fmt run
  • cargo clippy passes without warnings

Testing

  • Tests added/updated
  • cargo test passes locally without any error

Documentation

  • Public APIs documented
  • README / docs updated (if needed)

PR Hygiene

  • PR is small and focused (one logical change)
  • Branch is up to date with main
  • No unrelated commits
  • Commit messages explain why, not only what

🚀 Deployment Notes (if applicable)

No migrations or config changes; rollout is safe.


🧩 Additional Notes for Reviewers

  • PluginStats uses AtomicU64 for thread-safety
  • Zero-call guard ensures average latency doesn’t divide by zero
  • Integration test demonstrates typical usage and snapshot reading

@LuigiGonnella
Copy link
Contributor Author

@BH3GEI let me know

@lijingrs lijingrs merged commit 59880d8 into mofa-org:feature/mofa-rs Feb 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants