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