Class: Rack::PerftoolsProfiler::Profiler
- Inherits:
-
Object
- Object
- Rack::PerftoolsProfiler::Profiler
- 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
- #accepts?(password) ⇒ Boolean
- #data(options = {}) ⇒ Object
-
#initialize(app, options) ⇒ Profiler
constructor
A new instance of Profiler.
- #password_protected? ⇒ Boolean
- #password_valid?(password) ⇒ Boolean
- #profile(mode = nil) ⇒ Object
- #profiling? ⇒ Boolean
- #start(mode = nil) ⇒ Object
- #stop ⇒ Object
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, ) @printer = (.delete(:default_printer) { DEFAULT_PRINTER }).to_sym @frequency = (.delete(:frequency) { UNSET_FREQUENCY }).to_s @mode = (.delete(:mode) { DEFAULT_MODE }).to_sym @bundler = .delete(:bundler) { false } @gemfile_dir = .delete(:gemfile_dir) { DEFAULT_GEMFILE_DIR } @password = .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): #{.keys.join(' ')}" unless .empty? end |
Class Method Details
.clear_data ⇒ Object
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 |
.tmpdir ⇒ Object
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
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/rack/perftools_profiler/profiler.rb', line 100 def data( = {}) printer = (.fetch('printer') {@printer}).to_sym ignore = .fetch('ignore') { nil } focus = .fetch('focus') { nil } nodecount = .fetch('nodecount') { nil } nodefraction = .fetch('nodefraction') { nil } if ::File.exists?(PROFILING_DATA_FILE) args = ["--#{printer}"] args << "--ignore=#{ignore}" if ignore args << "--focus=#{focus}" if focus args << "--nodecount=#{nodecount}" if nodecount args << "--nodefraction=#{nodefraction}" if nodefraction args << PROFILING_DATA_FILE cmd = ["pprof.rb"] + args cmd = ["bundle", "exec"] + cmd if @bundler stdout, stderr, status = Dir.chdir(@gemfile_dir) { run(*cmd) } if status!=0 raise ProfilingError.new("Running the command '#{cmd.join(" ")}' exited with status #{status}", stderr) elsif stdout.length == 0 && stderr.length > 0 raise ProfilingError.new("Running the command '#{cmd.join(" ")}' failed to generate a file", stderr) else [printer, stdout] end else [:none, nil] end end |
#password_protected? ⇒ Boolean
70 71 72 |
# File 'lib/rack/perftools_profiler/profiler.rb', line 70 def password_protected? @password != :not_set end |
#password_valid?(password) ⇒ 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
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 |
#stop ⇒ Object
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 |