use std::time::{Duration, Instant};
use futures_util::future::BoxFuture;
use super::agent::agent;
use super::media::keep;
use super::seat::{self, tool};
use super::{Benchmark, Run, Work, bytes, ensure, fail, mib, number, short, since};
use crate::error::Result;
use crate::private::benchmarks::BenchmarkGroup;
use crate::termite::os::{ExecOptions, MachineTool};
use crate::termite::spec::{STANDARD_SPEC, Spec};
const GALLERY_PORT: u16 = 8125;
const QT_APP: &str = "qalculate-qt";
const SIGNUP_PAGE: &str = r#"<!doctype html><title>Sign up</title><style>
*{box-sizing:border-box}body{margin:0;background:#f4f1ee;color:#1d1d1f;font:18px/1.4 'IBM Plex Sans',sans-serif}
form{display:grid;grid-template-rows:40% 60%;justify-items:center;align-items:center;height:100vh}
label{display:grid;gap:8px;width:60%;font-weight:600}input{padding:14px 16px;border:2px solid #d6d0ca;border-radius:10px;font:inherit;font-weight:400}
input:focus{outline:none;border-color:#a31b28}button{width:60%;height:70%;border:0;border-radius:18px;background:#a31b28;color:#fff;font:600 28px 'IBM Plex Sans',sans-serif}
</style><form onsubmit="event.preventDefault();this.querySelector('button').textContent='Welcome, '+this.email.value.split('@')[0]"><label>Email<input name="email" type="email" placeholder="you@example.com"></label><button>Sign up</button></form>"#;
const NOTES: &str = "# Termite\n\nThe desktop's own windows: a terminal, this editor, files, a preview and a Web window.\n";
const SERVE_GALLERY: &str = "#!/usr/bin/env bash\ncp /opt/fauna/desktop/crown.png /repository/gallery/crown.png\ncd /repository/gallery && exec python3 -m http.server 8125\n";
const PHOTOS: [&str; 5] = ["Desktop", "Launcher", "GTK app", "Qt app", "Signed up"];
pub(super) struct Gallery;
fn goal() -> String {
format!(
"This is Termite's gallery, for people to review how the desktop looks. Do each step in order, and take each photo with exactly the title given, with no outline unless asked.
1. Open a terminal window that runs `ls -la gallery`, an editor on gallery/notes.md, a files window on gallery, a preview of gallery/crown.png, and a Web window on http://localhost:{GALLERY_PORT}/.
2. Arrange those five windows in a tidy grid that fills the desktop above the dock: the terminal, editor and files windows across the top, the preview and the Web window (two columns wide) across the bottom, with even gaps. Then take a photo titled \"Desktop\".
3. Press the Super key to open the launcher over the grid, take a photo titled \"Launcher\", press Escape, and close all five windows.
4. Start galculator (`galculator &` from exec), put its window in the middle of the desktop, take a photo titled \"GTK app\", and close it.
5. Install {QT_APP} with `sudo fauna-install {QT_APP}`, start it, put its window in the middle of the desktop, take a photo titled \"Qt app\", and close it.
6. Open a Web window on http://localhost:{GALLERY_PORT}/ about 1200 by 700 near the top left, start a recording titled \"Signing up\", caption what happens as you click the email field, type ada@example.com, and press Sign up, then stop the recording once the page says welcome.
7. Take a photo titled \"Signed up\" with the button you clicked outlined. Then you're done."
)
}
impl Benchmark for Gallery {
fn id(&self) -> &'static str {
"gallery"
}
fn name(&self) -> &'static str {
"Gallery"
}
fn group(&self) -> BenchmarkGroup {
BenchmarkGroup::Machines
}
fn about(&self) -> &'static str {
"Termite's look for review: a machine agent arranges every built in window kind, opens the launcher, shows a GTK and a Qt app, and records a short task with its photo."
}
fn work(&self) -> Work {
Work {
steps: 80.0,
machine_hours: 0.5,
..Work::default()
}
}
fn timeout(&self) -> Duration {
Duration::from_secs(30 * 60)
}
fn run<'a>(&'a self, run: &'a Run) -> BoxFuture<'a, Result<String>> {
Box::pin(async move {
run.progress(0.0, "Writing the gallery's files");
let repository = run.repository("gallery").await?;
repository
.write("/repository/gallery/index.html", SIGNUP_PAGE, None)
.await?;
repository
.write("/repository/gallery/notes.md", NOTES, None)
.await?;
repository
.write("/repository/entry.sh", SERVE_GALLERY, None)
.await?;
run.progress(
0.1,
"The agent arranging the desktop and taking its pictures",
);
let done = agent(&run.key, repository.id, &goal(), 80).await?;
run.progress(0.9, "Keeping the pictures");
let kept = keep(run, repository.id, "").await?;
let photos = kept.iter().filter(|kept| !kept.captioned_video()).count();
let recorded = kept.iter().any(|kept| kept.captioned_video());
run.measure(number("Photos", photos as f64, ""));
run.measure(number(
"Agent steps",
done["steps"].as_f64().unwrap_or(0.0),
"",
));
ensure(done["ok"].as_bool() == Some(true), || {
format!(
"The agent didn't finish: {}",
short(done["reason"].as_str().unwrap_or("it gave no reason"))
)
})?;
ensure(photos >= PHOTOS.len() && recorded, || {
format!(
"The agent kept {photos} of the {} photos and {} captioned recording.",
PHOTOS.len(),
if recorded { "a" } else { "no" }
)
})?;
Ok(format!(
"{photos} photos and a captioned recording of the desktop, the launcher, a GTK app, a Qt app and a signup."
))
})
}
}
pub(super) struct Blender;
const BLENDER: Spec = Spec {
vcpus: 7,
memory_bytes: 16 << 30,
storage_bytes: 32 << 30,
..STANDARD_SPEC
};
const BLENDER_MS: u64 = 20 * 60_000;
const COLORS: [&str; 4] = ["110,16,24", "143,191,127", "11,11,12", "22,22,23"];
const TREE_SCRIPT: &str = r#""""The Termite gallery's tree: a trunk and branches splitting recursively as cylinders, leaves
as small icospheres, on a ground plane under a sun, with a camera orbiting it. Writes tree.obj
(the tree, Y up) and tree.mp4 (a 4 second turntable at 640x360, Cycles on the CPU) beside
itself. Run as blender -b --factory-startup -P tree.py -- BARK LEAVES GROUND SKY, each an sRGB
r,g,b."""
import math
import os
import random
import sys
import bmesh
import bpy
from mathutils import Matrix, Vector
HERE = os.path.dirname(os.path.abspath(__file__))
FRAMES = 4 * 24
BARK, LEAVES, GROUND, SKY = (
[int(channel) for channel in color.split(",")] for color in sys.argv[sys.argv.index("--") + 1 :]
)
random.seed(7)
for made in list(bpy.data.objects):
bpy.data.objects.remove(made)
scene = bpy.context.scene
def linear(rgb):
def channel(value):
value /= 255
return value / 12.92 if value <= 0.04045 else ((value + 0.055) / 1.055) ** 2.4
return (*map(channel, rgb), 1.0)
def solid(name, built, rgb, roughness):
mesh = bpy.data.meshes.new(name)
built.to_mesh(mesh)
built.free()
mesh.polygons.foreach_set("use_smooth", [True] * len(mesh.polygons))
material = bpy.data.materials.new(name)
material.use_nodes = True
shader = material.node_tree.nodes["Principled BSDF"]
shader.inputs["Base Color"].default_value = linear(rgb)
shader.inputs["Roughness"].default_value = roughness
mesh.materials.append(material)
made = bpy.data.objects.new(name, mesh)
scene.collection.objects.link(made)
return made
def turned(direction, spread, around):
tilted = Matrix.Rotation(spread, 3, direction.orthogonal().normalized()) @ direction
return (Matrix.Rotation(around, 3, direction) @ tilted + Vector((0, 0, 0.3))).normalized()
def grow(bark, leaves, start, direction, length, radius, depth):
end = start + direction * length
along = direction.to_track_quat("Z", "Y").to_matrix().to_4x4()
bmesh.ops.create_cone(
bark,
cap_ends=True,
segments=12,
radius1=radius,
radius2=radius * 0.7,
depth=length,
matrix=Matrix.Translation((start + end) / 2) @ along,
)
if depth == 0:
for _ in range(8):
spot = end + Vector([random.uniform(-0.35, 0.35) for _ in range(3)])
size = random.uniform(0.12, 0.2)
bmesh.ops.create_icosphere(leaves, subdivisions=1, radius=size, matrix=Matrix.Translation(spot))
return
for n in range(3):
around = n * 2 * math.pi / 3 + random.uniform(-0.4, 0.4)
child = turned(direction, random.uniform(0.45, 0.75), around)
grow(bark, leaves, end, child, length * random.uniform(0.62, 0.78), radius * 0.62, depth - 1)
def write_obj(path, things):
with open(path, "w") as out:
first = 1
for made in things:
out.write(f"o {made.name}\n")
for vertex in made.data.vertices:
x, y, z = vertex.co
out.write(f"v {x:.4f} {z:.4f} {-y:.4f}\n")
for face in made.data.polygons:
out.write("f " + " ".join(str(first + index) for index in face.vertices) + "\n")
first += len(made.data.vertices)
bark = bmesh.new()
leaves = bmesh.new()
grow(bark, leaves, Vector((0, 0, 0)), Vector((0, 0, 1)), 2.2, 0.2, 4)
ground = bmesh.new()
bmesh.ops.create_grid(ground, x_segments=1, y_segments=1, size=12)
tree = [solid("bark", bark, BARK, 0.8), solid("leaves", leaves, LEAVES, 0.5)]
solid("ground", ground, GROUND, 0.9)
write_obj(os.path.join(HERE, "tree.obj"), tree)
sun = bpy.data.objects.new("sun", bpy.data.lights.new("sun", "SUN"))
sun.data.energy = 4
sun.data.angle = math.radians(4)
sun.rotation_euler = (math.radians(50), 0, math.radians(35))
scene.collection.objects.link(sun)
scene.world = bpy.data.worlds.new("sky")
scene.world.use_nodes = True
scene.world.node_tree.nodes["Background"].inputs["Color"].default_value = linear(SKY)
focus = bpy.data.objects.new("focus", None)
focus.location = (0, 0, 3)
pivot = bpy.data.objects.new("pivot", None)
camera = bpy.data.objects.new("camera", bpy.data.cameras.new("camera"))
camera.data.lens = 30
camera.location = (11, 0, 3.6)
camera.parent = pivot
aim = camera.constraints.new("TRACK_TO")
aim.target = focus
aim.track_axis = "TRACK_NEGATIVE_Z"
aim.up_axis = "UP_Y"
for made in (focus, pivot, camera):
scene.collection.objects.link(made)
scene.camera = camera
bpy.context.preferences.edit.keyframe_new_interpolation_type = "LINEAR"
for frame, turn in ((1, 0), (FRAMES + 1, 2 * math.pi)):
pivot.rotation_euler.z = turn
pivot.keyframe_insert("rotation_euler", index=2, frame=frame)
scene.render.engine = "CYCLES"
scene.cycles.device = "CPU"
scene.cycles.samples = 16
scene.cycles.use_denoising = False
scene.render.resolution_x = 640
scene.render.resolution_y = 360
scene.render.resolution_percentage = 100
scene.render.fps = 24
scene.frame_start = 1
scene.frame_end = FRAMES
scene.render.use_persistent_data = True
scene.render.image_settings.file_format = "FFMPEG"
scene.render.ffmpeg.format = "MPEG4"
scene.render.ffmpeg.codec = "H264"
scene.render.ffmpeg.constant_rate_factor = "MEDIUM"
scene.render.filepath = os.path.join(HERE, "tree.mp4")
bpy.ops.render.render(animation=True)
"#;
pub(super) fn mesh(obj: &str) -> (usize, usize) {
let count = |kind: &str| obj.lines().filter(|line| line.starts_with(kind)).count();
(count("v "), count("f "))
}
impl Benchmark for Blender {
fn id(&self) -> &'static str {
"blender"
}
fn name(&self) -> &'static str {
"Blender"
}
fn group(&self) -> BenchmarkGroup {
BenchmarkGroup::Machines
}
fn about(&self) -> &'static str {
"Blender installed from Debian on the small pool's largest machine, building a procedural tree and rendering a 4 second turntable with Cycles on the CPU, timed."
}
fn work(&self) -> Work {
Work {
machine_hours: 1.0,
..Work::default()
}
}
fn timeout(&self) -> Duration {
Duration::from_secs(60 * 60)
}
fn run<'a>(&'a self, run: &'a Run) -> BoxFuture<'a, Result<String>> {
Box::pin(async move {
run.progress(0.0, "Starting the machine");
let repository = run.repository("blender").await?;
let (room, _) = seat::boot(&run.key, repository.id, BLENDER).await?;
let long = || ExecOptions {
timeout_ms: Some(BLENDER_MS),
};
let rendered = async {
run.progress(0.1, "Installing Blender");
let from = Instant::now();
let installed = room
.exec(
"sudo fauna-install blender >/tmp/blender-install.log 2>&1 || { tail -n 20 /tmp/blender-install.log; exit 1; }",
long(),
)
.await?;
if installed.code != 0 {
return fail(format!(
"Installing Blender failed: {}",
installed.stdout.trim()
));
}
run.measure(number("Install", since(from, Instant::now()) / 1000.0, "s"));
tool(
&room,
MachineTool::Write {
path: "blender/tree.py".into(),
text: TREE_SCRIPT.into(),
line: None,
},
)
.await?;
run.progress(0.35, "Rendering the turntable");
let command = format!(
"blender -b --factory-startup -noaudio --python-exit-code 1 -P blender/tree.py -- {} >/tmp/blender.log 2>&1 && test -s blender/tree.mp4 || {{ tail -n 30 /tmp/blender.log; exit 1; }}",
COLORS.join(" ")
);
let from = Instant::now();
let rendered = room.exec(&command, long()).await?;
let took = since(from, Instant::now());
if rendered.code != 0 {
return fail(format!(
"Blender didn't render the tree: {}",
rendered.stdout.trim()
));
}
run.measure(number("Render", took / 1000.0, "s"));
Ok(took)
}
.await;
let took = match rendered {
Ok(took) => took,
Err(err) => {
let _ = room.stop().await;
return Err(err);
}
};
run.progress(0.9, "Keeping the tree and its turntable");
let obj = bytes(&repository, "/repository/blender/tree.obj").await?;
let video = bytes(&repository, "/repository/blender/tree.mp4").await?;
let _ = room.stop().await;
let (vertices, faces) = mesh(&String::from_utf8_lossy(&obj));
run.measure(number("Vertices", vertices as f64, ""));
run.measure(number("Faces", faces as f64, ""));
run.measure(number("Turntable", mib(video.len() as u64), "MiB"));
run.file("tree.obj", "model/obj", obj).await?;
run.file("tree.mp4", "video/mp4", video).await?;
ensure(vertices >= 8 && faces >= 6, || {
format!("tree.obj isn't a mesh: {vertices} vertices and {faces} faces.")
})?;
Ok(format!(
"Blender rendered a {vertices} vertex tree's turntable in {:.1} s on {} vCPUs.",
took / 1000.0,
BLENDER.vcpus
))
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counts_an_objs_vertices_and_faces() {
let cube = "# cube\nv 0 0 0\nv 1 0 0\nvn 0 0 1\nvt 0 0\nf 1 2 3\nf 1/1 2/2 3/3\n";
assert_eq!(mesh(cube), (2, 2));
assert_eq!(mesh(""), (0, 0));
}
#[test]
fn asks_for_every_photo_it_checks() {
let goal = goal();
for title in PHOTOS {
assert!(goal.contains(&format!("\"{title}\"")), "{title}");
}
}
}