Module: Stud

Extended by:
Stud
Included in:
Stud
Defined in:
lib/stud/secret.rb,
lib/stud/try.rb,
lib/stud/pool.rb,
lib/stud/task.rb,
lib/stud/trap.rb,
lib/stud/interval.rb,
lib/stud/benchmark.rb

Overview

Benchmark Use Cases

* Compare performance of different implementations.
  * run each implementation N times, compare runtimes (histogram, etc)

Defined Under Namespace

Modules: Benchmark Classes: Pool, Secret, Task, Try

Constant Summary collapse

TRY =

class Stud::Try

Try.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.simulate_signal(signal) ⇒ Object



21
22
23
24
# File 'lib/stud/trap.rb', line 21

def self.simulate_signal(signal)
  puts "Simulate: #{signal}"
  @traps[signal].each(&:call)
end

.trap(signal, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/stud/trap.rb', line 2

def self.trap(signal, &block)
  @traps ||= Hash.new { |h,k| h[k] = [] }

  if !@traps.include?(signal)
    # First trap call for this signal, tell ruby to invoke us.
    previous_trap = Signal::trap(signal) { simulate_signal(signal) }
    # If there was a previous trap (via Kernel#trap) set, make sure we remember it.
    if previous_trap.is_a?(Proc)
      # MRI's default traps are "DEFAULT" string
      # JRuby's default traps are Procs with a source_location of "(internal")
      if RUBY_ENGINE != "jruby" || previous_trap.source_location.first != "(internal)"
        @traps[signal] << previous_trap
      end
    end
  end

  @traps[signal] << block
end

Instance Method Details

#interval(time, &block) ⇒ Object

This implementation tries to keep clock more accurately. Prior implementations still permitted skew, where as this one will attempt to correct for skew.

The execution patterns of this method should be that the start time of ‘block.call’ should always be at time T*interval



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/stud/interval.rb', line 8

def interval(time, &block)
  start = Time.now
  while true
    block.call
    duration = Time.now - start
    # Sleep only if the duration was less than the time interval
    if duration < time
      sleep(time - duration)
      start += time
    else
      # Duration exceeded interval time, reset the clock and do not sleep.
      start = Time.now
    end
  end # loop forever
end

#try(enumerable = Stud::Try::FOREVER, &block) ⇒ Object

A simple try method for the common case.



118
119
120
# File 'lib/stud/try.rb', line 118

def try(enumerable=Stud::Try::FOREVER, &block)
  return TRY.try(enumerable, &block)
end