From cdcfc326f86c4f321dadd07381d13d3f61ed0293 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sun, 4 Jun 2023 20:26:17 +0100
Subject: [PATCH] chore: support for benchmark In the SDL version.

---
 .github/workflows/main.yml |  2 ++
 CHANGELOG.md               |  1 +
 frontends/sdl/src/main.rs  | 23 ++++++++++++++++-------
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index ac8b67cd..7e001c45 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -127,3 +127,5 @@ jobs:
         run: cd frontends/sdl && cargo build --release
       - name: Run release version
         run: cd frontends/sdl && cargo run --release -- --headless --cycles 10000000
+      - name: Run benchmark
+        run: cd frontends/sdl && cargo run --release -- --benchmark
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e8354e3a..e41b40b6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 
 * Support for the `clock_m()` function
+* Benchmark CLI option in SDL
 
 ### Changed
 
diff --git a/frontends/sdl/src/main.rs b/frontends/sdl/src/main.rs
index b9ac2e86..58eb1b60 100644
--- a/frontends/sdl/src/main.rs
+++ b/frontends/sdl/src/main.rs
@@ -226,22 +226,22 @@ impl Emulator {
 
         let count = params.count;
         let chunk_size = params.chunk_size.unwrap_or(1);
-        let mut cycles = 0;
+        let mut cycles = 0u64;
 
         let initial = SystemTime::now();
 
         if chunk_size > 1 {
             for _ in 0..(count / chunk_size) {
-                cycles += self.system.clock_m(chunk_size) as u32;
+                cycles += self.system.clock_m(chunk_size) as u64;
             }
         } else {
             for _ in 0..count {
-                cycles += self.system.clock() as u32;
+                cycles += self.system.clock() as u64;
             }
         }
 
-        let delta = initial.elapsed().unwrap().as_millis() as f32 / 1000.0;
-        let frequency_mhz = cycles as f32 / delta / 1000.0 / 1000.0;
+        let delta = initial.elapsed().unwrap().as_millis() as f64 / 1000.0;
+        let frequency_mhz = cycles as f64 / delta / 1000.0 / 1000.0;
 
         println!(
             "Took {:.2} seconds to run {} ticks ({} cycles) ({:.2} Mhz)!",
@@ -607,6 +607,13 @@ struct Args {
     #[arg(long, default_value_t = false, help = "If set no timer will be used")]
     no_timer: bool,
 
+    #[arg(
+        long,
+        default_value_t = false,
+        help = "Run in benchmark mode, with no UI"
+    )]
+    benchmark: bool,
+
     #[arg(
         long,
         default_value_t = false,
@@ -663,7 +670,7 @@ fn main() {
     let options = EmulatorOptions {
         auto_mode: Some(auto_mode),
         unlimited: Some(args.unlimited),
-        features: if args.headless {
+        features: if args.headless || args.benchmark {
             Some(vec![])
         } else {
             Some(vec!["video", "audio", "no-vsync"])
@@ -677,7 +684,9 @@ fn main() {
     // determines if the emulator should run in headless mode or
     // not and runs it accordingly, note that if running in headless
     // mode the number of cycles to be run may be specified
-    if args.headless {
+    if args.benchmark {
+        emulator.benchmark(Benchmark::new(500000000, None));
+    } else if args.headless {
         emulator.run_headless(if args.cycles > 0 {
             Some(args.cycles)
         } else {
-- 
GitLab