Skip to content

Instantly share code, notes, and snippets.

@palkan
Created January 24, 2022 13:29
Show Gist options
  • Select an option

  • Save palkan/a40e028865dc7bc4ccfb6db2ade9c94f to your computer and use it in GitHub Desktop.

Select an option

Save palkan/a40e028865dc7bc4ccfb6db2ade9c94f to your computer and use it in GitHub Desktop.

Revisions

  1. palkan created this gist Jan 24, 2022.
    41 changes: 41 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    Add the following to `application.rb`:

    ```ruby
    $icallbacks = []
    $icallbacks.define_singleton_method(:print) do
    puts sort_by { |(a, b)| -b }.map { |(a, b)| "#{b}\t\t#{a}" }.join("\n")
    end

    ActiveSupport::Notifications.subscribe("load_config_initializer.railties") do |event|
    $icallbacks << [event.payload[:initializer], event.duration]
    end

    $pcallbacks = []
    $pcallbacks.define_singleton_method(:print) do
    puts sort_by { |(a, b)| -b }.map { |(a, b)| "#{b}\t\t#{a.first}:#{a.last}" }.join("\n")
    end

    require 'active_support/reloader'
    require 'benchmark'

    ActiveSupport::Reloader.singleton_class.prepend(Module.new do
    def to_prepare(*args, &block)
    wrapper = proc do |*bargs|
    result = nil
    time = Benchmark.realtime do
    result = block.call(*bargs)
    end

    $pcallbacks << [block.source_location, time]

    result
    end

    super(*args, &wrapper)
    end
    end)
    ```

    Then, run `rails r 'puts $icallbacks.print'`, etc.

    See also [Bumbler](https://github.com/nevir/Bumbler).