Class: Actor::TestFixtures::ParallelIteration

Inherits:
Object
  • Object
show all
Includes:
TestBench::Fixture
Defined in:
lib/actor/test_fixtures/parallel_iteration.rb

Defined Under Namespace

Modules: Defaults

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iteration_proc, prose) ⇒ ParallelIteration

Returns a new instance of ParallelIteration.



15
16
17
18
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 15

def initialize iteration_proc, prose
  @iteration_proc = iteration_proc
  @prose = prose
end

Instance Attribute Details

#iteration_countObject



89
90
91
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 89

def iteration_count
  @iteration_count ||= Defaults.iteration_count
end

#iteration_procObject (readonly)

Returns the value of attribute iteration_proc.



7
8
9
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 7

def iteration_proc
  @iteration_proc
end

#proseObject (readonly)

Returns the value of attribute prose.



12
13
14
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 12

def prose
  @prose
end

#setup_procObject



100
101
102
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 100

def setup_proc
  @setup_proc ||= proc {}
end

#setup_thread_procObject



104
105
106
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 104

def setup_thread_proc
  @setup_thread_proc ||= proc {}
end

#teardown_procObject



108
109
110
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 108

def teardown_proc
  @teardown_proc ||= proc {}
end

#test_procObject

Returns the value of attribute test_proc.



11
12
13
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 11

def test_proc
  @test_proc
end

#thread_countObject



116
117
118
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 116

def thread_count
  @thread_count ||= Defaults.thread_count
end

Class Method Details

.build(prose, each_iteration:, test: nil, setup: nil, setup_thread: nil, teardown: nil, iterations: nil, threads: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 20

def self.build prose, each_iteration:, test: nil, setup: nil, setup_thread: nil, teardown: nil, iterations: nil, threads: nil
  instance = new each_iteration, prose
  instance.iteration_count = iterations
  instance.setup_proc = setup
  instance.setup_thread_proc = setup_thread
  instance.teardown_proc = teardown
  instance.test_proc = test
  instance.thread_count = threads
  instance
end

.call(*arguments) ⇒ Object



31
32
33
34
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 31

def self.call *arguments
  instance = build *arguments
  instance.()
end

Instance Method Details

#callObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 36

def call
  instance_exec &setup_proc

  threads = thread_count.times.map do |index|
    thread = Thread.new do
      Thread.stop

      instance_exec &setup_thread_proc

      iteration_count.times do
        instance_exec &iteration_proc
      end

      prose = "#{self.prose} (Thread: #{index + 1}/#{thread_count})"

      if test_proc
        test prose do
          instance_exec &test_proc
        end
      else
        context prose
      end
    end
    thread.abort_on_exception = true
    thread
  end

  Thread.pass until threads.all? &:stop?

  context "Started #{thread_count} threads; each will iterate #{iteration_count} times"

  t0 = Time.now

  threads.each &:wakeup
  threads.each &:join

  t1 = Time.now

  elapsed_time = t1 - t0

  total_iterations = thread_count * iteration_count

  iterations_per_second = Rational total_iterations, elapsed_time

  context "Finished %i iterations across %i threads in %.2fs; %.2f iterations per second" %
    [total_iterations, thread_count, elapsed_time, iterations_per_second]

  return iteration_count, thread_count

ensure
  instance_exec &teardown_proc
end


93
94
95
96
97
98
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 93

def print_debug receiver
  str = receiver.inspect
  str << "\n"

  $stdout.write str
end

#threadObject



112
113
114
# File 'lib/actor/test_fixtures/parallel_iteration.rb', line 112

def thread
  Thread.current
end