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
MODES =
[:cputime, :methods, :objects, :walltime]
DEFAULT_MODE =
:cputime
CHANGEABLE_MODES =
[:methods, :objects]
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.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rack/perftools_profiler/profiler.rb', line 31

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 }
  @password    = options.delete(:password) { :not_set }
  @mode_for_request = nil
  ProfileDataAction.check_printer(@printer)
  ensure_mode_is_valid(@mode)
  # We need to set the enviroment variables before loading perftools
  set_env_vars
  require 'perftools'
  raise ProfilerArgumentError, "Invalid option(s): #{options.keys.join(' ')}" unless options.empty?
end

Class Method Details

.clear_dataObject



54
55
56
# File 'lib/rack/perftools_profiler/profiler.rb', line 54

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

#accepts?(password) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/rack/perftools_profiler/profiler.rb', line 58

def accepts?(password)
  if password_protected?
    password_valid?(password)
  else
    true
  end
end

#data(options = {}) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/rack/perftools_profiler/profiler.rb', line 100

def data(options = {})
  if ::File.exists?(PROFILING_DATA_FILE)
    data_from_file(options)
  else
    [:none, nil]
  end
end

#password_protected?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rack/perftools_profiler/profiler.rb', line 70

def password_protected?
  @password != :not_set
end

#password_valid?(password) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rack/perftools_profiler/profiler.rb', line 66

def password_valid?(password)
  @password.nil? || password == @password
end

#profile(mode = nil) ⇒ Object



47
48
49
50
51
52
# File 'lib/rack/perftools_profiler/profiler.rb', line 47

def profile(mode = nil)
  start(mode)
  yield
ensure
  stop
end

#profiling?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/rack/perftools_profiler/profiler.rb', line 94

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

#start(mode = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rack/perftools_profiler/profiler.rb', line 74

def start(mode = nil)
  ensure_mode_is_changeable(mode) if mode
  PerfTools::CpuProfiler.stop
  if (mode)
    @mode_for_request = mode
  end
  unset_env_vars
  set_env_vars
  PerfTools::CpuProfiler.start(PROFILING_DATA_FILE)
  self.profiling = true
ensure
  @mode_for_request = nil
end

#stopObject



88
89
90
91
92
# File 'lib/rack/perftools_profiler/profiler.rb', line 88

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