rusting_engine 2.6.0

Vulkan 3D game engine with GPU-accelerated physics for massive physics-heavy scenes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! Headless render benchmark runner and regression baselines.
//!
//! [`run_render_benchmark`] renders the fixed benchmark scene along its
//! camera path into an offscreen image and summarises every frame into a
//! [`RenderBenchmarkReport`]. Reports serialize to JSON, so a report stored
//! from an earlier run is a baseline: [`RenderBenchmarkReport::regressions`]
//! lists every material regression of a new run against it.
//!
//! The runner waits for each frame before starting the next, so frame time
//! is CPU plus GPU time without overlap: an upper bound on a presented
//! frame, never a lower one.

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

use serde::{Deserialize, Serialize};
use vulkano::device::Queue;
use vulkano::image::view::ImageView;
use vulkano::image::{Image, ImageCreateInfo, ImageUsage};
use vulkano::memory::allocator::{
    AllocationCreateInfo, StandardMemoryAllocator,
};
use vulkano::sync::GpuFuture;

use super::scene_renderer::{SceneRenderOptions, SceneRenderer};
use super::swapchain::OFFSCREEN_COLOR_FORMAT;
use crate::assets::{AssetPlugin, AssetServer};
use crate::runtime::{
    render_benchmark_camera, render_benchmark_city_camera,
    spawn_render_benchmark, spawn_render_benchmark_extras,
    update_render_benchmark_bears, App, CpuFrameTimings, HybridPhysicsPlugin,
    PhysicsBody, QualityProfile, RenderBenchmarkExtras, RenderExtractPlugin,
    RenderSettings, RenderWorld,
};
use crate::Transform;

/// Simulated time per benchmark frame, so physics advances one 60 Hz tick
/// per frame regardless of how fast the machine renders.
pub const RENDER_BENCHMARK_FRAME_TIME: Duration = Duration::from_micros(16_667);

/// Relative growth of a time or work counter that counts as a regression.
pub const REGRESSION_TOLERANCE: f64 = 0.10;
/// Absolute time growth below which timing noise is ignored.
pub const REGRESSION_NOISE_MS: f64 = 0.5;

/// Mean, 95th percentile, and worst value of a per-frame time.
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct TimeStats {
    pub mean_ms: f64,
    pub p95_ms: f64,
    pub max_ms: f64,
}

impl TimeStats {
    fn from_samples(samples: &[Duration]) -> Self {
        if samples.is_empty() {
            return Self::default();
        }
        let mut ms: Vec<f64> = samples
            .iter()
            .map(|time| time.as_secs_f64() * 1e3)
            .collect();
        ms.sort_by(f64::total_cmp);
        let p95 = ms[((ms.len() * 95).div_ceil(100)).max(1) - 1];
        Self {
            mean_ms: ms.iter().sum::<f64>() / ms.len() as f64,
            p95_ms: p95,
            max_ms: ms[ms.len() - 1],
        }
    }
}

