From b6de95b807984c9caa6a9ce7e20438b598b6152c Mon Sep 17 00:00:00 2001 From: Xander Sigler Date: Wed, 8 Dec 2021 20:19:43 -0800 Subject: [PATCH] Added Run time for response answer --- AOC2021/Models/AOCDay.cs | 5 +++++ AOC2021/Models/AOCResponse.cs | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/AOC2021/Models/AOCDay.cs b/AOC2021/Models/AOCDay.cs index debd0fc..1fab357 100644 --- a/AOC2021/Models/AOCDay.cs +++ b/AOC2021/Models/AOCDay.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.Diagnostics; namespace AOC2021.Models { @@ -23,8 +24,10 @@ namespace AOC2021.Models public AOCResponse ExecuteDay(AOCRequest request) { _request = request; + var timer = new Stopwatch(); try { + timer.Start(); switch (request.Version) { case AOCVersion.A: @@ -34,6 +37,8 @@ namespace AOC2021.Models this._response = ExecutePartB(); break; } + timer.Stop(); + this._response.RunTime = timer.ElapsedMilliseconds.ToString(); this._response.Status = true; } catch (Exception e) diff --git a/AOC2021/Models/AOCResponse.cs b/AOC2021/Models/AOCResponse.cs index b0439df..c18924a 100644 --- a/AOC2021/Models/AOCResponse.cs +++ b/AOC2021/Models/AOCResponse.cs @@ -19,5 +19,16 @@ namespace AOC2021.Models public IEnumerable Debug { get; set; } [DataMember] public string StackTrace { get; set; } + private string timeInMs; + [DataMember] + public string RunTime { get { return FormatRunTime(); } set { timeInMs = value; } } + + public string FormatRunTime() + { + var ts = TimeSpan.FromMilliseconds(Convert.ToDouble(timeInMs)); + return $"Run time is {ts.Hours}h {ts.Minutes}min {ts.Seconds}sec {ts.Milliseconds}ms"; + } } + + }