forked from scratchfoundation/scratch-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-render-analysis.js
More file actions
228 lines (200 loc) · 7.39 KB
/
debug-render-analysis.js
File metadata and controls
228 lines (200 loc) · 7.39 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
/**
* Render Analysis Debug Script
*
* This script can be pasted into the browser console to help analyze
* component render patterns and detect potential infinite renders.
*
* Usage:
* 1. Open browser DevTools (F12)
* 2. Go to Console tab
* 3. Paste this entire script and press Enter
* 4. Use the application normally
* 5. Call analyzeRenders() to see the analysis
*/
// Global variables to track renders
window.renderTracker = {
logs: [],
startTime: Date.now(),
componentCounts: {},
lastRenderTimes: {},
suspiciousPatterns: [],
};
// Override console.log to capture our render logs
const originalConsoleLog = console.log;
console.log = function (...args) {
// Call original console.log first
originalConsoleLog.apply(console, args);
// Check if this is one of our render logs
const message = args[0];
if (
typeof message === "string" &&
(message.includes("🔄 GUIComponent RENDER:") ||
message.includes("🧱 Blocks") ||
message.includes("🎭 StageWrapperComponent RENDER:") ||
message.includes("🎯 TargetPane"))
) {
const timestamp = Date.now();
const logEntry = {
timestamp,
message,
data: args[1] || {},
component: extractComponentName(message),
};
window.renderTracker.logs.push(logEntry);
// Track component render counts
const component = logEntry.component;
window.renderTracker.componentCounts[component] =
(window.renderTracker.componentCounts[component] || 0) + 1;
// Check for rapid re-renders (potential infinite renders)
if (window.renderTracker.lastRenderTimes[component]) {
const timeDiff =
timestamp - window.renderTracker.lastRenderTimes[component];
if (timeDiff < 100) {
// Less than 100ms between renders
window.renderTracker.suspiciousPatterns.push({
component,
timeDiff,
timestamp,
message: `Rapid re-render detected: ${component} rendered ${timeDiff}ms after previous render`,
});
}
}
window.renderTracker.lastRenderTimes[component] = timestamp;
}
};
function extractComponentName(message) {
if (message.includes("🔄 GUIComponent")) return "GUIComponent";
if (message.includes("🧱 Blocks")) return "Blocks";
if (message.includes("🎭 StageWrapperComponent"))
return "StageWrapperComponent";
if (message.includes("🎯 TargetPane")) return "TargetPane";
return "Unknown";
}
// Analysis functions
window.analyzeRenders = function () {
const tracker = window.renderTracker;
const totalTime = Date.now() - tracker.startTime;
console.group("🔍 RENDER ANALYSIS REPORT");
console.log(`📊 Analysis Period: ${(totalTime / 1000).toFixed(2)} seconds`);
console.log(`📝 Total Render Logs: ${tracker.logs.length}`);
console.group("📈 Component Render Counts");
Object.entries(tracker.componentCounts)
.sort(([, a], [, b]) => b - a)
.forEach(([component, count]) => {
const rate = (count / (totalTime / 1000)).toFixed(2);
console.log(`${component}: ${count} renders (${rate} renders/sec)`);
});
console.groupEnd();
if (tracker.suspiciousPatterns.length > 0) {
console.group("⚠️ SUSPICIOUS PATTERNS (Potential Infinite Renders)");
tracker.suspiciousPatterns.forEach((pattern) => {
console.warn(pattern.message);
});
console.groupEnd();
} else {
console.log("✅ No suspicious rapid re-render patterns detected");
}
console.group("🕐 Recent Renders (Last 10)");
tracker.logs.slice(-10).forEach((log) => {
const timeFromStart = (
(log.timestamp - tracker.startTime) /
1000
).toFixed(2);
console.log(`[${timeFromStart}s] ${log.message}`, log.data);
});
console.groupEnd();
console.groupEnd();
return {
totalRenders: tracker.logs.length,
componentCounts: tracker.componentCounts,
suspiciousPatterns: tracker.suspiciousPatterns,
analysisTime: totalTime,
};
};
window.clearRenderTracking = function () {
window.renderTracker = {
logs: [],
startTime: Date.now(),
componentCounts: {},
lastRenderTimes: {},
suspiciousPatterns: [],
};
console.log("🧹 Render tracking data cleared");
};
window.getRendersByComponent = function (componentName) {
return window.renderTracker.logs.filter(
(log) => log.component === componentName
);
};
window.getRecentRenders = function (seconds = 10) {
const cutoff = Date.now() - seconds * 1000;
return window.renderTracker.logs.filter((log) => log.timestamp > cutoff);
};
// Utility function to detect useEffect dependency issues
window.detectDependencyIssues = function () {
const recentLogs = window.getRecentRenders(30);
const guiRenders = recentLogs.filter(
(log) => log.component === "GUIComponent"
);
console.group("🔍 DEPENDENCY ANALYSIS");
if (guiRenders.length > 5) {
console.warn(
`⚠️ GUIComponent rendered ${guiRenders.length} times in the last 30 seconds`
);
console.log("This might indicate useEffect dependency issues");
// Check for useEffect logs
const useEffectLogs = recentLogs.filter((log) =>
log.message.includes("useEffect")
);
if (useEffectLogs.length > 0) {
console.group("🔄 Recent useEffect executions:");
useEffectLogs.forEach((log) => {
console.log(log.message, log.data);
});
console.groupEnd();
}
} else {
console.log("✅ GUIComponent render frequency looks normal");
}
console.groupEnd();
};
// Auto-analysis every 30 seconds
let autoAnalysisInterval;
window.startAutoAnalysis = function () {
if (autoAnalysisInterval) {
clearInterval(autoAnalysisInterval);
}
autoAnalysisInterval = setInterval(() => {
const suspiciousCount = window.renderTracker.suspiciousPatterns.length;
if (suspiciousCount > 0) {
console.warn(
`🚨 AUTO-ANALYSIS: ${suspiciousCount} suspicious render patterns detected!`
);
window.analyzeRenders();
}
}, 30000);
console.log("🤖 Auto-analysis started (runs every 30 seconds)");
};
window.stopAutoAnalysis = function () {
if (autoAnalysisInterval) {
clearInterval(autoAnalysisInterval);
autoAnalysisInterval = null;
console.log("🛑 Auto-analysis stopped");
}
};
// Initialize
console.log("🚀 Render Analysis Debug Script Loaded!");
console.log("Available functions:");
console.log(" - analyzeRenders() - Show detailed analysis");
console.log(" - clearRenderTracking() - Clear tracking data");
console.log(
" - getRendersByComponent(name) - Get renders for specific component"
);
console.log(" - getRecentRenders(seconds) - Get recent renders");
console.log(" - detectDependencyIssues() - Check for useEffect issues");
console.log(" - startAutoAnalysis() - Start automatic monitoring");
console.log(" - stopAutoAnalysis() - Stop automatic monitoring");
console.log("");
console.log(
"💡 Tip: Use the application normally, then call analyzeRenders() to see the results"
);