/// Summary of one benchmark run. Counters are per-frame maxima unless they
/// say they are totals.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct RenderBenchmarkReport {
    pub device: String,
    pub driver: String,
    pub extent: [u32; 2],
    pub quality: QualityProfile,
    pub frames: u32,
    /// Wall time of update, extraction, recording, and the GPU wait.
    pub frame_time: TimeStats,
    /// CPU time measured inside the frame: physics, extraction,
    /// preparation, and recording.
    pub cpu_time: TimeStats,
    /// Sum of timed GPU passes. Empty when the queue has no timestamps.
    pub gpu_time: TimeStats,
    /// Mean GPU time per pass, by pass label.
    pub gpu_passes_ms: Vec<(String, f64)>,
    /// Mean CPU time per frame phase: `fixed`, `update`, `post_update`,
    /// `extract`, `prepare` and `record`, then `bears` (moving the extra
    /// bears), `screens` (recording the camera-screen feeds), `wait`
    /// (submit and fence wait, which covers the GPU work) and `untimed`
    /// (the rest of the frame time).
    #[serde(default)]
    pub cpu_phases_ms: Vec<(String, f64)>,
    pub draws: u32,
    pub dispatches: u32,
    pub triangles: u64,
    pub visible_instances: usize,
    pub gpu_memory_bytes: u64,
    /// Total bytes written to GPU buffers over the run.
    pub upload_bytes: u64,
    /// `upload_bytes` by part (see `UploadSplit`), totals over the run.
    #[serde(default)]
    pub upload_split_bytes: Vec<(String, u64)>,
    pub physics_bodies: usize,
    /// Totals over the run.
    pub physics_command_bytes: u64,
    pub physics_event_bytes: u64,
    pub physics_state_bytes: u64,
    /// Most frames between submitting and collecting a physics readback.
    pub readback_latency_frames: u32,
    /// Overflow and fallbacks at the end of the run.
    pub dropped_lights: usize,
    pub physics_events_dropped: u64,
    pub physics_commands_rejected: u64,
    /// Scene additions on top of the fixed scene; runs with different
    /// extras do not compare.
    #[serde(default)]
    pub screens: usize,
    #[serde(default)]
    pub screen_size: [u32; 2],
    #[serde(default)]
    pub bears: usize,
    #[serde(default)]
    pub bear_updates: usize,
    #[serde(default)]
    pub screen_every: u32,
    #[serde(default)]
    pub without_bodies: bool,
    #[serde(default)]
    pub lights: usize,
    #[serde(default)]
    pub city: usize,
    #[serde(default)]
    pub effects: bool,
    #[serde(default)]
    pub probes: bool,
    #[serde(default)]
    pub rain: bool,
    /// Most draws and triangles of the camera-screen feeds in one frame;
    /// `draws` and `triangles` leave them out.
    #[serde(default)]
    pub screen_draws: u32,
    #[serde(default)]
    pub screen_triangles: u64,
    /// Bytes the camera-screen feeds uploaded over the run; `upload_bytes`
    /// leaves them out.
    #[serde(default)]
    pub screen_upload_bytes: u64,
    /// Busiest GPU's load in percent before and after the run, when the
    /// driver reports it; another process's load skews the times. The
    /// value after covers the last second of this run too.
    #[serde(default)]
    pub gpu_busy_percent: Option<[u32; 2]>,
    /// Mean frame time of each repeat when the run was repeated; this
    /// report is the median one.
    #[serde(default)]
    pub repeat_frame_means_ms: Vec<f64>,
}

impl RenderBenchmarkReport {
    fn extras(&self) -> RenderBenchmarkExtras {
        RenderBenchmarkExtras {
            screens: self.screens,
            // The size means nothing without screens; older reports
            // stored it as zero.
            screen_size: if self.screens == 0 {
                [0, 0]
            } else {
                self.screen_size
            },
            bears: self.bears,
            bear_updates: self.bear_updates,
            screen_every: self.screen_every,
            without_bodies: self.without_bodies,
            lights: self.lights,
            city: self.city,
            effects: self.effects,
            probes: self.probes,
            rain: self.rain,
        }
    }

