Debugging Your First ECS Application¶
In this tutorial, you'll learn how to use the built-in debugging tools to understand and optimize your ECS application.
Prerequisites¶
- Basic familiarity with the ECS library
- A working browser-based project
- About 15 minutes
What You'll Build¶
A simple simulation with intentional performance issues that you'll identify and fix using the debugging tools.
Step 1: Create a World with Stats Overlay¶
Create a new file debug-tutorial.ts:
1 2 3 4 5 6 7 8 9 10 11 12 |
Run your application. You should see the ECS Stats overlay appear.
Checkpoint: The overlay should show: - Frame 0 - Archetypes: 1 - Alive entities: 0
Step 2: Add Some Entities¶
Let's spawn some entities and watch the stats update:
Checkpoint: The overlay should now show: - Archetypes: 2 (empty + Position+Velocity) - Alive entities: 100 - Rows: 100
Step 3: Add a Movement System¶
1 2 3 4 5 6 7 8 |
Step 4: Create the Game Loop¶
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Checkpoint:
- Watch the frame counter increment
- The graph should show blue bars (fast frames)
- frame= should be very low (< 1ms)
Step 5: Introduce a Performance Problem¶
Let's add a "bad" system that creates performance issues:
Checkpoint:
- Watch frame= time increase dramatically
- Red bars should appear in the graph
- Click to see phase timings in the console
Step 6: Identify the Slow System¶
Open your browser's developer console and click the button on the overlay to enable debug logging.
You'll see the output something like:
But we need per-system timing! The overlay shows this when using named functions:
Look at the overlay carefully. With profiling enabled, you'll see which system takes the most time.
Step 7: Fix the Performance Issue¶
Now that we've identified expensiveSystem as the problem, let's optimize it:
Step 8: Verify the Fix¶
After applying the optimization:
Checkpoint:
- frame= time should drop significantly
- Graph should return to mostly blue bars
- Console logging should show lower phase times
Step 9: Explore Archetype Fragmentation¶
Let's intentionally cause archetype fragmentation:
Checkpoint: - Watch the "Archetypes" counter grow - This indicates archetype fragmentation
Step 10: Clean Up¶
Remove the debugging artifacts when going to production:
1 2 3 4 5 |
Summary¶
In this tutorial, you learned how to:
- Create a World with the stats overlay
- Read entity and archetype counts
- Identify slow systems using frame timing
- Enable console debug logging
- Recognize archetype fragmentation
- Disable debugging tools for production
Next Steps¶
- Read the Understanding ECS Debugging & Profiling to understand what the metrics mean
- Check the Debugging & Profiling Reference for all available options
- Explore the How to Debug Your ECS Application for optimization techniques