fauna-api 1.0.856

The Fauna SDK for Rust: accounts, Drive, groups, calendar, chats, payments, Parrot and live rooms.
Documentation
//! An Ant simulation whose personas work machines at once: a timeline with a repository, two
//! personas from the catalog, and a website after its start, so the run's browse phase has each
//! persona open the site on a fork of its own. Each fork's machine saves a revision as its
//! segment ends.

use std::time::{Duration, Instant};

use futures_util::future::BoxFuture;

use super::{Benchmark, Made, Run, Work, ensure, fail, number, short};
use crate::ant::format::AntFormatOrFormat;
use crate::ant::graph::{GraphEdge, GraphNode, WebsiteNode};
use crate::ant::persona::{self, PersonaQuery};
use crate::ant::timeline::{
    self, ConnectOptions, CreateTimelineOptions, Run as SimulationRun, RunStatus, TimelineRoom,
};
use crate::billing::PageQuery;
use crate::error::Result;
use crate::parrot::format::{CategoricalFormat, Format};
use crate::private::benchmarks::BenchmarkGroup;
use crate::termite::versioning::{self, GroupOptions};

const PERSONAS: u32 = 2;
const SITE: &str = "https://example.com/";
/// Less than the benchmark's timeout, so a run that hangs is aborted here first.
const RUN_WAIT: Duration = Duration::from_secs(35 * 60);
const GRAPH_WAIT: Duration = Duration::from_secs(30);

/// Two personas browse, each with its machine for about as long as the run takes.
pub(super) struct Simulation;

impl Benchmark for Simulation {
    fn id(&self) -> &'static str {
        "ant"
    }
    fn name(&self) -> &'static str {
        "Ant simulation"
    }
    fn group(&self) -> BenchmarkGroup {
        BenchmarkGroup::Product
    }
    fn about(&self) -> &'static str {
        "An Ant simulation of two catalog personas browsing a site, each on a fork of the timeline's repository whose machine saves as its part ends."
    }
    fn work(&self) -> Work {
        Work {
            steps: 80.0,
            turns: 20.0,
            machine_hours: 1.0,
            dollars: 0.0,
        }
    }
    fn timeout(&self) -> Duration {
        Duration::from_secs(40 * 60)
    }
    fn run<'a>(&'a self, run: &'a Run) -> BoxFuture<'a, Result<String>> {
        Box::pin(async move {
            run.progress(0.0, "Making the timeline");
            let repository = run.repository("simulation computer").await?;
            let query = PersonaQuery {
                cursor: 0,
                max: PERSONAS,
                ..PersonaQuery::default()
            };
            let audience: Vec<String> = persona::query_personas(&run.key, query)
                .await?
                .into_iter()
                .map(|persona| persona.id)
                .collect();
            ensure(audience.len() == PERSONAS as usize, || {
                format!(
                    "The persona catalog gave {} personas, not {PERSONAS}.",
                    audience.len()
                )
            })?;
            let name = run.named("simulation");
            let options = CreateTimelineOptions {
                group: None,
                audience: Some(audience),
                repository: Some(repository.clone()),
            };
            let created = timeline::create_timeline(&run.key, &name, options).await?;
            run.made(Made::Timeline(created.id.clone()));

            let (room, _) = connect(run, &created.id).await?;
            room.upsert_graph_node(GraphNode::Website(WebsiteNode::new(
                "site", 240.0, 0.0, SITE, None, None,
            )));
            room.upsert_graph_edge(GraphEdge::new("to-site", "start", "site"));
            graph_landed(run, &created.id).await?;

            let question = CategoricalFormat::new(
                "Would you sign up for this site's newsletter?",
                vec!["Yes".into(), "No".into()],
            );
            let query = [AntFormatOrFormat::Format(Format::Categorical(question))];
            let started = chrono::Utc::now();
            let from = Instant::now();
            run.progress(0.1, "The personas browsing");
            let id = room
                .start_run("How people take to the site", &query)
                .await?;
            let finished = finished(run, room, &created.id, &id).await?;
            run.measure(number("Run", from.elapsed().as_secs_f64().round(), "s"));
            ensure(finished.status == RunStatus::Done, || {
                format!(
                    "The run ended {:?}: {}",
                    finished.status,
                    finished.error.as_deref().unwrap_or("no reason given")
                )
            })?;
            ensure(!finished.agent_ids.is_empty(), || {
                "The run had no personas.".into()
            })?;

            run.progress(0.9, "Checking each persona's fork");
            let repositories = versioning::list(&run.key, GroupOptions::default()).await?;
            for agent in &finished.agent_ids {
                let fork = format!("Agent {agent} in {name}");
                let mut saved = false;
                for found in repositories.iter().filter(|r| r.name.starts_with(&fork)) {
                    let latest = found.revisions(PageQuery { cursor: 0, max: 1 }).await?;
                    saved |= latest
                        .first()
                        .is_some_and(|revision| revision.date >= started);
                }
                ensure(saved, || {
                    format!(
                        "Persona {agent} has no fork its machine saved during the run (forks are named \"{fork}\")."
                    )
                })?;
            }
            run.measure(number("Personas", finished.agent_ids.len() as f64, ""));
            let posts: Vec<String> = finished
                .trajectories
                .iter()
                .filter_map(|trajectory| trajectory.thread.as_ref())
                .map(|thread| short(&thread.post))
                .collect();
            Ok(format!(
                "{} personas browsed {SITE} on forks of their own, each saved by its machine. Posts: {}",
                finished.agent_ids.len(),
                posts.join(" | ")
            ))
        })
    }
}

async fn connect(run: &Run, timeline: &str) -> Result<(TimelineRoom, crate::live::Presence)> {
    TimelineRoom::connect(&run.key, timeline, ConnectOptions::default()).await
}

/// Waits until a fresh connection to the timeline has the site and its edge, so the run starts
/// on the graph the room was sent.
async fn graph_landed(run: &Run, timeline: &str) -> Result<()> {
    let deadline = Instant::now() + GRAPH_WAIT;
    loop {
        let (fresh, _) = connect(run, timeline).await?;
        let landed = fresh.nodes().contains_key("site") && fresh.edges().contains_key("to-site");
        fresh.disconnect();
        if landed {
            return Ok(());
        }
        if Instant::now() > deadline {
            return fail("The timeline never got the site and its edge.");
        }
        tokio::time::sleep(Duration::from_secs(2)).await;
    }
}

/// The run once it's done, failed or aborted, from the timeline's room (connected again when
/// it drops). One still going after `RUN_WAIT` is aborted.
async fn finished(
    run: &Run,
    mut room: TimelineRoom,
    timeline: &str,
    id: &str,
) -> Result<SimulationRun> {
    let deadline = Instant::now() + RUN_WAIT;
    loop {
        if !room.ws().is_open() {
            room = connect(run, timeline).await?.0;
        }
        if let Some(found) = room
            .runs()
            .remove(id)
            .filter(|found| !matches!(found.status, RunStatus::Queued | RunStatus::Running))
        {
            room.disconnect();
            return Ok(found);
        }
        if Instant::now() > deadline {
            let aborted = room.abort_run(id).await;
            room.disconnect();
            return fail(format!(
                "The run didn't finish within {} minutes{}.",
                RUN_WAIT.as_secs() / 60,
                aborted
                    .err()
                    .map_or(String::new(), |err| format!(" and wouldn't abort: {err}"))
            ));
        }
        tokio::time::sleep(Duration::from_secs(10)).await;
    }
}