    /// Material regressions of `self` against `baseline`, one line each.
    ///
    /// Work counters are compared when both runs used the same extent and
    /// quality. Times are compared only on the same device and driver too;
    /// other machines' times say nothing about this one. Any overflow that
    /// the baseline did not have is a regression.
    #[must_use]
    pub fn regressions(&self, baseline: &Self) -> Vec<String> {
        let mut found = Vec::new();
        if self.extent != baseline.extent
            || self.quality != baseline.quality
            || self.frames != baseline.frames
            || self.extras() != baseline.extras()
        {
            found.push(format!(
                "run {:?} {:?} {} frames {:?} does not match baseline {:?} {:?} {} frames {:?}",
                self.extent,
                self.quality,
                self.frames,
                self.extras(),
                baseline.extent,
                baseline.quality,
                baseline.frames,
                baseline.extras(),
            ));
            return found;
        }
        let mut counter = |name: &str, now: f64, then: f64| {
            if now > then * (1.0 + REGRESSION_TOLERANCE) {
                found.push(format!("{name} rose from {then} to {now}"));
            }
        };
        counter("draws", self.draws.into(), baseline.draws.into());
        counter(
            "dispatches",
            self.dispatches.into(),
            baseline.dispatches.into(),
        );
        counter(
            "triangles",
            self.triangles as f64,
            baseline.triangles as f64,
        );
        counter(
            "visible instances",
            self.visible_instances as f64,
            baseline.visible_instances as f64,
        );
        counter(
            "GPU memory bytes",
            self.gpu_memory_bytes as f64,
            baseline.gpu_memory_bytes as f64,
        );
        counter(
            "upload bytes",
            self.upload_bytes as f64,
            baseline.upload_bytes as f64,
        );
        counter(
            "physics event bytes",
            self.physics_event_bytes as f64,
            baseline.physics_event_bytes as f64,
        );
        counter(
            "physics state bytes",
            self.physics_state_bytes as f64,
            baseline.physics_state_bytes as f64,
        );
        for (name, now, then) in [
            (
                "dropped lights",
                self.dropped_lights as u64,
                baseline.dropped_lights as u64,
            ),
            (
                "dropped physics events",
                self.physics_events_dropped,
                baseline.physics_events_dropped,
            ),
            (
                "rejected physics commands",
                self.physics_commands_rejected,
                baseline.physics_commands_rejected,
            ),
            (
                "readback latency frames",
                self.readback_latency_frames.into(),
                baseline.readback_latency_frames.into(),
            ),
        ] {
            if now > then {
                found.push(format!("{name} rose from {then} to {now}"));
            }
        }
        if self.same_machine(baseline) {
            found.extend(self.time_growth(baseline));
        }
        found
    }

    /// Whether times of `self` and `baseline` compare: same device and
    /// driver.
    #[must_use]
    pub fn same_machine(&self, baseline: &Self) -> bool {
        self.device == baseline.device && self.driver == baseline.driver
    }

    /// Material growth of the p95 frame, CPU and GPU times against
    /// `baseline`, one line each. [`Self::regressions`] counts these only
    /// on the same machine; across a driver update they are drift to warn
    /// about.
    #[must_use]
    pub fn time_growth(&self, baseline: &Self) -> Vec<String> {
        let mut found = Vec::new();
        for (name, now, then) in [
            ("frame time p95", self.frame_time, baseline.frame_time),
            ("CPU time p95", self.cpu_time, baseline.cpu_time),
            ("GPU time p95", self.gpu_time, baseline.gpu_time),
        ] {
            let limit = (then.p95_ms * (1.0 + REGRESSION_TOLERANCE))
                .max(then.p95_ms + REGRESSION_NOISE_MS);
            if now.p95_ms > limit {
                found.push(format!(
                    "{name} rose from {:.2} ms to {:.2} ms",
                    then.p95_ms, now.p95_ms
                ));
            }
        }
        found
    }
}

/// Load of the busiest GPU in percent, from `nvidia-smi` or the DRM
/// drivers' `gpu_busy_percent`; `None` when neither answers.
pub fn gpu_busy_percent() -> Option<u32> {
    let mut busy: Vec<u32> = std::fs::read_dir("/sys/class/drm")
        .into_iter()
        .flatten()
        .flatten()
        .filter_map(|card| {
            std::fs::read_to_string(card.path().join("device/gpu_busy_percent"))
                .ok()
        })
        .filter_map(|text| text.trim().parse().ok())
        .collect();
    if let Ok(output) = std::process::Command::new("nvidia-smi")
        .args([
            "--query-gpu=utilization.gpu",
            "--format=csv,noheader,nounits",
        ])
        .output()
    {
        let text = String::from_utf8_lossy(&output.stdout);
        busy.extend(
            text.lines()
                .filter_map(|line| line.trim().parse::<u32>().ok()),
        );
    }
    busy.into_iter().max()
}

/// Minimum, median and spread (max over min, minus one) of the repeats'
/// mean frame times. The median of an even count is the lower middle, so
/// it is always one of the runs.
#[must_use]
pub fn repeat_spread(means: &[f64]) -> (f64, f64, f64) {
    let mut sorted = means.to_vec();
    sorted.sort_by(f64::total_cmp);
    let min = sorted[0];
    let median = sorted[(sorted.len() - 1) / 2];
    (min, median, sorted[sorted.len() - 1] / min - 1.0)
}

