Class: Wildsight::Profiler::Profile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent) ⇒ Profile

Returns a new instance of Profile.



44
45
46
47
48
49
50
# File 'lib/wildsight/profiler/profiler.rb', line 44

def initialize(name, parent)
  @name = name
  @parent = parent
  @subtimes = []
  @result = nil
  @exception = nil
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



42
43
44
# File 'lib/wildsight/profiler/profiler.rb', line 42

def options
  @options
end

Instance Method Details

#<<(time) ⇒ Object



80
81
82
# File 'lib/wildsight/profiler/profiler.rb', line 80

def <<(time)
  @subtimes << time
end

#closeObject



67
68
69
70
# File 'lib/wildsight/profiler/profiler.rb', line 67

def close
  @parent << raw if @parent
  @parent
end

#durationObject



88
89
90
91
# File 'lib/wildsight/profiler/profiler.rb', line 88

def duration
  return raw if options[:raw]
  return raw - subtimes
end

#exceptionObject



76
77
78
# File 'lib/wildsight/profiler/profiler.rb', line 76

def exception
  @exception
end

#exportObject



97
98
99
# File 'lib/wildsight/profiler/profiler.rb', line 97

def export
  { before: @before, after: @after, duration: duration, raw: raw }
end

#profile!(&block) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/wildsight/profiler/profiler.rb', line 57

def profile!(&block)
  @before = DateTime.now.to_f
  begin
    @result = block.call
  rescue Exception => e
    @exception = e
  end
  @after = DateTime.now.to_f
end

#rawObject



93
94
95
# File 'lib/wildsight/profiler/profiler.rb', line 93

def raw
  (@after - @before) * 1000
end

#resultObject



72
73
74
# File 'lib/wildsight/profiler/profiler.rb', line 72

def result
  @result
end

#set(before, after) ⇒ Object



52
53
54
55
# File 'lib/wildsight/profiler/profiler.rb', line 52

def set(before, after)
  @before = before
  @after = after
end

#subtimesObject



84
85
86
# File 'lib/wildsight/profiler/profiler.rb', line 84

def subtimes
  @subtimes.inject(0.0) { |c,x| c + x }
end