analyzePerformance method
PerformanceAnalysisReport
analyzePerformance({
- FrameMetricsSnapshot? frameMetrics,
- MemoryMetricsSnapshot? memoryMetrics,
- WidgetMetricsSnapshot? widgetMetrics,
- ImageMetricsSnapshot? imageMetrics,
- ErrorMetricsSnapshot? errorMetrics,
- List<
AppInspectorEvent> ? customEvents,
Comprehensive developer-focused "Why Is My App Slow?" performance evaluation engine.
Implementation
PerformanceAnalysisReport analyzePerformance({
FrameMetricsSnapshot? frameMetrics,
MemoryMetricsSnapshot? memoryMetrics,
WidgetMetricsSnapshot? widgetMetrics,
NavigationMetricsSnapshot? navigationMetrics,
ImageMetricsSnapshot? imageMetrics,
ErrorMetricsSnapshot? errorMetrics,
List<AppInspectorEvent>? customEvents,
}) {
final issues = analyze(
frameMetrics: frameMetrics,
memoryMetrics: memoryMetrics,
widgetMetrics: widgetMetrics,
navigationMetrics: navigationMetrics,
imageMetrics: imageMetrics,
errorMetrics: errorMetrics,
);
final allEvents = customEvents ?? store.getEvents();
// 1. Analyze Network events from store
final networkEvents =
allEvents.where((e) => e.type == InspectorEventType.network).toList();
for (final net in networkEvents) {
final durMs = net.metadata['durationMs'] as num?;
if (durMs != null && durMs > 300) {
final url =
net.name ?? net.metadata['url']?.toString() ?? 'Network Request';
final isCritical = durMs > 800;
issues.add(
PerformanceIssue(
id: 'issue_network_${net.timestamp.millisecondsSinceEpoch}',
title: 'Slow Network Request ($url)',
severity: isCritical
? InspectorSeverity.critical
: InspectorSeverity.warning,
category: InspectorEventType.network,
target: url,
evidence: 'Network request to $url took ${durMs}ms.',
possibleCause:
'High server latency, large response payload, or un-optimized payload deserialization.',
recommendation: Recommendation(
title: 'Optimize Network Request',
steps: [
'Investigate $url endpoint latency.',
'Use payload compression or field projection.',
'Cache response data locally.',
],
),
),
);
}
}
// 2. Analyze Custom Operation events from store
final customOps =
allEvents.where((e) => e.type == InspectorEventType.custom).toList();
for (final op in customOps) {
final durMs = op.metadata['durationMs'] as num?;
if (durMs != null && durMs > 100) {
final name = op.name ?? 'Custom Task';
issues.add(
PerformanceIssue(
id: 'issue_custom_${op.timestamp.millisecondsSinceEpoch}',
title: 'Slow Custom Operation ($name)',
severity: durMs > 300
? InspectorSeverity.critical
: InspectorSeverity.warning,
category: InspectorEventType.custom,
target: name,
evidence: 'Custom operation $name took ${durMs}ms.',
possibleCause: 'Synchronous heavy execution on the UI isolate.',
recommendation: Recommendation(
title: 'Defer Custom Task',
steps: [
'Execute $name inside an Isolate via compute().',
'Break large tasks into smaller async microtasks.',
],
),
),
);
}
}
// Sort issues by severity index descending (critical -> warning -> info)
issues.sort((a, b) => b.severity.index.compareTo(a.severity.index));
// 3. Compute score
final score = calculateScore(
frameMetrics: frameMetrics,
memoryMetrics: memoryMetrics,
widgetMetrics: widgetMetrics,
navigationMetrics: navigationMetrics,
imageMetrics: imageMetrics,
errorMetrics: errorMetrics,
);
// 4. Detect cross-metric correlations
final correlations =
_detectCorrelations(allEvents, widgetMetrics, imageMetrics);
// 5. Build evidence-backed recommended priority
final recommendedPriority = _buildRecommendedPriority(issues, correlations);
// 6. Generate formatted summary text
final formattedSummary = _generateFormattedSummary(
score: score,
issues: issues,
recommendedPriority: recommendedPriority,
);
return PerformanceAnalysisReport(
score: score,
topIssues: List.unmodifiable(issues),
recommendedPriority: List.unmodifiable(recommendedPriority),
correlations: List.unmodifiable(correlations),
formattedSummary: formattedSummary,
);
}