cs-codex-dist-tests/Tools/AutoClient/Program.cs

82 lines
2.3 KiB
C#
Raw Permalink Normal View History

2024-04-01 18:40:03 +00:00
using ArgsUniform;
using AutoClient;
using CodexOpenApi;
2024-04-01 18:40:03 +00:00
using Core;
using Logging;
public static class Program
{
public static async Task Main(string[] args)
2024-04-01 18:40:03 +00:00
{
var cts = new CancellationTokenSource();
var cancellationToken = cts.Token;
Console.CancelKeyPress += (sender, args) => cts.Cancel();
var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args);
var config = uniformArgs.Parse(true);
2024-06-28 06:47:09 +00:00
if (config.NumConcurrentPurchases < 1)
{
throw new Exception("Number of concurrent purchases must be > 0");
}
2024-04-01 18:40:03 +00:00
var log = new LogSplitter(
new FileLog(Path.Combine(config.LogPath, "autoclient")),
new ConsoleLog()
);
var address = new Utils.Address(
host: config.CodexHost,
port: config.CodexPort
);
log.Log($"Start. Address: {address}");
var imgGenerator = new ImageGenerator();
var client = new HttpClient();
var codex = new CodexApi(client);
codex.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1";
2024-04-01 18:40:03 +00:00
await CheckCodex(codex, log);
2024-04-01 18:47:56 +00:00
2024-06-28 06:47:09 +00:00
var purchasers = new List<Purchaser>();
for (var i = 0; i < config.NumConcurrentPurchases; i++)
{
purchasers.Add(
2024-07-23 08:29:01 +00:00
new Purchaser(new LogPrefixer(log, $"({i}) "), client, address, codex, config, imgGenerator, cancellationToken)
2024-06-28 06:47:09 +00:00
);
}
var delayPerPurchaser = TimeSpan.FromMinutes(config.ContractDurationMinutes) / config.NumConcurrentPurchases;
foreach (var purchaser in purchasers)
{
purchaser.Start();
await Task.Delay(delayPerPurchaser);
}
2024-04-01 18:40:03 +00:00
2024-07-23 07:58:29 +00:00
cancellationToken.WaitHandle.WaitOne();
2024-04-01 18:40:03 +00:00
log.Log("Done.");
}
private static async Task CheckCodex(CodexApi codex, ILog log)
2024-04-01 18:47:56 +00:00
{
log.Log("Checking Codex...");
try
{
var info = await codex.GetDebugInfoAsync();
2024-04-01 18:47:56 +00:00
if (string.IsNullOrEmpty(info.Id)) throw new Exception("Failed to fetch Codex node id");
}
catch (Exception ex)
{
log.Log($"Codex not OK: {ex}");
throw;
}
}
2024-04-01 18:40:03 +00:00
private static void PrintHelp()
{
Console.WriteLine("Generates fake data and creates Codex storage contracts for it.");
}
}