Class: Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/benchify/benchmark.rb

Direct Known Subclasses

Benchify

Constant Summary collapse

MESSAGE =
'processing time:'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = :sec, message = nil) ⇒ Benchmark

Returns a new instance of Benchmark.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/benchify/benchmark.rb', line 13

def initialize( type = :sec, message = nil )
  case type
    when :m, :min, :minute, :minutes then :min
    when :ms, :milli, :milliseconds  then :ms
    when :h, :hour, :hours           then :h
    else :sec
  end
  @data = []
  @type = type
  @msg  = message || MESSAGE
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/benchify/benchmark.rb', line 8

def data
  @data
end

#maxObject (readonly)

Returns the value of attribute max.



8
9
10
# File 'lib/benchify/benchmark.rb', line 8

def max
  @max
end

#middleObject (readonly) Also known as: time

Returns the value of attribute middle.



8
9
10
# File 'lib/benchify/benchmark.rb', line 8

def middle
  @middle
end

#minObject (readonly)

Returns the value of attribute min.



8
9
10
# File 'lib/benchify/benchmark.rb', line 8

def min
  @min
end

#msgObject (readonly) Also known as: message

Returns the value of attribute msg.



7
8
9
# File 'lib/benchify/benchmark.rb', line 7

def msg
  @msg
end

#resultObject (readonly)

Returns the value of attribute result.



8
9
10
# File 'lib/benchify/benchmark.rb', line 8

def result
  @result
end

#resultsObject (readonly)

Returns the value of attribute results.



8
9
10
# File 'lib/benchify/benchmark.rb', line 8

def results
  @results
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/benchify/benchmark.rb', line 7

def type
  @type
end

Instance Method Details

#call(message = nil) {|operation| ... } ⇒ Object Also known as: measure

Yields:

  • (operation)


25
26
27
28
29
30
31
# File 'lib/benchify/benchmark.rb', line 25

def call( message = nil, &operation )
  @msg = message unless message.nil?
  start = Time.now
  yield operation
  ends  = Time.now
  @data << [ start, ends ]
end

#finishObject Also known as: end



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/benchify/benchmark.rb', line 35

def finish
  @results = Result.new @type, @msg
  @data.each do |st, en|
    @results.add st, en
  end
  @results.get
  @min     =  @results.min
  @max     =  @results.max
  @middle  =  @results.middle
  @result  =  @results.output
end