Class: ImLost::TimerStore

Inherits:
Object
  • Object
show all
Defined in:
lib/im-lost.rb

Overview

A store to create and register timers you can use to estimate the runtime of some code.

All timers are identified by an unique ID or a name.

Examples:

Use a named timer

ImLost.timer.create('my_test')

# ...your code here...

ImLost.timer['my_test']
# => prints the timer name, this location and runtime so far

# ...more code here...

ImLost.timer['my_test']
# => prints the timer name, this location and runtime since the timer was created

ImLost.timer.delete('my_test')
# the timer with name 'my_test' is not longer valid now

Use an anonymous timer

tmr = ImLost.timer.create

# ...your code here...

ImLost.timer[tmr]
# => prints the timer ID, this location and runtime so far

# ...more code here...

ImLost.timer[tmr]
# => prints the timer ID, this location and runtime since the timer was created

ImLost.timer.delete(tmr)
# the timer with the ID `tmr` is not longer valid now

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countInteger (readonly)

Returns the number of registered timers.

Returns:

  • (Integer)

    the number of registered timers



451
# File 'lib/im-lost.rb', line 451

def count = ids.size

#empty?Boolean (readonly)

Returns whether the timer store is empty or not.

Returns:

  • (Boolean)

    whether the timer store is empty or not



455
# File 'lib/im-lost.rb', line 455

def empty? = ids.empty?

#idsArray<Integer> (readonly)

Returns IDs of all registered timers.

Returns:

  • (Array<Integer>)

    IDs of all registered timers



459
# File 'lib/im-lost.rb', line 459

def ids = @ll.keys.keep_if { Integer === _1 }

#namesArray<String> (readonly)

Returns names of all registered named timers.

Returns:

  • (Array<String>)

    names of all registered named timers



463
# File 'lib/im-lost.rb', line 463

def names = @ll.keys.delete_if { Integer === _1 }

Class Method Details

.nowFloat

Returns current time.

Returns:

  • (Float)

    current time



443
# File 'lib/im-lost.rb', line 443

def self.now = Process.clock_gettime(Process::CLOCK_MONOTONIC)

Instance Method Details

#[](id_or_name) ⇒ Integer

Print the ID or name and the runtime since a timer was created. It includes the location.

Parameters:

  • id_or_name (Integer, #to_s)

    the identifier or the name of the timer

Returns:

  • (Integer)

    timer ID

Raises:

  • (ArgumentError)

    when the given id or name is not a registered timer identifier or name



519
520
521
522
523
524
525
# File 'lib/im-lost.rb', line 519

def [](id_or_name)
  now = self.class.now
  timer = @ll[Integer === id_or_name ? id_or_name : id_or_name.to_s]
  raise(ArgumentError, "not a timer - #{id_or_name.inspect}") unless timer
  @cb[timer[0], Kernel.caller_locations(1, 1)[0], (now - timer[1]).round(4)]
  timer.__id__
end

#allnil

Print the ID or name and the runtime of all active timers. It includes the location.

Returns:

  • (nil)


533
534
535
536
537
538
# File 'lib/im-lost.rb', line 533

def all
  now = self.class.now
  loc = Kernel.caller_locations(1, 1)[0]
  @ll.values.uniq.reverse_each { |name, start| @cb[name, loc, now - start] }
  nil
end

#clearself

Delete and unregister all timers.

Returns:

  • (self)

    itself



505
506
507
508
# File 'lib/im-lost.rb', line 505

def clear
  @ll = {}
  self
end

#create(name = nil) ⇒ Integer

Create and register a new named or anonymous timer. It print the ID or name of the created timer and includes the location.

Parameters:

  • name (#to_s) (defaults to: nil)

    optional timer name

Returns:

  • (Integer)

    timer ID



472
473
474
475
476
477
478
479
# File 'lib/im-lost.rb', line 472

def create(name = nil)
  timer = []
  @ll[id = timer.__id__] = timer
  name ? @ll[name = name.to_s] = timer : name = id
  @cb[name, Kernel.caller_locations(1, 1)[0]]
  timer << name << self.class.now
  id
end

#delete(*id_or_names) ⇒ nil

Delete and unregister timers.

Parameters:

  • id_or_names (Array<Integer, #to_s>)

    the IDs or the names

Returns:

  • (nil)


487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/im-lost.rb', line 487

def delete(*id_or_names)
  id_or_names.flatten.each do |id|
    if Integer === id
      del = @ll.delete(id)
      @ll.delete(del[0]) if del
    else
      del = @ll.delete(id.to_s)
      @ll.delete(del.__id__) if del
    end
  end
  nil
end