// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tools.DotNETCommon; using UnrealBuildTool; 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 post-task cleanup /// virtual protected void PerformCleanup() { } /// /// 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); } try { PerformCleanup(); } catch (Exception Ex) { Log.TraceError("Cleanup of {0} failed. {1}", GetFullTaskName(), Ex); } } /// /// 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; } public override string ToString() { return GetFullTaskName(); } } [Flags] public enum DDCTaskOptions { None = 0, WarmDDC = 1 << 0, ColdDDC = 1 << 1, NoDDC = 1 << 2, NoShaderDDC = 1 << 3, HotDDC = 1 << 4, } abstract class BenchmarkEditorTaskBase : BenchmarkTaskBase { protected DDCTaskOptions TaskOptions; protected FileReference ProjectFile = null; protected string EditorArgs = ""; protected string ProjectName { get { return ProjectFile == null ? "UE4" : ProjectFile.GetFileNameWithoutAnyExtensions(); } } protected BenchmarkEditorTaskBase(FileReference InProjectFile, DDCTaskOptions InTaskOptions, string InEditorArgs) { TaskOptions = InTaskOptions; EditorArgs = InEditorArgs.Trim().Replace(" ", " "); ProjectFile = InProjectFile; if (TaskOptions == DDCTaskOptions.None || TaskOptions.HasFlag(DDCTaskOptions.WarmDDC)) { TaskModifiers.Add("warmddc"); } if (TaskOptions.HasFlag(DDCTaskOptions.ColdDDC)) { TaskModifiers.Add("coldddc"); } if (TaskOptions.HasFlag(DDCTaskOptions.HotDDC)) { TaskModifiers.Add("hotddc"); } if (TaskOptions.HasFlag(DDCTaskOptions.NoDDC)) { TaskModifiers.Add("noddc"); } if (TaskOptions.HasFlag(DDCTaskOptions.NoShaderDDC)) { TaskModifiers.Add("noshaderddc"); } if (!string.IsNullOrEmpty(EditorArgs)) { TaskModifiers.Add(EditorArgs); } } private void DeleteLocalDDC(FileReference InProjectFile) { List DirsToClear = new List(); DirectoryReference ProjectDir = InProjectFile.Directory; DirsToClear.Add(DirectoryReference.Combine(ProjectDir, "Saved")); DirsToClear.Add(DirectoryReference.Combine(CommandUtils.EngineDirectory, "DerivedDataCache")); DirsToClear.Add(DirectoryReference.Combine(ProjectDir, "DerivedDataCache")); string LocalDDC = Environment.GetEnvironmentVariable("UE-LocalDataCachePath"); if (!string.IsNullOrEmpty(LocalDDC) && Directory.Exists(LocalDDC)) { DirsToClear.Add(new DirectoryReference(LocalDDC)); } foreach (var Dir in DirsToClear) { try { if (DirectoryReference.Exists(Dir)) { Log.TraceInformation("Removing {0}", Dir); DirectoryReference.Delete(Dir, true); } } catch (Exception Ex) { Log.TraceWarning("Failed to remove path {0}. {1}", Dir.FullName, Ex.Message); } } } private DirectoryReference PrivateDDCPath = null; private string OldLocalDCCPath = null; private string GetLocalDDCPathEnvironmentKey() { string EnvVar = "UE-LocalDataCachePath"; if (BuildHostPlatform.Current.Platform != UnrealTargetPlatform.Win64) { EnvVar = EnvVar.Replace("-", "_"); } return EnvVar; } protected override bool PerformPrequisites() { if (TaskOptions.HasFlag(DDCTaskOptions.ColdDDC)) { PrivateDDCPath = DirectoryReference.Combine(CommandUtils.EngineDirectory, "BenchmarkDerivedDataCache"); if (DirectoryReference.Exists(PrivateDDCPath)) { DirectoryReference.Delete(PrivateDDCPath, true); } string Key = GetLocalDDCPathEnvironmentKey(); OldLocalDCCPath = Environment.GetEnvironmentVariable(Key); Environment.SetEnvironmentVariable(Key, PrivateDDCPath.FullName); DirectoryReference BootDDC = DirectoryReference.Combine(ProjectFile.Directory, "DerivedDataCache"); try { DirectoryReference.Delete(BootDDC, true); } catch { } } return base.PerformPrequisites(); } protected override void PerformCleanup() { if (PrivateDDCPath != null) { if (DirectoryReference.Exists(PrivateDDCPath)) { DirectoryReference.Delete(PrivateDDCPath, true); } Environment.SetEnvironmentVariable(GetLocalDDCPathEnvironmentKey(), OldLocalDCCPath); } } } }