Skip to content

Feature Request: Performance Insights Tool - Automated N+1 and Slow Query Detection #660

@siefhesham-boca

Description

@siefhesham-boca

Problem Statement

As Laravel developers, we frequently encounter performance bottlenecks that are difficult to spot during development without external tools. Common issues include:

  • N+1 queries that slip through code reviews
  • Slow database queries that degrade user experience
  • Excessive query counts per request that impact scalability
  • Memory usage issues that surface under load

Currently, detecting these requires:

  • Installing and configuring Laravel Telescope or Debugbar
  • Manually reviewing logs
  • Running performance profilers
  • Discovering issues late in development or production

Proposed Solution

Add a new Performance Insights Tool to Laravel Boost that provides automated performance analysis directly within the AI development workflow.

Capabilities

1. N+1 Query Detection

// Analyzes logs to detect patterns like:
// SELECT * FROM users WHERE id = 1
// SELECT * FROM posts WHERE user_id = 1
// SELECT * FROM posts WHERE user_id = 2
// SELECT * FROM posts WHERE user_id = 3
// ... repeated queries

// Suggests: User::with('posts')->get()

2. Slow Query Analysis

  • Identifies queries exceeding configurable thresholds (default: 100ms)
  • Reports execution time, query content, and file location
  • Prioritizes by impact (frequency × duration)

3. Query Count Monitoring

  • Tracks total queries per endpoint/request
  • Warns on excessive counts (e.g., >50 queries)
  • Compares against application baselines

4. Memory Usage Insights

  • Reports peak memory usage from logs
  • Identifies memory-intensive operations
  • Tracks trends over time

Tool Schema

#[Description('Analyze application performance and detect issues like N+1 queries, slow queries, and memory usage.')]
class PerformanceInsightsTool extends Tool
{
    public function schema(JsonSchema $schema): array
    {
        return [
            'type' => $schema->string()
                ->enum(['n+1', 'slow-queries', 'query-count', 'memory'])
                ->description('Type of performance analysis to run')
                ->required(),

            'limit' => $schema->integer()
                ->description('Number of entries to analyze')
                ->default(10),

            'threshold' => $schema->integer()
                ->description('Threshold for slow query detection (ms)')
                ->default(100),
        ];
    }
}

Benefits

For Developers

  • ✅ Proactive issue detection during development
  • ✅ Reduced debugging time
  • ✅ Learn performance best practices through suggestions
  • ✅ No additional tool installation needed

For Laravel Boost

  • ✅ Natural fit with existing capabilities (database-query, read-log-entries, last-error)
  • ✅ Differentiates Boost from generic AI coding tools
  • ✅ Addresses a real pain point in Laravel development
  • ✅ Aligns with Laravel's philosophy of developer happiness

For AI Agents

  • ✅ Can proactively suggest optimizations
  • ✅ Provides concrete, actionable insights
  • ✅ Improves code quality automatically

Implementation Considerations

Architecture

  • Leverage existing ReadLogEntries tool for log parsing
  • Integrate with DatabaseQuery for query analysis
  • Follow established MCP tool patterns
  • Maintain read-only, non-destructive operations

Parsing Strategies

  • Regex patterns for common N+1 signatures
  • Laravel query log format parsing
  • Configurable thresholds per project
  • Support for custom log formats

Output Format

{
  "type": "n+1_detected",
  "severity": "high",
  "location": "app/Http/Controllers/UserController.php:42",
  "queries": 150,
  "suggestion": "Use eager loading: User::with('posts', 'comments')->get()",
  "example": "// Current: N+1 pattern\n// Suggested: Eager loading"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions