2
0
mirror of synced 2025-02-02 19:53:29 +00:00

Allows autoclient to generate files of any size with random data

This commit is contained in:
benbierens 2024-08-23 13:21:31 +02:00
parent 00ed3caafe
commit 88daab379f
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
4 changed files with 63 additions and 12 deletions

View File

@ -16,10 +16,10 @@ namespace AutoClient
[Uniform("purchases", "np", "PURCHASES", false, "Number of concurrent purchases.")] [Uniform("purchases", "np", "PURCHASES", false, "Number of concurrent purchases.")]
public int NumConcurrentPurchases { get; set; } = 10; public int NumConcurrentPurchases { get; set; } = 10;
[Uniform("contract-duration", "cd", "CONTRACTDURATION", false, "contract duration in minutes. (default 30)")] [Uniform("contract-duration", "cd", "CONTRACTDURATION", false, "contract duration in minutes. (default 6 hours)")]
public int ContractDurationMinutes { get; set; } = 30; 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; public int ContractExpiryMinutes { get; set; } = 15;
[Uniform("num-hosts", "nh", "NUMHOSTS", false, "Number of hosts for contract. (default 5)")] [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)")] [Uniform("collateral", "c", "COLLATERAL", false, "Required collateral. (default 1)")]
public int RequiredCollateral { get; set; } = 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 public string LogPath
{ {
get get

View File

@ -1,9 +1,26 @@
namespace AutoClient using FileUtils;
using Logging;
using Utils;
namespace AutoClient
{ {
public class ImageGenerator public interface IFileGenerator
{ {
public async Task<string> GenerateImage() Task<string> Generate();
}
public class ImageGenerator : IFileGenerator
{ {
private LogSplitter log;
public ImageGenerator(LogSplitter log)
{
this.log = log;
}
public async Task<string> Generate()
{
log.Log("Fetching random image from picsum.photos...");
var httpClient = new HttpClient(); var httpClient = new HttpClient();
var thing = await httpClient.GetStreamAsync("https://picsum.photos/3840/2160"); var thing = await httpClient.GetStreamAsync("https://picsum.photos/3840/2160");
@ -14,4 +31,25 @@
return filename; 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<string> Generate()
{
return Task.Run(() =>
{
var file = fileManager.GenerateFile(size);
return file.Filename;
});
}
}
} }

View File

@ -3,6 +3,7 @@ using AutoClient;
using CodexOpenApi; using CodexOpenApi;
using Core; using Core;
using Logging; using Logging;
using Utils;
public static class Program public static class Program
{ {
@ -25,14 +26,14 @@ public static class Program
new ConsoleLog() new ConsoleLog()
); );
var address = new Utils.Address( var address = new Address(
host: config.CodexHost, host: config.CodexHost,
port: config.CodexPort port: config.CodexPort
); );
log.Log($"Start. Address: {address}"); log.Log($"Start. Address: {address}");
var imgGenerator = new ImageGenerator(); var generator = CreateGenerator(config, log);
var client = new HttpClient(); var client = new HttpClient();
var codex = new CodexApi(client); var codex = new CodexApi(client);
@ -44,7 +45,7 @@ public static class Program
for (var i = 0; i < config.NumConcurrentPurchases; i++) for (var i = 0; i < config.NumConcurrentPurchases; i++)
{ {
purchasers.Add( 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."); 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) private static async Task CheckCodex(CodexApi codex, ILog log)
{ {
log.Log("Checking Codex..."); log.Log("Checking Codex...");

View File

@ -13,10 +13,10 @@ namespace AutoClient
private readonly Address address; private readonly Address address;
private readonly CodexApi codex; private readonly CodexApi codex;
private readonly Configuration config; private readonly Configuration config;
private readonly ImageGenerator generator; private readonly IFileGenerator generator;
private readonly CancellationToken ct; 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.log = log;
this.client = client; this.client = client;
@ -50,7 +50,7 @@ namespace AutoClient
private async Task<string> CreateFile() private async Task<string> CreateFile()
{ {
return await generator.GenerateImage(); return await generator.Generate();
} }
private async Task<ContentId> UploadFile(string filename) private async Task<ContentId> UploadFile(string filename)