Skip to content

Instantly share code, notes, and snippets.

@camallen
Forked from moofkit/config_application.rb
Created July 13, 2025 22:50
Show Gist options
  • Save camallen/317d3f7dc7f2c3f78deb4d2944802c07 to your computer and use it in GitHub Desktop.
Save camallen/317d3f7dc7f2c3f78deb4d2944802c07 to your computer and use it in GitHub Desktop.
Rails track boot time
# config/boot.rb
# frozen_string_literal: true
# Measure boot time
require_relative "../lib/boot_time"
BootTime.instance.store_start_timestamp
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
require "bundler/setup" # Set up gems listed in the Gemfile.
require "bootsnap/setup" # Speed up boot time by caching expensive operations.
# config/initializers/boot_time.rb
# frozen_string_literal: true
Rails.application.config.after_initialize do
boot_time = BootTime.instance.boot_time_elapsed_milliseconds
Rails.logger.info("Boot time: #{boot_time}ms")
end
# lib/boot_time.rb
# frozen_string_literal: true
require "singleton"
class BootTime
include Singleton
attr_reader :start_timestamp
def store_start_timestamp
return if @start_timestamp
@start_timestamp = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
def boot_time_elapsed_milliseconds
return 0.0 unless @start_timestamp
@boot_time_elapsed_milliseconds ||= (time_since_start * 1000).round(2)
end
def reset!
@start_timestamp = nil
@boot_time_elapsed_milliseconds = nil
end
private
def time_since_start
Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start_timestamp
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment