// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Xml; using EpicGames.Core; using Microsoft.Extensions.Logging; namespace AutomationTool.Tasks { /// /// Parameters for . /// public class RandomDataTaskParameters { /// /// Seed for the data generation /// [TaskParameter(Optional = true)] public int Seed { get; set; } = -1; /// /// The size of each file. /// [TaskParameter(Optional = true)] public int Size { get; set; } = 1024; /// /// Number of files to write. /// [TaskParameter(Optional = true)] public int Count { get; set; } = 50; /// /// Whether to generate different data for each output file. /// [TaskParameter(Optional = true)] public bool Different { get; set; } = true; /// /// Output directory /// [TaskParameter(Optional = true)] public string OutputDir { get; set; } /// /// Optional filter to be applied to the list of input files. /// [TaskParameter(Optional = true)] public string Tag { get; set; } } /// /// Creates files containing random data in the specified output directory. Used for generating test data for the temp storage system. /// [TaskElement("RandomData", typeof(RandomDataTaskParameters))] public class RandomDataTask : BgTaskImpl { readonly RandomDataTaskParameters _parameters; /// /// Constructor /// /// Parameters for this task public RandomDataTask(RandomDataTaskParameters parameters) { _parameters = parameters; } /// /// ExecuteAsync the task. /// /// Information about the current job /// Set of build products produced by this node. /// Mapping from tag names to the set of files they include public override async Task ExecuteAsync(JobContext job, HashSet buildProducts, Dictionary> tagNameToFileSet) { DirectoryReference outputDir = ResolveDirectory(_parameters.OutputDir); DirectoryReference.CreateDirectory(outputDir); Random random; if (_parameters.Seed >= 0) { random = new Random(_parameters.Seed); } else { random = new Random(); } byte[] buffer = Array.Empty(); for (int idx = 0; idx < _parameters.Count; idx++) { if (idx == 0 || _parameters.Different) { buffer = new byte[_parameters.Size]; random.NextBytes(buffer); } FileReference file = FileReference.Combine(outputDir, $"test-{_parameters.Size}-{idx}.dat"); await FileReference.WriteAllBytesAsync(file, buffer); buildProducts.Add(file); } Logger.LogInformation("Created {NumFiles:n0} files of {Size:n0} bytes in {OutputDir} (Different={Different})", _parameters.Count, _parameters.Size, outputDir, _parameters.Different); // Apply the optional output tag to them foreach (string tagName in FindTagNamesFromList(_parameters.Tag)) { FindOrAddTagSet(tagNameToFileSet, tagName).UnionWith(buildProducts); } } /// /// Output this task out to an XML writer. /// public override void Write(XmlWriter writer) { Write(writer, _parameters); } /// /// Find all the tags which are used as inputs to this task /// /// The tag names which are read by this task public override IEnumerable FindConsumedTagNames() { yield break; } /// /// Find all the tags which are modified by this task /// /// The tag names which are modified by this task public override IEnumerable FindProducedTagNames() { return FindTagNamesFromList(_parameters.Tag); } } }