Class: AppTester::Timer Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/app-tester/timer.rb

Overview

This class is abstract.

Benchmark helper class

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, threshold = nil, method = nil, *args) { ... } ⇒ Timer

Created a new timer object

Examples:

apptester.define_test "my test 400 threshold" do |options, connection|
  AppTester::Timer.new("test timer", 400) do
    sleep 0.5
  end
end

Parameters:

  • message (NilClass, String) (defaults to: nil)

    custom message to be displayed

  • threshold (Number) (defaults to: nil)

    amount in ms. If this limit is passed a warning message will be displayed

  • method (NilClass, Symbol) (defaults to: nil)

    method to benchmark. Optional

  • args (NilClass, String)

    arguments to be passed onto the method

Yields:

  • code snipper to be benchmarked



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/app-tester/timer.rb', line 20

def initialize(message=nil, threshold=nil, method=nil, *args)
  beginning_time = Time.now
  if block_given?
    yield
  else
    self.send(method, args)
  end
  end_time = Time.now
  time_passed = ((end_time - beginning_time)*1000).round(3)

  threshold_message = ""
  unless threshold.nil?
    printf "#{AppTester::Utils::Strings::WARNING} " if time_passed.to_f > threshold.to_f
    threshold_message = " (threshold: #{threshold} ms)"
  end
  message = "to #{message}," if message
  puts "Time elapsed #{message} #{time_passed} milliseconds#{threshold_message}"
  puts ""
end