-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.dart
More file actions
121 lines (106 loc) · 3.8 KB
/
main.dart
File metadata and controls
121 lines (106 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import 'package:flutter/material.dart';
import 'package:performance_monitor/performance_monitor.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Start total startup timing
PerformanceMonitor.startTimer('Total App Startup');
// Essential services (before runApp) – measurement only
await PerformanceMonitor.measureAsync('Essential Services', () async {
// Simulate some essential startup work
await Future.delayed(const Duration(milliseconds: 300));
});
// Optional: warm up cache layer
final perf = PerformanceOptimizationService.instance;
await perf.initialize();
// Simulate an expensive call that will be cached
await PerformanceMonitor.measureAsync('Smart Cache Demo', () async {
await perf.getCachedOrLoad('user_profile', () async {
await Future.delayed(const Duration(milliseconds: 200));
return {'name': 'Demo User', 'id': 123};
});
});
// Render UI
runApp(const PerformanceMonitorExampleApp());
// Finish total startup timing and print report
PerformanceMonitor.endTimer('Total App Startup');
PerformanceMonitor.printTimingReport();
}
class PerformanceMonitorExampleApp extends StatelessWidget {
const PerformanceMonitorExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Performance Monitor Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
Future<void> _runCacheDemo() async {
final perf = PerformanceOptimizationService.instance;
// First call triggers the loader
await PerformanceMonitor.measureAsync('Cache Demo - first call', () async {
await perf.getCachedOrLoad('demo_cache', () async {
await Future.delayed(const Duration(milliseconds: 500));
return 'heavy_result';
});
});
// Second call returns instantly from cache
await PerformanceMonitor.measureAsync('Cache Demo - second call', () async {
await perf.getCachedOrLoad<String>('demo_cache', () async {
throw Exception('This should not be called if cache works!');
});
});
PerformanceMonitor.printTimingReport();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Performance Monitor'),
backgroundColor: Colors.blue.shade700,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Text(
'Check console for performance report\n'
'⏱ Measures startup & manual operations\n'
'Optional smart caching demo included',
style: TextStyle(fontSize: 16),
),
),
),
const SizedBox(height: 20),
const ElevatedButton(
onPressed: PerformanceMonitor.printTimingReport,
child: Text('Refresh Report'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () async {
await PerformanceMonitor.measureAsync('Manual Test', () async {
await Future.delayed(const Duration(seconds: 1));
});
PerformanceMonitor.printTimingReport();
},
child: const Text('Run Manual Timing Test'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: _runCacheDemo,
child: const Text('Run Cache Demo'),
),
],
),
),
);
}
}