use std::alloc::System;
use stats_alloc::{INSTRUMENTED_SYSTEM, Region, StatsAlloc};
use task_allocation::AgentId;
use task_allocation::cbba::Agent;
use task_allocation::sim::Simulation;
#[path = "../../benches/support/colony.rs"]
mod colony;
use colony::{JOBS, Plan, QUEUE, WARM_UP_ROUNDS, WORKERS, colony, negotiation};
#[global_allocator]
static GLOBAL: &StatsAlloc<System> = &INSTRUMENTED_SYSTEM;
const BUSY_BUDGET: usize = 165;
const SETTLED_BUDGET: usize = 165;
fn allocations_of_one_round(simulation: &mut Simulation<Agent<Plan>>) -> (usize, usize) {
let region = Region::new(GLOBAL);
simulation.step();
let stats = region.change();
(stats.allocations, stats.bytes_allocated)
}
#[test]
fn rounds_stay_within_their_allocation_budget() {
let mut simulation = negotiation(&colony());
for _ in 0..WARM_UP_ROUNDS {
simulation.step();
}
let (busy, busy_bytes) = allocations_of_one_round(&mut simulation);
simulation.run_until_settled(10_000).expect("settles");
let (settled, settled_bytes) = allocations_of_one_round(&mut simulation);
println!(
"cbba round, {WORKERS} x {JOBS}: {busy} allocations ({busy_bytes} bytes) mid negotiation, {settled} ({settled_bytes} bytes) once settled"
);
assert!(busy <= BUSY_BUDGET, "{busy} allocations mid negotiation");
assert!(
settled <= SETTLED_BUDGET,
"{settled} allocations once settled"
);
agent_phases_do_not_allocate(&colony());
}
fn agent_phases_do_not_allocate(plans: &[Plan]) {
let mut agents: Vec<Agent<Plan>> = plans
.iter()
.take(2)
.cloned()
.enumerate()
.map(|(id, plan)| Agent::new(AgentId(id), 2, JOBS, QUEUE, plan))
.collect();
for _ in 0..WARM_UP_ROUNDS {
exchange(&mut agents);
}
let message = agents[1].message();
let region = Region::new(GLOBAL);
agents[0].build_bundle();
let building = region.change().allocations;
let region = Region::new(GLOBAL);
agents[0].receive(&message);
let receiving = region.change().allocations;
let phases = building + receiving;
let region = Region::new(GLOBAL);
let broadcast = agents[0].message();
let messaging = region.change().allocations;
drop(broadcast);
assert_eq!(phases, 0, "bundle and consensus phases allocated");
let outbid = releasing_an_outbid_bundle(plans);
assert_eq!(outbid, 0, "releasing an outbid bundle allocated");
assert_eq!(
messaging, 2,
"a message allocates its claim and clock tables"
);
}
fn releasing_an_outbid_bundle(plans: &[Plan]) -> usize {
let plan = plans[0].clone();
let mut winner = Agent::new(AgentId(0), 2, JOBS, QUEUE, plan.clone());
let mut loser = Agent::new(AgentId(1), 2, JOBS, QUEUE, plan);
winner.build_bundle();
loser.build_bundle();
let message = winner.message();
let region = Region::new(GLOBAL);
loser.receive(&message);
let allocations = region.change().allocations;
assert_eq!(loser.bundle().len(), 0, "the loser keeps nothing");
allocations
}
fn exchange(agents: &mut [Agent<Plan>]) {
for agent in agents.iter_mut() {
agent.build_bundle();
}
let messages: Vec<_> = agents.iter_mut().map(Agent::message).collect();
agents[0].receive(&messages[1]);
agents[1].receive(&messages[0]);
}