From 1f893c30bb72b1c4842375eadf124c14ca1e47b7 Mon Sep 17 00:00:00 2001 From: "Nuno \"Omega Leo\" Diogo" Date: Thu, 9 Jan 2025 14:30:00 +0000 Subject: [PATCH] Added a Benchmarking Tool to be used mostly in game engines --- OmegaLeo.HelperLib.ConsoleApp/Program.cs | 83 ++++++++++--------- .../Helpers/BenchmarkUtility.cs | 70 ++++++++++++++++ 2 files changed, 115 insertions(+), 38 deletions(-) create mode 100644 OmegaLeo.HelperLib/Helpers/BenchmarkUtility.cs diff --git a/OmegaLeo.HelperLib.ConsoleApp/Program.cs b/OmegaLeo.HelperLib.ConsoleApp/Program.cs index b1fca0e..1fe41d7 100644 --- a/OmegaLeo.HelperLib.ConsoleApp/Program.cs +++ b/OmegaLeo.HelperLib.ConsoleApp/Program.cs @@ -3,6 +3,7 @@ // /media/omegaleo/Development/Library Dev/HelperLib using GameDevLibrary.Extensions; +using GameDevLibrary.Helpers; using LibGit2Sharp; using OmegaLeo.HelperLib.Git; using OmegaLeo.HelperLib.Git.Models; @@ -17,51 +18,57 @@ if (repo.IsNullOrEmpty()) var git = new GitClient(repo); -var changes = git.GetChanges(); - -if (changes.FolderName.IsNotNullOrEmpty()) +var result = BenchmarkUtility.Record(() => { - RecursiveOutput(new List() { changes }); + var changes = git.GetChanges(); - var option = 99; - - while (option != 0) + if (changes.FolderName.IsNotNullOrEmpty()) { - ShowOptions(); - if (option != 99) - { - switch (option) - { - case 1: - Console.WriteLine("Write the commit message"); - var msg = Console.ReadLine(); + RecursiveOutput(new List() { changes }); - while (msg.IsNullOrEmpty()) - { - Console.WriteLine("Empty message detected, please write a commit message..."); - msg = Console.ReadLine(); - } - - var signature = new Signature("Nuno 'Omega Leo' Diogo", "nunodiogo@omegaleo.pt", DateTimeOffset.Now); - git.Commit(msg, signature); - - break; - case 2: - git.Push(); - break; - default: - Console.WriteLine("Invalid Option!"); - break; + var option = 99; + + while (option != 0) + { + ShowOptions(); + if (option != 99) + { + switch (option) + { + case 1: + Console.WriteLine("Write the commit message"); + var msg = Console.ReadLine(); + + while (msg.IsNullOrEmpty()) + { + Console.WriteLine("Empty message detected, please write a commit message..."); + msg = Console.ReadLine(); + } + + var signature = new Signature("Nuno 'Omega Leo' Diogo", "nunodiogo@omegaleo.pt", + DateTimeOffset.Now); + git.Commit(msg, signature); + + break; + case 2: + git.Push(); + break; + default: + Console.WriteLine("Invalid Option!"); + break; + } } + + int.TryParse(Console.ReadLine(), out option); } - - int.TryParse(Console.ReadLine(), out option); } -} -else -{ - Console.WriteLine("No changes found in specified repository"); -} + else + { + Console.WriteLine("No changes found in specified repository"); + } +}); + +Console.WriteLine($"Time for checking for changes: {result} ms"); Console.ReadLine(); diff --git a/OmegaLeo.HelperLib/Helpers/BenchmarkUtility.cs b/OmegaLeo.HelperLib/Helpers/BenchmarkUtility.cs new file mode 100644 index 0000000..be421b7 --- /dev/null +++ b/OmegaLeo.HelperLib/Helpers/BenchmarkUtility.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; + +namespace GameDevLibrary.Helpers +{ + public class BenchmarkUtility + { + private static Dictionary> _benchmarks = new Dictionary>(); + private static Dictionary _stopwatches = new Dictionary(); + + private static Stopwatch GetStopwatch(string key) + { + Stopwatch stopwatch = new Stopwatch(); + + if (_stopwatches.TryGetValue(key, out stopwatch)) + { + } + else + { + stopwatch = new Stopwatch(); + _stopwatches.Add(key, stopwatch); + } + + return stopwatch; + } + + public static long Record(Action actionToRecord) + { + var stopwatch = new Stopwatch(); + stopwatch.Start(); + + actionToRecord.Invoke(); + + stopwatch.Stop(); + return stopwatch.ElapsedMilliseconds; + } + + public static void Start(string key) + { + GetStopwatch(key).Restart(); + } + + public static void Stop(string key) + { + if (!_benchmarks.ContainsKey(key)) + { + _benchmarks[key] = new List(); + } + + GetStopwatch(key).Stop(); + _benchmarks[key].Add(GetStopwatch(key).ElapsedMilliseconds); + } + + public static List GetResults(string key) + { + return _benchmarks.ContainsKey(key) ? _benchmarks[key] : new List(); + } + + public static void ClearResults() + { + _benchmarks.Clear(); + } + + public static Dictionary> GetAllResults() + { + return new Dictionary>(_benchmarks); + } + } +} \ No newline at end of file