Skip to content

Middleware Performance Benchmarks & External Plugin System#434

Merged
A6dulmalik merged 3 commits intoMindBlockLabs:mainfrom
OthmanImam:feat/Pre-Midleware
Mar 28, 2026
Merged

Middleware Performance Benchmarks & External Plugin System#434
A6dulmalik merged 3 commits intoMindBlockLabs:mainfrom
OthmanImam:feat/Pre-Midleware

Conversation

@OthmanImam
Copy link
Copy Markdown
Contributor

Overview

This PR adds two major features to the @mindblock/middleware package:

  1. Per-Middleware Performance Benchmarks - Automated tooling to measure latency overhead of each middleware individually
  2. External Plugin Loader - Complete system for dynamically loading and managing middleware plugins from npm packages

All implementation is confined to the middleware repository with no backend modifications.

Features

Performance Benchmarks (#369)

  • Automated benchmarking script measuring middleware overhead against baseline
  • Tracks requests/second, latency percentiles (p50, p95, p99), and error rates
  • Individual profiling for JWT Auth, RBAC, Security Headers, Timeout, Circuit Breaker, Correlation ID
  • Compare middlewares by contribution to overall latency
  • CLI commands: npm run benchmark and npm run benchmark:ci

Files:

  • scripts/benchmark.ts - Load testing implementation
  • docs/PERFORMANCE.md - Benchmarking documentation (updated)
  • tests/integration/benchmark.integration.spec.ts - Test coverage

External Plugin Loader System

  • PluginInterface - Standard contract for all plugins
  • PluginLoader - Low-level discovery, loading, and lifecycle management
  • PluginRegistry - High-level plugin orchestration and management
  • Plugin lifecycle hooks: onLoad, onInit, onActivate, onDeactivate, onUnload, onReload
  • Configuration validation with JSON Schema support
  • Semantic version compatibility checking
  • Plugin dependency resolution
  • Priority-based execution ordering
  • Comprehensive error handling (10 custom error types)

Files:

  • src/common/interfaces/plugin.interface.ts - Plugin types and metadata
  • src/common/interfaces/plugin.errors.ts - Error classes
  • src/common/utils/plugin-loader.ts - Loader service (650+ lines)
  • src/common/utils/plugin-registry.ts - Registry service (400+ lines)
  • src/plugins/example.plugin.ts - Template plugin for developers
  • docs/PLUGINS.md - Complete plugin documentation (750+ lines)
  • docs/PLUGIN_QUICKSTART.md - Quick start guide for plugin developers (600+ lines)
  • tests/integration/plugin-system.integration.spec.ts - Integration tests

Usage

Performance Benchmarking

npm run benchmark

Outputs comprehensive latency overhead comparison for each middleware.

Loading Plugins

import { PluginRegistry } from '@mindblock/middleware';

const registry = new PluginRegistry({ autoLoadEnabled: true });
await registry.init();

const plugin = await registry.load('@yourorg/plugin-example');
await registry.initialize(plugin.metadata.id);
await registry.activate(plugin.metadata.id);

Creating Plugins

Developers can create plugins by implementing PluginInterface:

export class MyPlugin implements PluginInterface {
  metadata: PluginMetadata = {
    id: 'com.org.plugin.example',
    name: 'My Plugin',
    version: '1.0.0',
    description: 'My custom middleware'
  };

  getMiddleware() {
    return (req, res, next) => { /* middleware logic */ };
  }
}

Publish to npm with scoped name (@yourorg/plugin-name) and users can discover and load automatically.

Testing

  • Benchmark integration tests validate middleware setup
  • Plugin system tests cover:
    • Plugin interface validation
    • Lifecycle hook execution
    • Configuration validation
    • Dependency resolution
    • Error handling
    • Batch operations

Run tests: npm test

Dependencies Added

  • autocannon@^7.15.0 - Load testing library (already installed, fallback to simple HTTP client)
  • semver@^7.6.0 - Semantic version validation
  • @types/semver@^7.5.8 - TypeScript definitions
  • ts-node@^10.9.2 - TypeScript execution

Documentation

  • PERFORMANCE.md - Performance optimization guide and benchmarking docs
  • PLUGINS.md - Comprehensive plugin system documentation with examples
  • PLUGIN_QUICKSTART.md - Quick start for plugin developers with patterns and examples
  • README.md - Updated with plugin system overview

Breaking Changes

None. All additions are backward compatible.

Commits


Ready for review and merge into main after testing!

- Implement automated benchmarking system to measure latency overhead of each middleware individually
- Create benchmark.ts script with load testing client for realistic performance measurement
- Support benchmarking of JWT Auth, RBAC, Security Headers, Timeout, Circuit Breaker, and Correlation ID middleware
- Add npm scripts: 'benchmark' and 'benchmark:ci' for running performance tests
- Update PERFORMANCE.md with comprehensive benchmarking documentation and usage guide
- Add benchmark integration tests to verify middleware initialization
- Update package.json with autocannon (load testing) and ts-node dependencies
- Update README.md with performance benchmarking section
- Update tsconfig.json to include scripts directory
- Export security middleware components for benchmarking

All implementation confined to middleware repository as required.
PLUGIN SYSTEM IMPLEMENTATION
============================

Core Components:
- PluginInterface: Standard interface all plugins must implement
- PluginLoader: Low-level plugin discovery, loading, and lifecycle management
- PluginRegistry: High-level service for plugin management and orchestration

Key Features:
✓ Dynamic discovery of plugins from npm packages
✓ Plugin lifecycle management (load, init, activate, deactivate, unload, reload)
✓ Configuration validation with JSON Schema support
✓ Semantic version compatibility checking
✓ Dependency resolution between plugins
✓ Plugin priority-based execution ordering
✓ Plugin registry with search and filter capabilities
✓ Plugin context for access to shared services
✓ Comprehensive error handling with specific error types
✓ Plugin middleware export and utility export
✓ Plugin statistics and monitoring

Error Types:
- PluginNotFoundError
- PluginLoadError
- PluginAlreadyLoadedError
- PluginConfigError
- PluginDependencyError
- PluginVersionError
- PluginInitError
- PluginInactiveError
- InvalidPluginPackageError
- PluginResolutionError

Files Added:
- src/common/interfaces/plugin.interface.ts: Core plugin types and metadata
- src/common/interfaces/plugin.errors.ts: Custom error classes
- src/common/utils/plugin-loader.ts: PluginLoader service implementation
- src/common/utils/plugin-registry.ts: PluginRegistry service implementation
- src/plugins/example.plugin.ts: Example plugin template
- tests/integration/plugin-system.integration.spec.ts: Plugin system tests
- docs/PLUGINS.md: Complete plugin system documentation
- docs/PLUGIN_QUICKSTART.md: Quick start guide for plugin developers

Files Modified:
- package.json: Added semver, @types/semver dependencies
- src/index.ts: Export plugin system components
- src/common/interfaces/index.ts: Plugin interface exports
- src/common/utils/index.ts: Plugin utility exports
- README.md: Added plugin system overview and links

USAGE EXAMPLE:
==============
const registry = new PluginRegistry({ autoLoadEnabled: true });
await registry.init();
const plugin = await registry.load('@yourorg/plugin-example');
await registry.initialize(plugin.metadata.id);
await registry.activate(plugin.metadata.id);

PLUGIN DEVELOPMENT:
===================
1. Implement PluginInterface with metadata
2. Create package.json with mindblockPlugin configuration
3. Export plugin class/instance from main entry point
4. Publish to npm with scoped name (@yourorg/plugin-name)
5. Users can discover, load, and activate via PluginRegistry

All implementation confined to middleware repository as required.
@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 28, 2026

@OthmanImam Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@A6dulmalik A6dulmalik merged commit 064a251 into MindBlockLabs:main Mar 28, 2026
2 of 6 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.

External Plugin Loader (npm packages) Per-Middleware Performance Benchmarks

2 participants