forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshimmer_cpu_test.dart
More file actions
402 lines (353 loc) · 17.3 KB
/
Copy pathshimmer_cpu_test.dart
File metadata and controls
402 lines (353 loc) · 17.3 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:shimmer/shimmer.dart';
/// Shimmer CPU Usage Test
///
/// This test confirms that the Shimmer widget causes continuous CPU usage
/// even when idle, compared to a static widget.
///
/// Run with:
/// ```bash
/// flutter drive \
/// --driver=test_driver/integration_test.dart \
/// --target=integration_test/shimmer_cpu_test.dart \
/// --profile \
/// --flavor dev \
/// -d <device_id>
/// ```
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('Shimmer CPU Usage Test', () {
testWidgets('Compare frame counts: Shimmer vs Static widget', (WidgetTester tester) async {
debugPrint('');
debugPrint('╔══════════════════════════════════════════════════════════════╗');
debugPrint('║ SHIMMER CPU USAGE TEST ║');
debugPrint('╚══════════════════════════════════════════════════════════════╝');
debugPrint('');
// === TEST 1: Static widget (baseline) ===
debugPrint('[1/3] Testing STATIC widget (10 seconds)...');
debugPrint(' Expected: Minimal frames (only initial render)');
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Container(
width: 300,
height: 100,
decoration: BoxDecoration(color: const Color(0xFF2A2A32), borderRadius: BorderRadius.circular(16)),
child: const Center(
child: Text('Processing...', style: TextStyle(color: Colors.white, fontSize: 16)),
),
),
),
),
),
);
await tester.pumpAndSettle();
final staticTimings = <FrameTiming>[];
void staticCallback(List<FrameTiming> timings) {
staticTimings.addAll(timings);
}
WidgetsBinding.instance.addTimingsCallback(staticCallback);
// Pump for 10 seconds without any interaction
for (int i = 0; i < 100; i++) {
await tester.pump(const Duration(milliseconds: 100));
}
WidgetsBinding.instance.removeTimingsCallback(staticCallback);
debugPrint(' Static widget frames: ${staticTimings.length}');
// === TEST 2: Shimmer widget ===
debugPrint('');
debugPrint('[2/3] Testing SHIMMER widget (10 seconds)...');
debugPrint(' Expected: Many frames (continuous animation)');
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Shimmer.fromColors(
baseColor: const Color(0xFF2A2A32),
highlightColor: const Color(0xFF3D3D47),
child: Container(
width: 300,
height: 100,
decoration: BoxDecoration(color: const Color(0xFF2A2A32), borderRadius: BorderRadius.circular(16)),
child: const Center(
child: Text('Processing...', style: TextStyle(color: Colors.white, fontSize: 16)),
),
),
),
),
),
),
);
// DON'T use pumpAndSettle - Shimmer never settles!
await tester.pump();
final shimmerTimings = <FrameTiming>[];
void shimmerCallback(List<FrameTiming> timings) {
shimmerTimings.addAll(timings);
}
WidgetsBinding.instance.addTimingsCallback(shimmerCallback);
// Pump for 10 seconds
for (int i = 0; i < 100; i++) {
await tester.pump(const Duration(milliseconds: 100));
}
WidgetsBinding.instance.removeTimingsCallback(shimmerCallback);
debugPrint(' Shimmer widget frames: ${shimmerTimings.length}');
// === TEST 3: Shimmer with RepaintBoundary ===
debugPrint('');
debugPrint('[3/3] Testing SHIMMER + RepaintBoundary (10 seconds)...');
debugPrint(' Testing if RepaintBoundary reduces CPU impact');
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Center(
child: RepaintBoundary(
child: Shimmer.fromColors(
baseColor: const Color(0xFF2A2A32),
highlightColor: const Color(0xFF3D3D47),
child: Container(
width: 300,
height: 100,
decoration: BoxDecoration(color: const Color(0xFF2A2A32), borderRadius: BorderRadius.circular(16)),
child: const Center(
child: Text('Processing...', style: TextStyle(color: Colors.white, fontSize: 16)),
),
),
),
),
),
),
),
);
// DON'T use pumpAndSettle - Shimmer never settles!
await tester.pump();
final boundaryTimings = <FrameTiming>[];
void boundaryCallback(List<FrameTiming> timings) {
boundaryTimings.addAll(timings);
}
WidgetsBinding.instance.addTimingsCallback(boundaryCallback);
// Pump for 10 seconds
for (int i = 0; i < 100; i++) {
await tester.pump(const Duration(milliseconds: 100));
}
WidgetsBinding.instance.removeTimingsCallback(boundaryCallback);
debugPrint(' Shimmer+Boundary frames: ${boundaryTimings.length}');
// === RESULTS ===
debugPrint('');
debugPrint('╔══════════════════════════════════════════════════════════════╗');
debugPrint('║ TEST RESULTS ║');
debugPrint('╠══════════════════════════════════════════════════════════════╣');
debugPrint('║ Widget Type │ Frames (10s) │ Frames/sec ║');
debugPrint('╠══════════════════════════════════════════════════════════════╣');
debugPrint(
'║ Static (baseline) │ ${staticTimings.length.toString().padLeft(12)} │ ${(staticTimings.length / 10).toStringAsFixed(1).padLeft(10)}/s ║',
);
debugPrint(
'║ Shimmer │ ${shimmerTimings.length.toString().padLeft(12)} │ ${(shimmerTimings.length / 10).toStringAsFixed(1).padLeft(10)}/s ║',
);
debugPrint(
'║ Shimmer+RepaintBoundary │ ${boundaryTimings.length.toString().padLeft(12)} │ ${(boundaryTimings.length / 10).toStringAsFixed(1).padLeft(10)}/s ║',
);
debugPrint('╚══════════════════════════════════════════════════════════════╝');
// Calculate overhead
final shimmerOverhead = shimmerTimings.length - staticTimings.length;
final boundaryOverhead = boundaryTimings.length - staticTimings.length;
debugPrint('');
debugPrint('Analysis:');
debugPrint(' Shimmer overhead: +$shimmerOverhead frames (+${(shimmerOverhead / 10).toStringAsFixed(1)}/s)');
debugPrint(
' RepaintBoundary effect: ${boundaryOverhead < shimmerOverhead ? "Reduced by ${shimmerOverhead - boundaryOverhead} frames" : "No significant reduction"}',
);
// Verify shimmer causes more frames
if (shimmerTimings.length > staticTimings.length * 2) {
debugPrint('');
debugPrint('╔══════════════════════════════════════════════════════════════╗');
debugPrint('║ ⚠️ CONFIRMED: Shimmer causes continuous frame rendering ║');
debugPrint('╚══════════════════════════════════════════════════════════════╝');
}
// Assertions
expect(
shimmerTimings.length,
greaterThan(staticTimings.length),
reason: 'Shimmer should cause more frames than static widget',
);
});
testWidgets('Measure build time impact of Shimmer', (WidgetTester tester) async {
debugPrint('');
debugPrint('╔══════════════════════════════════════════════════════════════╗');
debugPrint('║ SHIMMER BUILD TIME TEST ║');
debugPrint('╚══════════════════════════════════════════════════════════════╝');
debugPrint('');
// Test ProcessingConversationWidget-like structure
debugPrint('[1/2] Testing ProcessingConversationWidget structure...');
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: ListView(
children: [
// Simulate multiple processing conversations
for (int i = 0; i < 3; i++)
Padding(
padding: const EdgeInsets.all(16),
child: RepaintBoundary(
child: Shimmer.fromColors(
baseColor: const Color(0xFF2A2A32),
highlightColor: const Color(0xFF3D3D47),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
width: 24,
height: 24,
decoration: BoxDecoration(
color: const Color(0xFF2A2A32),
borderRadius: BorderRadius.circular(12),
),
),
const SizedBox(width: 8),
Container(
width: 80,
height: 24,
decoration: BoxDecoration(
color: const Color(0xFF35343B),
borderRadius: BorderRadius.circular(16),
),
),
const Spacer(),
Container(
width: 50,
height: 14,
decoration: BoxDecoration(
color: const Color(0xFF2A2A32),
borderRadius: BorderRadius.circular(4),
),
),
],
),
const SizedBox(height: 12),
Container(
width: double.maxFinite,
height: 16,
decoration: BoxDecoration(
color: const Color(0xFF2A2A32),
borderRadius: BorderRadius.circular(4),
),
),
],
),
),
),
),
],
),
),
),
);
// DON'T use pumpAndSettle - Shimmer never settles!
await tester.pump();
final multiShimmerTimings = <FrameTiming>[];
void multiCallback(List<FrameTiming> timings) {
multiShimmerTimings.addAll(timings);
}
WidgetsBinding.instance.addTimingsCallback(multiCallback);
for (int i = 0; i < 100; i++) {
await tester.pump(const Duration(milliseconds: 100));
}
WidgetsBinding.instance.removeTimingsCallback(multiCallback);
debugPrint(' 3x Shimmer widgets: ${multiShimmerTimings.length} frames');
if (multiShimmerTimings.isNotEmpty) {
final buildTimes = multiShimmerTimings.map((t) => t.buildDuration.inMicroseconds).toList()..sort();
final avgBuild = buildTimes.reduce((a, b) => a + b) / buildTimes.length;
final maxBuild = buildTimes.last;
debugPrint(' Avg build time: ${(avgBuild / 1000).toStringAsFixed(2)} ms');
debugPrint(' Max build time: ${(maxBuild / 1000).toStringAsFixed(2)} ms');
}
// === Alternative: Static skeleton ===
debugPrint('');
debugPrint('[2/2] Testing static skeleton alternative...');
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: ListView(
children: [
for (int i = 0; i < 3; i++)
Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
width: 24,
height: 24,
decoration: BoxDecoration(
color: const Color(0xFF2A2A32),
borderRadius: BorderRadius.circular(12),
),
),
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: const Color(0xFF35343B),
borderRadius: BorderRadius.circular(16),
),
child: const Text('Processing...', style: TextStyle(color: Colors.white70, fontSize: 14)),
),
],
),
const SizedBox(height: 12),
Container(
width: double.maxFinite,
height: 16,
decoration: BoxDecoration(
color: const Color(0xFF2A2A32),
borderRadius: BorderRadius.circular(4),
),
),
],
),
),
],
),
),
),
);
await tester.pumpAndSettle();
final staticSkeletonTimings = <FrameTiming>[];
void staticSkeletonCallback(List<FrameTiming> timings) {
staticSkeletonTimings.addAll(timings);
}
WidgetsBinding.instance.addTimingsCallback(staticSkeletonCallback);
for (int i = 0; i < 100; i++) {
await tester.pump(const Duration(milliseconds: 100));
}
WidgetsBinding.instance.removeTimingsCallback(staticSkeletonCallback);
debugPrint(' Static skeleton: ${staticSkeletonTimings.length} frames');
// === SUMMARY ===
debugPrint('');
debugPrint('╔══════════════════════════════════════════════════════════════╗');
debugPrint('║ RECOMMENDATION ║');
debugPrint('╠══════════════════════════════════════════════════════════════╣');
final savings = multiShimmerTimings.length - staticSkeletonTimings.length;
debugPrint('║ Replacing Shimmer with static skeleton would save: ║');
debugPrint('║ ~$savings frames over 10 seconds'.padRight(61) + '║');
debugPrint('║ ~${(savings / 10).toStringAsFixed(0)} frames/second'.padRight(61) + '║');
debugPrint('╚══════════════════════════════════════════════════════════════╝');
expect(
multiShimmerTimings.length,
greaterThan(staticSkeletonTimings.length),
reason: 'Shimmer skeleton should use more frames than static skeleton',
);
});
});
}