module CQL::Performance

Overview

Example usage:

# Quick setup
CQL::Performance.enable_development_mode!

# Or custom configuration
CQL::Performance.configure do |config|
  config.monitoring.enabled = true
  config.profiling.slow_query_threshold = 50.milliseconds
  config.detection.threshold = 3
  config.logging.colorize = true
end

# Every query execution is monitored once monitoring is enabled
users = AcmeDB.query.from(:users).where(active: true).all(User)

# Track with context for better insights
CQL::Performance.with_context("UserController#index") do
  users = AcmeDB.query.from(:users)
    .where(active: true)
    .order(created_at: :desc)
    .limit(20)
    .all(User)
end

# Manual tracking for SQL that CQL does not run itself
name = CQL::Performance.track("SELECT name FROM users WHERE id = ?", [1] of DB::Any) do
  AcmeDB.with_connection { |connection| connection.query_one("SELECT name FROM users WHERE id = ?", 1, as: String) }
end

# Generate performance report
puts CQL::Performance.report("console")

Defined in:

performance.cr
performance/config.cr
performance/interfaces.cr
performance/monitor.cr
performance/n_plus_one_detector.cr
performance/performance_metrics.cr
performance/query_profiler.cr
performance/sql_formatter.cr
performance/unified_report_generator.cr
performance/utilities.cr

Class Method Summary

Class Method Detail

def self.configure(&) #

[View source]
def self.create_detector(config : Config::Detection = Config::Detection.new) #

[View source]
def self.create_profiler(config : Config::Profiling = Config::Profiling.new) #

Make concrete implementations available as shortcuts


[View source]
def self.critical_issues : Array(Issue) #

[View source]
def self.development_config : Config #

Export config presets for easy use


[View source]
def self.disable! #

[View source]
def self.disable_sql_logging #

[View source]
def self.enable_development_mode! #

Quick setup methods


[View source]
def self.enable_monitoring #

Monitoring is opt-in; this turns on the global monitor.


[View source]
def self.enable_production_mode! #

[View source]
def self.enable_sql_logging(colorize : Bool = true, pretty : Bool = true) #

SQL logging convenience. SQL is logged by the monitor, so this enables monitoring too.


[View source]
def self.end_relation_loading #

[View source]
def self.export_metrics_as_json : String #

[View source]
def self.flush(timeout : Time::Span | Nil = nil) : Bool #

Waits until the monitor has recorded every statement handed to it; false on timeout. Pass a timeout at shutdown.


[View source]
def self.healthy? : Bool #

[View source]
def self.high_priority_issues : Array(Issue) #

[View source]
def self.is_healthy? : Bool #

[View source]
def self.metrics : PerformanceMetrics #

Performance Metrics convenience methods


[View source]
def self.metrics_summary : Hash(String, String | Int64 | Float64) #

[View source]
def self.minimal_config : Config #

[View source]
def self.monitor : Monitor #

[View source]
def self.monitor=(monitor : Monitor) #

Replacing the global monitor closes the old one, whose later statements are recorded inline.


[View source]
def self.production_config : Config #

[View source]
def self.report(format : String | Nil = nil) : String #

[View source]
def self.sql_formatter : SQLFormatter #

[View source]
def self.sql_formatter=(formatter : SQLFormatter) #

[View source]
def self.start_relation_loading(relation : String, model : String) #

[View source]
def self.test_config : Config #

[View source]
def self.track(sql : String, params : Array(DB::Any) = [] of DB::Any, &) #

Convenience methods


[View source]
def self.with_context(context : String, &) #

[View source]