/// Names of [`RenderBenchmarkReport::cpu_phases_ms`], in frame order.
const CPU_PHASES: [&str; 10] = [
    "fixed",
    "update",
    "post_update",
    "extract",
    "prepare",
    "record",
    "bears",
    "screens",
    "wait",
    "untimed",
];

/// `timings` in the order of [`CPU_PHASES`], up to `record`.
fn cpu_phases(timings: &CpuFrameTimings) -> [Duration; 6] {
    [
        timings.physics,
        timings.update,
        timings.post_update,
        timings.extraction,
        timings.preparation,
        timings.recording,
    ]
}

/// Renders `frames` frames of the fixed benchmark scene and camera path at
/// `extent` and `quality` into an offscreen image.
pub fn run_render_benchmark(
    queue: Arc<Queue>,
    extent: [u32; 2],
    quality: QualityProfile,
    frames: u32,
) -> Result<RenderBenchmarkReport, String> {
    run_render_benchmark_with(
        queue,
        extent,
        quality,
        frames,
        RenderBenchmarkExtras::default(),
    )
}

/// [`run_render_benchmark`] with camera screens and bears added to the
/// scene; screens render before each frame, as in the game window.
pub fn run_render_benchmark_with(
    queue: Arc<Queue>,
    extent: [u32; 2],
    quality: QualityProfile,
    frames: u32,
    extras: RenderBenchmarkExtras,
) -> Result<RenderBenchmarkReport, String> {
    let device = queue.device().clone();
    let memory_allocator =
        Arc::new(StandardMemoryAllocator::new_default(device.clone()));
    let mut renderer = SceneRenderer::new(
        queue.clone(),
        memory_allocator.clone(),
        OFFSCREEN_COLOR_FORMAT,
        extent,
    )
    .map_err(|error| format!("scene renderer: {error}"))?;
    let image = Image::new(
        memory_allocator,
        ImageCreateInfo {
            format: OFFSCREEN_COLOR_FORMAT,
            extent: [extent[0], extent[1], 1],
            usage: ImageUsage::COLOR_ATTACHMENT | ImageUsage::TRANSFER_SRC,
            ..Default::default()
        },
        AllocationCreateInfo::default(),
    )
    .map_err(|error| format!("benchmark target: {error}"))?;
    let target = ImageView::new_default(image)
        .map_err(|error| format!("benchmark target view: {error}"))?;

    let mut app = App::new();
    app.add_plugin(AssetPlugin)
        .and_then(|app| app.add_plugin(HybridPhysicsPlugin))
        .and_then(|app| app.add_plugin(RenderExtractPlugin))
        .map_err(|error| format!("benchmark plugins: {error}"))?;
    app.world_mut().resource_mut::<RenderSettings>().quality = quality;
    let camera = spawn_render_benchmark(app.world_mut());
    let bears = spawn_render_benchmark_extras(app.world_mut(), extras);
    let physics_bodies = app
        .world_mut()
        .query::<&PhysicsBody>()
        .iter(app.world())
        .count();

    let properties = device.physical_device().properties();
    let mut report = RenderBenchmarkReport {
        device: properties.device_name.clone(),
        driver: properties.driver_info.clone().unwrap_or_default(),
        extent,
        quality,
        frames,
        physics_bodies,
        screens: extras.screens,
        screen_size: extras.screen_size,
        bears: extras.bears,
        bear_updates: extras.bear_updates,
        screen_every: extras.screen_every,
        without_bodies: extras.without_bodies,
        lights: extras.lights,
        city: extras.city,
        effects: extras.effects,
        probes: extras.probes,
        rain: extras.rain,
        ..RenderBenchmarkReport::default()
    };
    let mut frame_times = Vec::with_capacity(frames as usize);
    let mut cpu_times = Vec::with_capacity(frames as usize);
    let mut gpu_times = Vec::with_capacity(frames as usize);
    let mut passes: Vec<(String, Duration, u32)> = Vec::new();
    let mut phases = [Duration::ZERO; CPU_PHASES.len()];
    for frame in 0..frames {
        let start = Instant::now();
        // Only traffic is measured; gameplay never reads these.
        renderer.take_completed_physics_events();
        renderer.take_completed_physics_states();
        renderer.take_physics_events_lost();
        *app.world_mut().get_mut::<Transform>(camera).unwrap() =
            if extras.city > 0 {
                render_benchmark_city_camera(extras.city, frame)
            } else {
                render_benchmark_camera(frame)
            };
        let bears_start = Instant::now();
        update_render_benchmark_bears(app.world_mut(), &bears, extras, frame);
        let bears_time = bears_start.elapsed();
        app.update(RENDER_BENCHMARK_FRAME_TIME)
            .map_err(|error| format!("frame {frame} update: {error}"))?;
        let world = app.world();
        let screens_start = Instant::now();
        let screens = renderer
            .render_screens(
                vulkano::sync::now(device.clone()).boxed(),
                world.resource::<RenderWorld>(),
                world.resource::<AssetServer>(),
                extent,
            )
            .map_err(|error| format!("frame {frame} screens: {error}"))?;
        let screens_time = screens_start.elapsed();
        let rendered = renderer
            .render(
                screens,
                target.clone(),
                extent,
                SceneRenderOptions::game(extent),
                world.resource::<RenderWorld>(),
                world.resource::<AssetServer>(),
            )
            .map_err(|error| format!("frame {frame} render: {error}"))?;
        let waiting = Instant::now();
        rendered
            .then_signal_fence_and_flush()
            .map_err(|error| format!("frame {frame} submit: {error}"))?
            .wait(None)
            .map_err(|error| format!("frame {frame} wait: {error}"))?;
        let wait = waiting.elapsed();
        let frame_time = start.elapsed();
        frame_times.push(frame_time);

        let mut cpu = *app.world().resource::<CpuFrameTimings>();
        renderer.write_cpu_timings(&mut cpu);
        cpu_times.push(
            cpu.physics + cpu.extraction + cpu.preparation + cpu.recording,
        );
        let timed = cpu_phases(&cpu);
        for (total, time) in phases.iter_mut().zip(timed) {
            *total += time;
        }
        let measured = [bears_time, screens_time, wait];
        for (total, time) in phases[6..9].iter_mut().zip(measured) {
            *total += time;
        }
        phases[9] += frame_time
            .saturating_sub(timed.iter().chain(&measured).sum::<Duration>());
        // Pass times lag a frame or two; each completed frame counts once
        // it is read, so the samples cover all but the last frames.
        let gpu = renderer.gpu_pass_times();
        if !gpu.0.is_empty() {
            gpu_times.push(gpu.total());
            for (pass, time) in gpu.0 {
                match passes
                    .iter_mut()
                    .find(|(label, ..)| label == pass.label())
                {
                    Some((_, total, count)) => {
                        *total += time;
                        *count += 1;
                    }
                    None => passes.push((pass.label().into(), time, 1)),
                }
            }
        }
        let screen = renderer.screen_counters();
        report.screen_draws = report.screen_draws.max(screen.draws);
        report.screen_triangles = report.screen_triangles.max(screen.triangles);
        report.screen_upload_bytes += screen.upload_bytes;
        let counters = renderer.render_counters();
        report.draws = report.draws.max(counters.draws);
        report.dispatches = report.dispatches.max(counters.dispatches);
        report.triangles = report.triangles.max(counters.triangles);
        report.visible_instances =
            report.visible_instances.max(counters.visible_instances);
        report.gpu_memory_bytes =
            report.gpu_memory_bytes.max(counters.gpu_memory_bytes);
        report.upload_bytes += counters.upload_bytes;
        let parts = counters.upload_split.parts();
        if report.upload_split_bytes.is_empty() {
            report.upload_split_bytes =
                parts.iter().map(|(name, _)| ((*name).into(), 0)).collect();
        }
        for ((_, total), (_, bytes)) in
            report.upload_split_bytes.iter_mut().zip(parts)
        {
            *total += bytes;
        }
        report.physics_command_bytes += counters.physics_command_bytes;
        report.physics_event_bytes += counters.physics_event_bytes;
        report.physics_state_bytes += counters.physics_state_bytes;
        report.readback_latency_frames = report
            .readback_latency_frames
            .max(counters.physics_readback_latency_frames);
    }
    let capacity = renderer.capacity_diagnostics();
    report.dropped_lights = capacity.dropped_lights;
    report.physics_events_dropped = capacity.physics_events_dropped;
    report.physics_commands_rejected = capacity.physics_commands_rejected;
    report.frame_time = TimeStats::from_samples(&frame_times);
    report.cpu_time = TimeStats::from_samples(&cpu_times);
    report.gpu_time = TimeStats::from_samples(&gpu_times);
    report.cpu_phases_ms = CPU_PHASES
        .iter()
        .zip(phases)
        .map(|(name, total)| {
            let mean = total.as_secs_f64() * 1e3 / f64::from(frames.max(1));
            ((*name).to_owned(), mean)
        })
        .collect();
    report.gpu_passes_ms = passes
        .into_iter()
        .map(|(label, total, count)| {
            (label, total.as_secs_f64() * 1e3 / f64::from(count))
        })
        .collect();
    Ok(report)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn report() -> RenderBenchmarkReport {
        let time = |p95_ms| TimeStats {
            mean_ms: p95_ms,
            p95_ms,
            max_ms: p95_ms,
        };
        RenderBenchmarkReport {
            device: "gpu".into(),
            driver: "1".into(),
            extent: [1920, 1080],
            quality: QualityProfile::Eco,
            frames: 600,
            frame_time: time(10.0),
            cpu_time: time(4.0),
            gpu_time: time(6.0),
            draws: 100,
            triangles: 1_000_000,
            upload_bytes: 1 << 20,
            ..RenderBenchmarkReport::default()
        }
    }

    #[test]
    fn time_stats_take_mean_p95_and_worst() {
        let samples: Vec<_> = (1..=100).map(Duration::from_millis).collect();
        let stats = TimeStats::from_samples(&samples);
        assert_eq!(stats.p95_ms, 95.0);
        assert_eq!(stats.max_ms, 100.0);
        assert!((stats.mean_ms - 50.5).abs() < 1e-9);
        assert_eq!(TimeStats::from_samples(&[]), TimeStats::default());
    }

    #[test]
    fn regressions_flag_material_growth_only() {
        let baseline = report();
        assert!(baseline.regressions(&baseline).is_empty());

        // Within tolerance and noise: not a regression.
        let mut run = report();
        run.draws = 110;
        run.frame_time.p95_ms = 10.4;
        run.gpu_time.p95_ms = 6.4;
        assert!(
            run.regressions(&baseline).is_empty(),
            "{:?}",
            run.regressions(&baseline)
        );

        let mut run = report();
        run.draws = 111;
        run.triangles = 2_000_000;
        run.cpu_time.p95_ms = 5.0;
        run.physics_events_dropped = 1;
        let found = run.regressions(&baseline);
        assert_eq!(found.len(), 4, "{found:?}");
        assert!(found[0].starts_with("draws"), "{found:?}");
        assert!(found.iter().any(|line| line.starts_with("CPU time")));

        // Another machine's times are not compared; its counters are.
        run.device = "other gpu".into();
        assert_eq!(run.regressions(&baseline).len(), 3);
        assert!(!run.same_machine(&baseline));
        assert_eq!(run.time_growth(&baseline).len(), 1);

        // A screen size without screens is not part of the run.
        let mut sized = report();
        sized.screen_size = [320, 180];
        assert!(sized.regressions(&baseline).is_empty());

        run.extent = [1280, 720];
        let found = run.regressions(&baseline);
        assert_eq!(found.len(), 1);
        assert!(found[0].contains("does not match baseline"), "{found:?}");
    }

    #[test]
    fn repeats_report_minimum_median_and_spread() {
        let (min, median, spread) = repeat_spread(&[12.0, 10.0, 11.0]);
        assert_eq!((min, median), (10.0, 11.0));
        assert!((spread - 0.2).abs() < 1e-12);
        let (min, median, spread) = repeat_spread(&[13.0, 10.0, 10.5, 30.0]);
        assert_eq!((min, median), (10.0, 10.5));
        assert!((spread - 2.0).abs() < 1e-12);
        assert_eq!(repeat_spread(&[4.0]), (4.0, 4.0, 0.0));
    }

    #[test]
    fn reports_round_trip_through_json() {
        let mut report = report();
        report.gpu_passes_ms = vec![("scene".into(), 3.5)];
        let json = serde_json::to_string_pretty(&report).unwrap();
        let back: RenderBenchmarkReport = serde_json::from_str(&json).unwrap();
        assert_eq!(back, report);
    }

    #[test]
    #[cfg_attr(
        not(feature = "gpu-tests"),
        ignore = "run with `--features gpu-tests` on a machine with a Vulkan driver"
    )]
    fn benchmark_records_every_metric_along_the_camera_path() {
        let base = crate::rendering::test_support::headless_device();
        let report = run_render_benchmark(
            base.queue.clone(),
            [320, 180],
            QualityProfile::Eco,
            30,
        )
        .unwrap();
        println!("{}", serde_json::to_string_pretty(&report).unwrap());
        assert_eq!(report.frames, 30);
        assert!(report.frame_time.mean_ms > 0.0);
        assert!(report.cpu_time.mean_ms > 0.0);
        assert!(report.draws > 0 && report.dispatches > 0);
        assert!(report.triangles > 0 && report.visible_instances > 0);
        assert!(report.gpu_memory_bytes > 0 && report.upload_bytes > 0);
        let split = &report.upload_split_bytes;
        assert_eq!(
            split.iter().map(|(_, bytes)| bytes).sum::<u64>(),
            report.upload_bytes,
            "{split:?}"
        );
        assert_eq!(
            report.physics_bodies,
            1 + crate::runtime::RENDER_BENCHMARK_BODIES
        );
        // 1 sun + 24 point lights fit Eco's 256.
        assert_eq!(report.dropped_lights, 0);
        assert!(report.regressions(&report).is_empty());
        let phases: Vec<_> = report
            .cpu_phases_ms
            .iter()
            .map(|(name, _)| name.as_str())
            .collect();
        assert_eq!(phases, CPU_PHASES);
        // Every phase that runs work in this scene took time; it has no
        // extra bears or screens.
        for (name, ms) in &report.cpu_phases_ms {
            if !["update", "bears", "screens"].contains(&name.as_str()) {
                assert!(*ms > 0.0, "{name}: {:?}", report.cpu_phases_ms);
            }
        }
        // The phases, the wait and the untimed rest make up the frame.
        let sum: f64 = report.cpu_phases_ms.iter().map(|(_, ms)| ms).sum();
        assert!(
            (sum - report.frame_time.mean_ms).abs() < 0.01,
            "{sum} vs {:?}",
            report.frame_time
        );
    }

    #[test]
    #[cfg_attr(
        not(feature = "gpu-tests"),
        ignore = "run with `--features gpu-tests` on a machine with a Vulkan driver"
    )]
    fn benchmark_counts_screen_feeds_and_bears_apart() {
        let base = crate::rendering::test_support::headless_device();
        let report = run_render_benchmark_with(
            base.queue.clone(),
            [320, 180],
            QualityProfile::Eco,
            4,
            RenderBenchmarkExtras {
                screens: 2,
                screen_size: [64, 36],
                bears: 8,
                bear_updates: 8,
                ..RenderBenchmarkExtras::default()
            },
        )
        .unwrap();
        assert!(report.screen_draws > 0 && report.screen_triangles > 0);
        assert!(report.screen_upload_bytes > 0);
        let phase = |name: &str| {
            report.cpu_phases_ms.iter().find(|p| p.0 == name).unwrap().1
        };
        assert!(phase("bears") > 0.0 && phase("screens") > 0.0);
    }
}