Class: Lavin::Runner
- Inherits:
-
Object
- Object
- Lavin::Runner
- Defined in:
- lib/lavin/runner.rb
Defined Under Namespace
Classes: Entry
Class Attribute Summary collapse
-
.thread ⇒ Object
Returns the value of attribute thread.
Instance Attribute Summary collapse
-
#index ⇒ Object
Returns the value of attribute index.
-
#personas ⇒ Object
readonly
Returns the value of attribute personas.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
-
#spawned_users ⇒ Object
Returns the value of attribute spawned_users.
-
#total_users ⇒ Object
readonly
Returns the value of attribute total_users.
Class Method Summary collapse
Instance Method Summary collapse
- #create_async_task ⇒ Object
-
#initialize(personas: nil) ⇒ Runner
constructor
A new instance of Runner.
- #next_persona ⇒ Object
- #spawn(count: 1) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(personas: nil) ⇒ Runner
Returns a new instance of Runner.
79 80 81 82 83 84 85 |
# File 'lib/lavin/runner.rb', line 79 def initialize(personas: nil) @personas = personas || User.personas @remaining = @personas.map { |persona| Entry.new(persona) } @total_users = @remaining.sum(&:count) @spawned_users = 0 @index = -1 end |
Class Attribute Details
.thread ⇒ Object
Returns the value of attribute thread.
41 42 43 |
# File 'lib/lavin/runner.rb', line 41 def thread @thread end |
Instance Attribute Details
#index ⇒ Object
Returns the value of attribute index.
38 39 40 |
# File 'lib/lavin/runner.rb', line 38 def index @index end |
#personas ⇒ Object (readonly)
Returns the value of attribute personas.
37 38 39 |
# File 'lib/lavin/runner.rb', line 37 def personas @personas end |
#remaining ⇒ Object (readonly)
Returns the value of attribute remaining.
37 38 39 |
# File 'lib/lavin/runner.rb', line 37 def remaining @remaining end |
#spawned_users ⇒ Object
Returns the value of attribute spawned_users.
38 39 40 |
# File 'lib/lavin/runner.rb', line 38 def spawned_users @spawned_users end |
#total_users ⇒ Object (readonly)
Returns the value of attribute total_users.
37 38 39 |
# File 'lib/lavin/runner.rb', line 37 def total_users @total_users end |
Class Method Details
.running? ⇒ Boolean
70 71 72 73 74 75 76 |
# File 'lib/lavin/runner.rb', line 70 def running? return false unless thread return true if %w[run sleep].include? thread.status stop false end |
.start ⇒ Object
47 48 49 |
# File 'lib/lavin/runner.rb', line 47 def start new.start end |
.start_async ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lavin/runner.rb', line 51 def start_async self.thread = Thread.new do new.start stop exit end rescue StandardError => error puts "Failed to run in thread: #{error.}" thread.join self.thread = nil raise end |
.stop ⇒ Object
64 65 66 67 68 |
# File 'lib/lavin/runner.rb', line 64 def stop thread&.kill self.thread = nil Statistics.stop end |
.yield ⇒ Object
43 44 45 |
# File 'lib/lavin/runner.rb', line 43 def yield Async::Task.current.yield if Async::Task.current? end |
Instance Method Details
#create_async_task ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lavin/runner.rb', line 96 def create_async_task @task = Async(annotation: "Main") do |task| spawn(count: total_users) do |persona| next unless persona user_index = spawned_users annotation = "User: #{persona.name} ##{user_index}" task.async(annotation:) do |user_task| user = persona.new(user_index:, task: user_task) user.run ensure user.cleanup end end end end |
#next_persona ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/lavin/runner.rb', line 132 def next_persona self.index += 1 entry = remaining[index % remaining.size] if entry.present? entry.get elsif remaining.any?(&:present?) next_persona end end |
#spawn(count: 1) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/lavin/runner.rb', line 122 def spawn(count: 1) count.times do persona = next_persona break unless persona self.spawned_users += 1 yield persona end end |
#start ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/lavin/runner.rb', line 87 def start Statistics.meassure { create_async_task } rescue StandardError => error puts "Failed to run tasks: #{error.}" raise ensure stop end |
#stop ⇒ Object
117 118 119 120 |
# File 'lib/lavin/runner.rb', line 117 def stop @task&.stop @task&.wait end |
#wait ⇒ Object
113 114 115 |
# File 'lib/lavin/runner.rb', line 113 def wait @task&.wait end |