Class: Rack::PerftoolsProfiler::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/perftools_profiler/profiler.rb

Constant Summary collapse

PROFILING_DATA_FILE =
::File.join(self.tmpdir, 'rack_perftools_profiler.prof')
PROFILING_SETTINGS_FILE =
::File.join(self.tmpdir, 'rack_perftools_profiler.config')
DEFAULT_PRINTER =
:text
DEFAULT_MODE =
:cputime
UNSET_FREQUENCY =
-1
DEFAULT_GEMFILE_DIR =
'.'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ Profiler

Returns a new instance of Profiler.



29
30
31
32
33
34
35
36
37
# File 'lib/rack/perftools_profiler/profiler.rb', line 29

def initialize(app, options)
  @printer     = (options.delete(:default_printer) { DEFAULT_PRINTER }).to_sym
  @frequency   = (options.delete(:frequency) { UNSET_FREQUENCY }).to_s
  @mode        = (options.delete(:mode) { DEFAULT_MODE }).to_sym
  @bundler     = (options.delete(:bundler) { false })
  @gemfile_dir = (options.delete(:gemfile_dir) { DEFAULT_GEMFILE_DIR })
  ProfileDataAction.check_printer(@printer)
  raise ProfilerArgumentError, "Invalid option(s): #{options.keys.join(' ')}" unless options.empty?
end

Class Method Details

.clear_dataObject



46
47
48
# File 'lib/rack/perftools_profiler/profiler.rb', line 46

def self.clear_data
  ::File.delete(PROFILING_DATA_FILE) if ::File.exists?(PROFILING_DATA_FILE)
end

.tmpdirObject



16
17
18
19
20
# File 'lib/rack/perftools_profiler/profiler.rb', line 16

def self.tmpdir
  dir = nil
  Dir.chdir Dir.tmpdir do dir = Dir.pwd end # HACK FOR OSX
  dir
end

Instance Method Details

#data(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rack/perftools_profiler/profiler.rb', line 68

def data(options = {})
  printer = (options.fetch('printer') {@printer}).to_sym
  ignore = options.fetch('ignore') { nil }
  focus = options.fetch('focus') { nil }
  if ::File.exists?(PROFILING_DATA_FILE)
    args = "--#{printer}"
    args += " --ignore=#{ignore}" if ignore
    args += " --focus=#{focus}" if focus
    cmd = "pprof.rb #{args} #{PROFILING_DATA_FILE}"
    cmd = "bundle exec " + cmd if @bundler
    stdout, stderr, status = Dir.chdir(@gemfile_dir) { run(cmd) }
    if(status == 0)
      [printer, stdout]
    else
      raise ProfilingError.new("Running the command '#{cmd}' exited with status #{status}", stderr)
    end
  else
    [:none, nil]
  end
end

#profileObject



39
40
41
42
43
44
# File 'lib/rack/perftools_profiler/profiler.rb', line 39

def profile
  start
  yield
ensure
  stop
end

#profiling?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/rack/perftools_profiler/profiler.rb', line 62

def profiling?
  pstore_transaction(true) do |store|
    store[:profiling?]
  end
end

#startObject



50
51
52
53
54
# File 'lib/rack/perftools_profiler/profiler.rb', line 50

def start
  set_env_vars
  PerfTools::CpuProfiler.start(PROFILING_DATA_FILE)
  self.profiling = true
end

#stopObject



56
57
58
59
60
# File 'lib/rack/perftools_profiler/profiler.rb', line 56

def stop
  PerfTools::CpuProfiler.stop
  self.profiling = false
  unset_env_vars
end