lib/stackprofiler/web_ui.rb in stackprofiler-0.0.2 vs lib/stackprofiler/web_ui.rb in stackprofiler-0.0.3

- old
+ new

@@ -1,7 +1,9 @@ module Stackprofiler class WebUI < Sinatra::Base + use Rack::Deflater + helpers Sinatra::ContentFor set :views, proc { File.join(root, 'web_ui', 'views') } set :public_folder, proc { File.join(root, 'web_ui', 'public') } def initialize(options={}) @@ -17,25 +19,15 @@ super end configure :development do - # require 'better_errors' - # use BetterErrors::Middleware - # BetterErrors.application_root = __dir__ + require 'better_errors' + use BetterErrors::Middleware + BetterErrors.application_root = __dir__ end - before do - puts "starting #{request.path}" - @start_req = Time.now - end - - after do - duration = Time.now - @start_req - puts "finished #{request.path} (#{duration})" - end - get '/' do @runs = RunDataSource.runs @run_id = params[:run_id].try(:to_i) || (@runs.count - 1) erb :index end @@ -46,10 +38,17 @@ objs = ObjectSpace.reachable_objects_from run.profile objs.reduce(0) {|size, obj| size + ObjectSpace::memsize_of(obj) } end.to_json end + get '/dump' do + content_type 'application/json' + run_id = params[:run_id].to_i + run = RunDataSource.runs[run_id] + Oj.dump run + end + get '/frames' do content_type 'application/json' run_id = params[:run_id].to_i run = RunDataSource.runs[run_id] run.profile[:frames].sort_by {|addr, frame| frame[:samples] }.to_json @@ -68,11 +67,11 @@ halt 404 if frame.nil? @file, @first_line = frame.values_at :file, :line @first_line ||= 1 - @source = MethodSource::source_helper([@file, @first_line]).strip_heredoc + @source = run.code_cache.source_helper([@file, @first_line]).strip_heredoc @output = CodeRay.scan(@source, :ruby).div(wrap: nil).lines.map.with_index do |code, idx| line_index = idx + @first_line samples = frame[:lines][line_index] || [] {code: code, samples: samples.join('/') } end @@ -83,32 +82,33 @@ get '/save' do run_id = params[:run_id].to_i run = RunDataSource.runs[run_id] content_type 'application/octet-stream' - Marshal.dump(run.profile) - # Oj.dump(run) + # Marshal.dump(run.profile) + Oj.dump(run) end get '/frame_names' do - name = params[:term] + name = params[:term].downcase run_id = params[:run_id].to_i run = RunDataSource.runs[run_id] frames = run.profile[:frames] - matching = frames.select {|addr, f| f[:name].include? name } + matching = frames.select {|addr, f| f[:name].downcase.include? name } results = matching.map {|addr, f| f[:name] } content_type 'application/json' Oj.dump(results, mode: :compat) end post '/receive' do data = request.body.read - json = Oj.load(data) - run = Run.new 'unknown', json, Time.now + json = Marshal.load(data) + name = json[:name] || 'unknown' + run = Run.new name, json, Time.now RunDataSource.runs << run # if they sent us a profile, they probably changed something and want that reflected # todo: remove hack MethodSource::instance_variable_get(:@lines_for_file).try(:clear) @@ -119,12 +119,15 @@ get '/gem_breakdown' do run_id = params[:run_id].to_i run = RunDataSource.runs[run_id] + breakdown = run.gem_breakdown + breakdown['(gc)'] = run.profile[:gc_samples] + content_type 'application/json' - run.gem_breakdown.to_json + breakdown.to_json end post '/json' do json_params = Oj.strict_load(request.body.read, nil) params.merge!(json_params) @@ -153,10 +156,10 @@ elsif opts klass.new(opts) end end.compact - filtered = filters.reduce(run) {|memo, filter| filter.filter(memo, frames) } + filtered = filters.reduce(run) {|memo, filter| filter.filter(memo, run) } content_type 'application/json' Oj.dump(filtered, mode: :compat) end end