From 88daab379f67a000d169d14e2540a065bafc80d6 Mon Sep 17 00:00:00 2001 From: benbierens Date: Fri, 23 Aug 2024 13:21:31 +0200 Subject: [PATCH] Allows autoclient to generate files of any size with random data --- Tools/AutoClient/Configuration.cs | 9 ++++-- Tools/AutoClient/ImageGenerator.cs | 44 ++++++++++++++++++++++++++++-- Tools/AutoClient/Program.cs | 16 +++++++++-- Tools/AutoClient/Purchaser.cs | 6 ++-- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/Tools/AutoClient/Configuration.cs b/Tools/AutoClient/Configuration.cs index a380882..d21deb5 100644 --- a/Tools/AutoClient/Configuration.cs +++ b/Tools/AutoClient/Configuration.cs @@ -16,10 +16,10 @@ namespace AutoClient [Uniform("purchases", "np", "PURCHASES", false, "Number of concurrent purchases.")] public int NumConcurrentPurchases { get; set; } = 10; - [Uniform("contract-duration", "cd", "CONTRACTDURATION", false, "contract duration in minutes. (default 30)")] - public int ContractDurationMinutes { get; set; } = 30; + [Uniform("contract-duration", "cd", "CONTRACTDURATION", false, "contract duration in minutes. (default 6 hours)")] + public int ContractDurationMinutes { get; set; } = 60 * 6; - [Uniform("contract-expiry", "ce", "CONTRACTEXPIRY", false, "contract expiry in minutes. (default 15)")] + [Uniform("contract-expiry", "ce", "CONTRACTEXPIRY", false, "contract expiry in minutes. (default 15 minutes)")] public int ContractExpiryMinutes { get; set; } = 15; [Uniform("num-hosts", "nh", "NUMHOSTS", false, "Number of hosts for contract. (default 5)")] @@ -34,6 +34,9 @@ namespace AutoClient [Uniform("collateral", "c", "COLLATERAL", false, "Required collateral. (default 1)")] public int RequiredCollateral { get; set; } = 1; + [Uniform("filesizemb", "smb", "FILESIZEMB", false, "When greater than zero, size of file generated and uploaded. When zero, random images are used instead.")] + public int FileSizeMb { get; set; } = 0; + public string LogPath { get diff --git a/Tools/AutoClient/ImageGenerator.cs b/Tools/AutoClient/ImageGenerator.cs index eeab0d3..390381b 100644 --- a/Tools/AutoClient/ImageGenerator.cs +++ b/Tools/AutoClient/ImageGenerator.cs @@ -1,9 +1,26 @@ -namespace AutoClient +using FileUtils; +using Logging; +using Utils; + +namespace AutoClient { - public class ImageGenerator + public interface IFileGenerator { - public async Task GenerateImage() + Task Generate(); + } + + public class ImageGenerator : IFileGenerator + { + private LogSplitter log; + + public ImageGenerator(LogSplitter log) { + this.log = log; + } + + public async Task Generate() + { + log.Log("Fetching random image from picsum.photos..."); var httpClient = new HttpClient(); var thing = await httpClient.GetStreamAsync("https://picsum.photos/3840/2160"); @@ -14,4 +31,25 @@ return filename; } } + + public class RandomFileGenerator : IFileGenerator + { + private readonly ByteSize size; + private readonly FileManager fileManager; + + public RandomFileGenerator(Configuration config, ILog log) + { + size = config.FileSizeMb.MB(); + fileManager = new FileManager(log, config.DataPath); + } + + public Task Generate() + { + return Task.Run(() => + { + var file = fileManager.GenerateFile(size); + return file.Filename; + }); + } + } } diff --git a/Tools/AutoClient/Program.cs b/Tools/AutoClient/Program.cs index fbd144a..d5c4aa0 100644 --- a/Tools/AutoClient/Program.cs +++ b/Tools/AutoClient/Program.cs @@ -3,6 +3,7 @@ using AutoClient; using CodexOpenApi; using Core; using Logging; +using Utils; public static class Program { @@ -25,14 +26,14 @@ public static class Program new ConsoleLog() ); - var address = new Utils.Address( + var address = new Address( host: config.CodexHost, port: config.CodexPort ); log.Log($"Start. Address: {address}"); - var imgGenerator = new ImageGenerator(); + var generator = CreateGenerator(config, log); var client = new HttpClient(); var codex = new CodexApi(client); @@ -44,7 +45,7 @@ public static class Program for (var i = 0; i < config.NumConcurrentPurchases; i++) { purchasers.Add( - new Purchaser(new LogPrefixer(log, $"({i}) "), client, address, codex, config, imgGenerator, cancellationToken) + new Purchaser(new LogPrefixer(log, $"({i}) "), client, address, codex, config, generator, cancellationToken) ); } @@ -60,6 +61,15 @@ public static class Program log.Log("Done."); } + private static IFileGenerator CreateGenerator(Configuration config, LogSplitter log) + { + if (config.FileSizeMb > 0) + { + return new RandomFileGenerator(config, log); + } + return new ImageGenerator(log); + } + private static async Task CheckCodex(CodexApi codex, ILog log) { log.Log("Checking Codex..."); diff --git a/Tools/AutoClient/Purchaser.cs b/Tools/AutoClient/Purchaser.cs index a9ec89c..d0d6995 100644 --- a/Tools/AutoClient/Purchaser.cs +++ b/Tools/AutoClient/Purchaser.cs @@ -13,10 +13,10 @@ namespace AutoClient private readonly Address address; private readonly CodexApi codex; private readonly Configuration config; - private readonly ImageGenerator generator; + private readonly IFileGenerator generator; private readonly CancellationToken ct; - public Purchaser(ILog log, HttpClient client, Address address, CodexApi codex, Configuration config, ImageGenerator generator, CancellationToken ct) + public Purchaser(ILog log, HttpClient client, Address address, CodexApi codex, Configuration config, IFileGenerator generator, CancellationToken ct) { this.log = log; this.client = client; @@ -50,7 +50,7 @@ namespace AutoClient private async Task CreateFile() { - return await generator.GenerateImage(); + return await generator.Generate(); } private async Task UploadFile(string filename)