| Actor | Sent | Received | Send Rate | Receive Rate | Activity |
|---|---|---|---|---|---|
| alice | 6799 | 6709 | 2266.33 msg/s | 2236.33 msg/s | 13508 total |
| bob | 6750 | 6678 | 2250.0 msg/s | 2226.0 msg/s | 13428 total |
| charlie | 6925 | 6865 | 2308.33 msg/s | 2288.33 msg/s | 13790 total |
| diana | 6945 | 6897 | 2315.0 msg/s | 2299.0 msg/s | 13842 total |
| eve | 7029 | 6987 | 2343.0 msg/s | 2329.0 msg/s | 14016 total |
| frank | 7366 | 7330 | 2455.33 msg/s | 2443.33 msg/s | 14696 total |
| grace | 7496 | 7460 | 2498.67 msg/s | 2486.67 msg/s | 14956 total |
This is the Elixir code that defines the actor simulation model:
# Create 7 actors that randomly send :hi messages to each other
# Using fixed random seed for reproducible results
# Set random seed for reproducibility
:rand.seed(:exs1024, {12_345, 67_890, 11_111})
# Create list of all actor names for random selection
all_actors = [:alice, :bob, :charlie, :diana, :eve, :frank, :grace]
# Function to randomly select a target (excluding self)
random_target = fn sender, targets ->
available_targets = Enum.reject(targets, fn target -> target == sender end)
if available_targets != [] do
Enum.random(available_targets)
else
nil
end
end
# Handler that sends random :hi messages
random_hi_handler = fn msg, state ->
case msg do
:hi ->
# Randomly choose target and send :hi back
target = random_target.(state.name, state.all_actors)
if target do
{:send, [{target, :hi}], state}
else
{:ok, state}
end
_ ->
{:ok, state}
end
end
simulation =
ActorSimulation.new(trace: true) # Enable tracing for sequence diagram
|> ActorSimulation.add_actor(:alice,
send_pattern: {:periodic, 200, :hi},
targets: [:bob, :charlie, :diana, :eve, :frank, :grace],
on_receive: random_hi_handler,
initial_state: %{name: :alice, all_actors: all_actors}
)
|> ActorSimulation.add_actor(:bob,
targets: [:alice, :charlie, :diana, :eve, :frank, :grace],
on_receive: random_hi_handler,
initial_state: %{name: :bob, all_actors: all_actors}
)
|> ActorSimulation.add_actor(:charlie,
targets: [:alice, :bob, :diana, :eve, :frank, :grace],
on_receive: random_hi_handler,
initial_state: %{name: :charlie, all_actors: all_actors}
)
|> ActorSimulation.add_actor(:diana,
targets: [:alice, :bob, :charlie, :eve, :frank, :grace],
on_receive: random_hi_handler,
initial_state: %{name: :diana, all_actors: all_actors}
)
|> ActorSimulation.add_actor(:eve,
targets: [:alice, :bob, :charlie, :diana, :frank, :grace],
on_receive: random_hi_handler,
initial_state: %{name: :eve, all_actors: all_actors}
)
|> ActorSimulation.add_actor(:frank,
targets: [:alice, :bob, :charlie, :diana, :eve, :grace],
on_receive: random_hi_handler,
initial_state: %{name: :frank, all_actors: all_actors}
)
|> ActorSimulation.add_actor(:grace,
targets: [:alice, :bob, :charlie, :diana, :eve, :frank],
on_receive: random_hi_handler,
initial_state: %{name: :grace, all_actors: all_actors}
)
|> ActorSimulation.run(duration: 3000) # Run for 3 seconds to get many interactions
# Draw the message flow
mermaid = ActorSimulation.trace_to_mermaid(simulation, enhanced: true)
{:ok, _path} =
ActorSimulation.write_flowchart_report(simulation, "random_hi_report.html",
title: "Random Hi Messages - 7 Actors",
model_source: model_source
)