Skip to content

Instantly share code, notes, and snippets.

@andreas
Created April 22, 2013 07:14
Show Gist options
  • Save andreas/5432990 to your computer and use it in GitHub Desktop.
Save andreas/5432990 to your computer and use it in GitHub Desktop.

Revisions

  1. Andreas created this gist Apr 22, 2013.
    33 changes: 33 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    class RBLineProfiler
    WALL_TIME_LIMIT = 10e3 # ms

    def initialize(app)
    @app = app
    end

    def call(env)
    result = nil
    profile = lineprof(/./) { result = @app.call(env) }

    profile.each do |file, line_samples|
    file_lines = nil

    line_samples.each_with_index do |(wall_time, calls), num|
    if wall_time > WALL_TIME_LIMIT
    if !file_lines
    file_lines = File.readlines(file)
    puts file
    end

    printf "% 8.1fms | %s", wall_time/1000.0, file_lines[num-1]
    end
    end
    end

    result
    end
    end

    # Add to config/application.rb

    config.middleware.use RBLineProfiler