Class: EasyProfiler::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_prof/profile.rb

Overview

Contains global profiling parameters and methods to start and stop profiling.

Constant Summary collapse

@@profile_results =
{}

Class Method Summary collapse

Class Method Details

.reset!Object



57
58
59
# File 'lib/easy_prof/profile.rb', line 57

def self.reset!
  @@profile_results = {}
end

.start(name, config = nil) ⇒ Object

Starts a profiling session.

Parameters:

  • name – session name.

  • options – a Hash of options.

Possible options:

  • :enabled – value indicating whether profiling is enabled.

  • :limit – minimum time period which should be reached to print profiling log.

  • :count_ar_instances —- indicating whether profiler should log an approximate number of instantiated ActiveRecord objects.

  • :count_memory_usage —- indicating whether profiler should log an approximate amount of memory used.

  • :logger – a Logger instance.

  • :colorize_logging – indicating whether profiling log lines should be colorized.

  • :live_logging – indicating whether profiler should flush logs on every checkpoint.

Returns:

  • an instance of profiler (descendant of the EasyProfiler::ProfileInstanceBase class).



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/easy_prof/profile.rb', line 24

def self.start(name, config = nil)
  if @@profile_results[name]
    raise ArgumentError.new("EasyProfiler::Profile.start() collision! '#{name}' is already started.")
  end

  config = Configuration.parse(config)

  klass = config.enabled? ? ProfileInstance : NoProfileInstance
  instance = klass.new(name, config)

  @@profile_results[name] = instance

  # Disable garbage collector to get more precise results
  GC.disable if instance.config.disable_gc?

  instance
end

.stop(name) ⇒ Object

Finishes a profiling session and dumps results to the log.

Parameters:

  • name – session name, used in start method.



46
47
48
49
50
51
52
53
54
55
# File 'lib/easy_prof/profile.rb', line 46

def self.stop(name)
  unless instance = @@profile_results.delete(name)
    raise ArgumentError.new("EasyProfiler::Profile.stop() error! '#{name}' is not started yet.")
  end

  instance.dump_results

  # Enable garbage collector which has been disabled before
  GC.enable if instance.config.disable_gc?
end