Blackfire Profiling
Install Blackfire
# Install Blackfire agent
curl -fsSL https://get.blackfire.io | sudo bash
# Install PHP probe
blackfire agent:install
# Verify installation
blackfire doctor
Profile with Blackfire
# Profile a URL
blackfire curl https://magento.example.com/
# Profile with custom options
blackfire --title="Homepage" curl https://magento.example.com/
# Compare two profiles
blackfire compare profile1.prof profile2.prof
Blackfire in Code
Blackfire\Profile::start();
// Code to profile
$product = $this->productRepository->get('TEST-001');
$products = $this->productCollectionFactory->create()->load();
Blackfire\Profile::stop();
Key Points
- Blackfire shows wall time, CPU, memory, I/O
- Compare profiles before/after optimization
- Use thresholds to detect regressions
- Integration with CI/CD for automated profiling
Xdebug Profiler
Enable Profiling
; php.ini
[xdebug]
xdebug.mode=profile
xdebug.output_dir=/tmp/xdebug
xdebug.profiler_output_name=profile.%p-%t
Analyze with KCacheGrind
# Convert to callgrind format
php vendor/bin/pprof --convert callgrind /tmp/xdebug/profile.xdebug
# Open in KCacheGrind
kcachegrind /tmp/xdebug/profile.xdebug
# Or use QCachegrind (macOS)
qcacheegrind /tmp/xdebug/profile.xdebug
Key Metrics
- Self time: time spent in function itself
- Inclusive time: time including sub-calls
- Call count: number of times function called
- Memory: peak memory usage
Key Points
- Xdebug profiler is free and built-in
- Shows function-level performance data
- Use callgrind format for visualization
- Disable in production (performance impact)
Flame Graphs
Generate Flame Graphs
# Using phpspy
php vendor/bin/phprof /tmp/xdebug/profile.xdebug > /tmp/stacks.txt
# Generate flame graph
./flamegraph.pl /tmp/stacks.txt > /tmp/flamegraph.svg
# Open in browser
open /tmp/flamegraph.svg
Read Flame Graphs
- X-axis: percentage of time
- Y-axis: call stack depth
- Wide blocks = more time spent
- Color: random (not meaningful)
Key Points
- Flame graphs visualize call stack overhead
- Wide blocks indicate optimization targets
- Use for identifying hot paths
- Interactive in browser (zoom, search)
Identifying Bottlenecks
Common Bottlenecks
// 1. Database queries
// - N+1 queries
// - Missing indexes
// - Slow queries
// 2. Memory usage
// - Large collections
// - Memory leaks
// - Unbounded data loading
// 3. CPU-intensive code
// - Complex calculations
// - String processing
// - Regex operations
// 4. I/O operations
// - File system access
// - External API calls
// - Network requests
Profiling Workflow
// 1. Identify hot path
// 2. Measure baseline
// 3. Optimize
// 4. Measure improvement
// 5. Repeat
use Magento\Framework\Profiler;
Profiler::start('optimization_test');
// ... code ...
Profiler::stop('optimization_test');
Key Points
- Profile before optimizing
- Focus on biggest bottlenecks first
- Measure impact of each change
- Avoid premature optimization
Practice Problems
0 / 1 solved
Profile and Optimize
Profile a slow page and optimize the biggest bottleneck.
Solution
// Profile shows N+1 queries in loop
// Fix: Use JOIN to fetch categories
$collection = $this->productCollectionFactory->create();
$collection->getSelect()->join(
['c' => 'catalog_category_entity'],
'main_table.category_id = c.entity_id',
['category_name' => 'name']
);
$collection->load();
// Re-profile to verify improvement Quiz
1. What does Blackfire show?
2. What does a wide block in flame graph mean?
3. When should you profile?
4. What is self time?
Flashcards
Question
Blackfire benefit?
Click to reveal answer
Answer
Comprehensive profiling: time, CPU, memory, I/O
Question
Flame graph wide block?
Click to reveal answer
Answer
Function takes more time to execute
Question
Self time vs inclusive time?
Click to reveal answer
Answer
Self: function only; Inclusive: including sub-calls
Question
Profiling workflow?
Click to reveal answer
Answer
Measure baseline, optimize, measure again
Revision Notes
Key Takeaways
- 1. Blackfire provides comprehensive PHP profiling
- 2. Flame graphs visualize call stack overhead
- 3. Profile before and after optimization
- 4. Focus on biggest bottlenecks first
Interview Tips
- • Explain how to identify performance bottlenecks
- • Discuss profiling tools and when to use each
- • Know how to interpret profiling data
Cheat Sheet
PHP Profiling
- Blackfire: comprehensive profiling
- Xdebug: free, callgrind format
- Flame graphs: visualize call stacks
- Focus: self time, wide blocks