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
- DEFAULT_MODE =
:cputime
- UNSET_FREQUENCY =
-1
- DEFAULT_GEMFILE_DIR =
'.'
Class Method Summary collapse
Instance Method Summary collapse
- #data(options = {}) ⇒ Object
-
#initialize(app, options) ⇒ Profiler
constructor
A new instance of Profiler.
- #profile ⇒ Object
- #profiling? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
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, ) @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 }) ProfileDataAction.check_printer(@printer) raise ProfilerArgumentError, "Invalid option(s): #{.keys.join(' ')}" unless .empty? end |
Class Method Details
.clear_data ⇒ Object
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 |
.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
#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( = {}) printer = (.fetch('printer') {@printer}).to_sym ignore = .fetch('ignore') { nil } focus = .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 |
#profile ⇒ Object
39 40 41 42 43 44 |
# File 'lib/rack/perftools_profiler/profiler.rb', line 39 def profile start yield ensure stop end |
#profiling? ⇒ 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 |
#start ⇒ Object
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 |
#stop ⇒ Object
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 |