Skip to content

Commit 88daab3

Browse files
committed
Allows autoclient to generate files of any size with random data
1 parent 00ed3ca commit 88daab3

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

Tools/AutoClient/Configuration.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public class Configuration
1616
[Uniform("purchases", "np", "PURCHASES", false, "Number of concurrent purchases.")]
1717
public int NumConcurrentPurchases { get; set; } = 10;
1818

19-
[Uniform("contract-duration", "cd", "CONTRACTDURATION", false, "contract duration in minutes. (default 30)")]
20-
public int ContractDurationMinutes { get; set; } = 30;
19+
[Uniform("contract-duration", "cd", "CONTRACTDURATION", false, "contract duration in minutes. (default 6 hours)")]
20+
public int ContractDurationMinutes { get; set; } = 60 * 6;
2121

22-
[Uniform("contract-expiry", "ce", "CONTRACTEXPIRY", false, "contract expiry in minutes. (default 15)")]
22+
[Uniform("contract-expiry", "ce", "CONTRACTEXPIRY", false, "contract expiry in minutes. (default 15 minutes)")]
2323
public int ContractExpiryMinutes { get; set; } = 15;
2424

2525
[Uniform("num-hosts", "nh", "NUMHOSTS", false, "Number of hosts for contract. (default 5)")]
@@ -34,6 +34,9 @@ public class Configuration
3434
[Uniform("collateral", "c", "COLLATERAL", false, "Required collateral. (default 1)")]
3535
public int RequiredCollateral { get; set; } = 1;
3636

37+
[Uniform("filesizemb", "smb", "FILESIZEMB", false, "When greater than zero, size of file generated and uploaded. When zero, random images are used instead.")]
38+
public int FileSizeMb { get; set; } = 0;
39+
3740
public string LogPath
3841
{
3942
get

Tools/AutoClient/ImageGenerator.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
namespace AutoClient
1+
using FileUtils;
2+
using Logging;
3+
using Utils;
4+
5+
namespace AutoClient
26
{
3-
public class ImageGenerator
7+
public interface IFileGenerator
48
{
5-
public async Task<string> GenerateImage()
9+
Task<string> Generate();
10+
}
11+
12+
public class ImageGenerator : IFileGenerator
13+
{
14+
private LogSplitter log;
15+
16+
public ImageGenerator(LogSplitter log)
617
{
18+
this.log = log;
19+
}
20+
21+
public async Task<string> Generate()
22+
{
23+
log.Log("Fetching random image from picsum.photos...");
724
var httpClient = new HttpClient();
825
var thing = await httpClient.GetStreamAsync("https://picsum.photos/3840/2160");
926

@@ -14,4 +31,25 @@ public async Task<string> GenerateImage()
1431
return filename;
1532
}
1633
}
34+
35+
public class RandomFileGenerator : IFileGenerator
36+
{
37+
private readonly ByteSize size;
38+
private readonly FileManager fileManager;
39+
40+
public RandomFileGenerator(Configuration config, ILog log)
41+
{
42+
size = config.FileSizeMb.MB();
43+
fileManager = new FileManager(log, config.DataPath);
44+
}
45+
46+
public Task<string> Generate()
47+
{
48+
return Task.Run(() =>
49+
{
50+
var file = fileManager.GenerateFile(size);
51+
return file.Filename;
52+
});
53+
}
54+
}
1755
}

Tools/AutoClient/Program.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using CodexOpenApi;
44
using Core;
55
using Logging;
6+
using Utils;
67

78
public static class Program
89
{
@@ -25,14 +26,14 @@ public static async Task Main(string[] args)
2526
new ConsoleLog()
2627
);
2728

28-
var address = new Utils.Address(
29+
var address = new Address(
2930
host: config.CodexHost,
3031
port: config.CodexPort
3132
);
3233

3334
log.Log($"Start. Address: {address}");
3435

35-
var imgGenerator = new ImageGenerator();
36+
var generator = CreateGenerator(config, log);
3637

3738
var client = new HttpClient();
3839
var codex = new CodexApi(client);
@@ -44,7 +45,7 @@ public static async Task Main(string[] args)
4445
for (var i = 0; i < config.NumConcurrentPurchases; i++)
4546
{
4647
purchasers.Add(
47-
new Purchaser(new LogPrefixer(log, $"({i}) "), client, address, codex, config, imgGenerator, cancellationToken)
48+
new Purchaser(new LogPrefixer(log, $"({i}) "), client, address, codex, config, generator, cancellationToken)
4849
);
4950
}
5051

@@ -60,6 +61,15 @@ public static async Task Main(string[] args)
6061
log.Log("Done.");
6162
}
6263

64+
private static IFileGenerator CreateGenerator(Configuration config, LogSplitter log)
65+
{
66+
if (config.FileSizeMb > 0)
67+
{
68+
return new RandomFileGenerator(config, log);
69+
}
70+
return new ImageGenerator(log);
71+
}
72+
6373
private static async Task CheckCodex(CodexApi codex, ILog log)
6474
{
6575
log.Log("Checking Codex...");

Tools/AutoClient/Purchaser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public class Purchaser
1313
private readonly Address address;
1414
private readonly CodexApi codex;
1515
private readonly Configuration config;
16-
private readonly ImageGenerator generator;
16+
private readonly IFileGenerator generator;
1717
private readonly CancellationToken ct;
1818

19-
public Purchaser(ILog log, HttpClient client, Address address, CodexApi codex, Configuration config, ImageGenerator generator, CancellationToken ct)
19+
public Purchaser(ILog log, HttpClient client, Address address, CodexApi codex, Configuration config, IFileGenerator generator, CancellationToken ct)
2020
{
2121
this.log = log;
2222
this.client = client;
@@ -50,7 +50,7 @@ private async Task<string> StartNewPurchase()
5050

5151
private async Task<string> CreateFile()
5252
{
53-
return await generator.GenerateImage();
53+
return await generator.Generate();
5454
}
5555

5656
private async Task<ContentId> UploadFile(string filename)

0 commit comments

Comments
 (0)