Flutter Performance Monitor with Optional Smart Cache
Lightweight timing utilities for measuring startup and async operations, plus an optional cache layer to deduplicate expensive calls.
Add the package to your project using one of the following methods:
dependencies:
performance_monitor: ^1.1.2flutter pub add performance_monitorimport 'package:performance_monitor/performance_monitor.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Start total startup timing
PerformanceMonitor.startTimer('App Startup');
// Measure essential startup services
await PerformanceMonitor.measureAsync('Essential Services', () async {
// Replace with actual startup tasks, e.g. Firebase.initializeApp(), config loading, etc.
await Future.delayed(const Duration(milliseconds: 300));
});
// Optional: render UI after timing measurements begin
runApp(const MyApp());
// End startup timing and print report
PerformanceMonitor.endTimer('App Startup');
PerformanceMonitor.printTimingReport();
}The cache layer deduplicates expensive API calls by caching results.
final perf = PerformanceOptimizationService.instance;
await perf.initialize();final userData = await perf.getCachedOrLoad(
'user_profile',
() => fetchUserProfile(), // your loader returning Future<T>
);Notes:
- The caching layer is optional.
PerformanceMonitorremains focused on timing; the cache layer provides deduplication for expensive calls when needed. - The sample demonstrates that the first call incurs latency, while subsequent calls reuse the cached result.
📊 PERFORMANCE REPORT
==================================================
App Startup : 350ms 28.1%
Essential Services : 300ms 24.0%
Smart Cache Demo : 218ms 17.4%
Total App Startup : 868ms 100.0%
==================================================
TOTAL: 868ms
Measure async operations:
await PerformanceMonitor.measureAsync('API Call', apiCall);Manual timing blocks:
PerformanceMonitor.startTimer('Long Task');
// ... your code here
PerformanceMonitor.endTimer('Long Task');Get the slowest operation above a threshold:
final slowest = PerformanceMonitor.getSlowestOperation(100);Get all operations slower than a threshold:
final slowOps = PerformanceMonitor.getSlowOperations(100);Print detailed timing report with percentages.
Initialize the caching layer (call once, typically at startup):
await PerformanceOptimizationService.instance.initialize();Load data with caching and deduplication:
final data = await perf.getCachedOrLoad('key', expensiveLoader);Manual cache eviction controls:
perf.clearCache('key');
perf.clearAllCache();Complete example usage is demonstrated in the example/lib/main.dart. The app shows:
- Measuring total startup time
- Timing essential async startup operations
- Optional smart caching demo with a first slow call followed by cached reuse
Navigate to the example project:
cd exampleGet dependencies and run:
flutter pub get
flutter run- Version 1.1.0 introduces a simplified, Future-based cache layer that prevents duplicate API calls by caching the underlying Future. Timing functionality remains the core feature of
PerformanceMonitor. - The pubspec version should be bumped to
1.1.0to reflect the new API and behavior. - Documentation emphasizes the optional nature of the caching layer and provides clear Quick Start usage for both timing and caching scenarios.
- Fork the repo
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push (
git push origin feature/amazing-feature) - Open Pull Request