// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tools.DotNETCommon; namespace AutomationTool.Benchmark { /// /// Base class for running tasks /// abstract class BenchmarkTaskBase { /// /// True or false based on whether the task failed /// public bool Failed { get; protected set; } /// /// Failure message /// public string FailureString { get; protected set; } /// /// Don't report this test /// public bool SkipReport { get; protected set; } /// /// Time the task took (does not include prequisites) /// public TimeSpan TaskTime { get; protected set; } /// /// Time the task started /// public DateTime StartTime { get; protected set; } /// /// Perform any prerequisites the task requires /// virtual protected bool PerformPrequisites() { return true; } /// /// Perform the actual task that is measured /// protected abstract bool PerformTask(); /// /// Return a name for this task for reporting /// /// public string TaskName { get; set; } /// /// A list of modifiers that can be considered when /// protected List TaskModifiers { get { return InternalModifiers; } } private readonly List InternalModifiers = new List(); /// /// Run the task. Performs any prerequisites, then the actual task itself /// public void Run() { try { if (PerformPrequisites()) { StartTime = DateTime.Now; if (PerformTask()) { TaskTime = DateTime.Now - StartTime; } else { FailureString = "Task Failed"; Failed = true; } } else { FailureString = "Prequisites Failed"; Failed = true; } } catch (Exception Ex) { FailureString = string.Format("Exception: {0}", Ex.ToString()); Failed = true; } if (Failed) { Log.TraceError("{0} failed. {1}", GetFullTaskName(), FailureString); } } /// /// Report how long the task took /// public void Report() { if (!Failed) { Log.TraceInformation("Task {0}:\t\t\t\t{1}", GetFullTaskName(), TaskTime.ToString(@"hh\:mm\:ss")); } else { Log.TraceInformation("Task {0}::\t\t\t\tFailed. {1}", GetFullTaskName(), FailureString); } } /// /// Returns a full name to use in reporting and logging /// /// public string GetFullTaskName() { string Name = TaskName; if (TaskModifiers.Count > 0) { Name = string.Format("{0} ({1})", Name, string.Join(" ", TaskModifiers)); } return Name; } } }