Class: Sqreen::Graft::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/sqreen/graft/call.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, &block) ⇒ Timer

Returns a new instance of Timer.



105
106
107
108
109
110
# File 'lib/sqreen/graft/call.rb', line 105

def initialize(tag, &block)
  @tag = tag
  @block = block
  @tally = 0
  @size = 0
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



103
104
105
# File 'lib/sqreen/graft/call.rb', line 103

def size
  @size
end

#tagObject (readonly)

Returns the value of attribute tag.



103
104
105
# File 'lib/sqreen/graft/call.rb', line 103

def tag
  @tag
end

Class Method Details

.readObject



99
100
101
# File 'lib/sqreen/graft/call.rb', line 99

def self.read
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Instance Method Details

#durationObject

Raises:



118
119
120
121
122
# File 'lib/sqreen/graft/call.rb', line 118

def duration
  raise(TimerError, 'Timer#duration when running') if @size.odd?

  @tally
end

#elapsedObject

Raises:



112
113
114
115
116
# File 'lib/sqreen/graft/call.rb', line 112

def elapsed
  raise(TimerError, 'Timer#elapsed when paused') if @size.even?

  @tally + Timer.read
end

#ignoreObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/sqreen/graft/call.rb', line 124

def ignore
  raise(TimerError, 'Timer#ignore when paused') if @size.even?

  @size += 1
  @tally += Timer.read
  yield(self)
ensure
  @size += 1
  @tally -= Timer.read
end

#include_measurements(another_timer) ⇒ Object



198
199
200
# File 'lib/sqreen/graft/call.rb', line 198

def include_measurements(another_timer)
  @blips += another_timer.instance_variable_get(:@blips)
end

#measure(opts = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sqreen/graft/call.rb', line 135

def measure(opts = nil)
  raise(TimerError, 'Timer#measure when running') if @size.odd?

  now = Timer.read

  ignore = opts[:ignore] if opts
  if ignore
    ignore.size += 1
    ignore.tally += now
  end

  @size += 1
  @tally -= now

  yield(self)
ensure
  now = Timer.read

  if ignore
    ignore.size += 1
    ignore.tally -= now
  end

  @size += 1
  @tally += now

  @block.call(self) if @block
end

#paused?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/sqreen/graft/call.rb', line 194

def paused?
  @size.even?
end

#running?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/sqreen/graft/call.rb', line 190

def running?
  @size.odd?
end

#start(at = Timer.read) ⇒ Object

Raises:



164
165
166
167
168
169
170
171
# File 'lib/sqreen/graft/call.rb', line 164

def start(at = Timer.read)
  raise(TimerError, 'Timer#start when started') unless @size.even?

  @size += 1
  @tally -= at

  at
end

#start_and_endObject



202
203
204
205
# File 'lib/sqreen/graft/call.rb', line 202

def start_and_end
  raise 'Not exactly two measurements recorded' unless size == 2
  @blips
end

#started?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/sqreen/graft/call.rb', line 182

def started?
  @size != 0 && @size.odd?
end

#stop(at = Timer.read) ⇒ Object

Raises:



173
174
175
176
177
178
179
180
# File 'lib/sqreen/graft/call.rb', line 173

def stop(at = Timer.read)
  raise(TimerError, 'Timer#stop when unstarted') unless @size.odd?

  @size += 1
  @tally += at

  at
end

#stopped?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/sqreen/graft/call.rb', line 186

def stopped?
  @size != 0 && @size.even?
end

#to_sObject



207
208
209
# File 'lib/sqreen/graft/call.rb', line 207

def to_s
  "#{@tag}: time=%.03fus" % (duration * 1_000_000)
end