diff --git a/Tools/AutoClient/Purchaser.cs b/Tools/AutoClient/AutomaticPurchaser.cs similarity index 89% rename from Tools/AutoClient/Purchaser.cs rename to Tools/AutoClient/AutomaticPurchaser.cs index b846f4a0..ea252348 100644 --- a/Tools/AutoClient/Purchaser.cs +++ b/Tools/AutoClient/AutomaticPurchaser.cs @@ -6,23 +6,16 @@ using Utils; namespace AutoClient { - public class Purchaser + public class AutomaticPurchaser { - private readonly App app; - private readonly string nodeId; private readonly ILog log; - private readonly HttpClient client; - private readonly Address address; - private readonly CodexApi codex; + private readonly ICodexInstance codex; private Task workerTask = Task.CompletedTask; + private App app => codex.App; - public Purchaser(App app, string nodeId, ILog log, HttpClient client, Address address, CodexApi codex) + public AutomaticPurchaser(ILog log, ICodexInstance codex) { - this.app = app; - this.nodeId = nodeId; this.log = log; - this.client = client; - this.address = address; this.codex = codex; } @@ -57,7 +50,7 @@ namespace AutoClient private async Task DownloadForeignCid() { - var cid = app.CidRepo.GetForeignCid(nodeId); + var cid = app.CidRepo.GetForeignCid(codex.NodeId); if (cid == null) return; var size = app.CidRepo.GetSizeForCid(cid); if (size == null) return; @@ -68,7 +61,7 @@ namespace AutoClient var filename = Guid.NewGuid().ToString().ToLowerInvariant(); { using var fileStream = File.OpenWrite(filename); - var fileResponse = await codex.DownloadNetworkStreamAsync(cid); + var fileResponse = await codex.Codex.DownloadNetworkStreamAsync(cid); fileResponse.Stream.CopyTo(fileStream); } var time = sw.Elapsed; @@ -122,7 +115,7 @@ namespace AutoClient var cid = await UploadStream(fileStream, filename); var time = sw.Elapsed; app.Performance.UploadSuccessful(info.Length, time); - app.CidRepo.Add(nodeId, cid.Id, info.Length); + app.CidRepo.Add(codex.NodeId, cid.Id, info.Length); return cid; } catch (Exception exc) @@ -135,7 +128,7 @@ namespace AutoClient private async Task UploadStream(FileStream fileStream, string filename) { log.Debug($"Uploading file..."); - var response = await codex.UploadAsync( + var response = await codex.Codex.UploadAsync( content_type: "application/x-binary", content_disposition: $"attachment; filename=\"{filename}\"", fileStream, app.Cts.Token); @@ -150,7 +143,7 @@ namespace AutoClient private async Task RequestStorage(ContentId cid) { log.Debug("Requesting storage for " + cid.Id); - var result = await codex.CreateStorageRequestAsync(cid.Id, new StorageRequestCreation() + var result = await codex.Codex.CreateStorageRequestAsync(cid.Id, new StorageRequestCreation() { Collateral = app.Config.RequiredCollateral.ToString(), Duration = (app.Config.ContractDurationMinutes * 60).ToString(), @@ -186,7 +179,7 @@ namespace AutoClient private async Task GetStoragePurchase(string pid) { // openapi still don't match code. - var str = await client.GetStringAsync($"{address.Host}:{address.Port}/api/codex/v1/storage/purchases/{pid}"); + var str = await codex.Client.GetStringAsync($"{codex.Address.Host}:{codex.Address.Port}/api/codex/v1/storage/purchases/{pid}"); if (string.IsNullOrEmpty(str)) return null; return JsonConvert.DeserializeObject(str); } diff --git a/Tools/AutoClient/CodexInstance.cs b/Tools/AutoClient/CodexInstance.cs new file mode 100644 index 00000000..5b179fc0 --- /dev/null +++ b/Tools/AutoClient/CodexInstance.cs @@ -0,0 +1,33 @@ +using CodexOpenApi; +using Logging; +using Utils; + +namespace AutoClient +{ + public interface ICodexInstance + { + string NodeId { get; } + App App { get; } + CodexApi Codex { get; } + HttpClient Client { get; } + Address Address { get; } + } + + public class CodexInstance : ICodexInstance + { + public CodexInstance(App app, CodexApi codex, HttpClient client, Address address) + { + App = app; + Codex = codex; + Client = client; + Address = address; + NodeId = Guid.NewGuid().ToString(); + } + + public string NodeId { get; } + public App App { get; } + public CodexApi Codex { get; } + public HttpClient Client { get; } + public Address Address { get; } + } +} diff --git a/Tools/AutoClient/Modes/FolderStoreMode.cs b/Tools/AutoClient/Modes/FolderStoreMode.cs new file mode 100644 index 00000000..85c162b7 --- /dev/null +++ b/Tools/AutoClient/Modes/FolderStoreMode.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutoClient.Modes +{ + public class FolderStoreMode : IMode + { + public void Start(ICodexInstance instance, int index) + { + throw new NotImplementedException(); + } + + public void Stop() + { + throw new NotImplementedException(); + } + } +} diff --git a/Tools/AutoClient/Modes/Mode.cs b/Tools/AutoClient/Modes/Mode.cs new file mode 100644 index 00000000..5ab33a10 --- /dev/null +++ b/Tools/AutoClient/Modes/Mode.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutoClient.Modes +{ + public interface IMode + { + void Start(ICodexInstance instance, int index); + void Stop(); + } +} diff --git a/Tools/AutoClient/CodexUser.cs b/Tools/AutoClient/Modes/PurchasingMode.cs similarity index 58% rename from Tools/AutoClient/CodexUser.cs rename to Tools/AutoClient/Modes/PurchasingMode.cs index fd3259c9..f4be3ce8 100644 --- a/Tools/AutoClient/CodexUser.cs +++ b/Tools/AutoClient/Modes/PurchasingMode.cs @@ -1,32 +1,23 @@ -using CodexOpenApi; -using Logging; -using Utils; +using Logging; -namespace AutoClient +namespace AutoClient.Modes { - public class CodexUser + public class PurchasingMode : IMode { + private readonly List purchasers = new List(); private readonly App app; - private readonly CodexApi codex; - private readonly HttpClient client; - private readonly Address address; - private readonly List purchasers = new List(); private Task starterTask = Task.CompletedTask; - private readonly string nodeId = Guid.NewGuid().ToString(); - public CodexUser(App app, CodexApi codex, HttpClient client, Address address) + public PurchasingMode(App app) { this.app = app; - this.codex = codex; - this.client = client; - this.address = address; } - public void Start(int index) + public void Start(ICodexInstance instance, int index) { for (var i = 0; i < app.Config.NumConcurrentPurchases; i++) { - purchasers.Add(new Purchaser(app, nodeId, new LogPrefixer(app.Log, $"({i}) "), client, address, codex)); + purchasers.Add(new AutomaticPurchaser(new LogPrefixer(app.Log, $"({i}) "), instance)); } var delayPerPurchaser = diff --git a/Tools/AutoClient/Program.cs b/Tools/AutoClient/Program.cs index 6d1c1fd2..a2f1c563 100644 --- a/Tools/AutoClient/Program.cs +++ b/Tools/AutoClient/Program.cs @@ -1,11 +1,13 @@ using ArgsUniform; using AutoClient; +using AutoClient.Modes; using CodexOpenApi; using Utils; public class Program { private readonly App app; + private readonly List modes = new List(); public Program(Configuration config) { @@ -31,26 +33,35 @@ public class Program public async Task Run() { - var codexUsers = await CreateUsers(); + var codexInstances = await CreateCodexInstances(); var i = 0; - foreach (var user in codexUsers) + foreach (var cdx in codexInstances) { - user.Start(i); + var mode = CreateMode(); + modes.Add(mode); + + mode.Start(cdx, i); i++; } app.Cts.Token.WaitHandle.WaitOne(); - foreach (var user in codexUsers) user.Stop(); + foreach (var mode in modes) mode.Stop(); + modes.Clear(); app.Log.Log("Done"); } - private async Task CreateUsers() + private IMode CreateMode() + { + return new PurchasingMode(app); + } + + private async Task CreateCodexInstances() { var endpointStrs = app.Config.CodexEndpoints.Split(";", StringSplitOptions.RemoveEmptyEntries); - var result = new List(); + var result = new List(); foreach (var e in endpointStrs) { @@ -60,7 +71,7 @@ public class Program return result.ToArray(); } - private async Task CreateUser(string endpoint) + private async Task CreateUser(string endpoint) { var splitIndex = endpoint.LastIndexOf(':'); var host = endpoint.Substring(0, splitIndex); @@ -79,7 +90,7 @@ public class Program await CheckCodex(codex); app.Log.Log("OK"); - return new CodexUser( + return new CodexInstance( app, codex, client, @@ -105,4 +116,4 @@ public class Program { Console.WriteLine("Generates fake data and creates Codex storage contracts for it."); } -} \ No newline at end of file +}