Skip to content

Commit 75fcc68

Browse files
committed
Merge branch 'feature/better-autoclient'
2 parents a02d955 + a41272f commit 75fcc68

22 files changed

+348
-551
lines changed

.github/workflows/docker-autoclientcenter.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

Tools/AutoClient/App.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Logging;
2+
3+
namespace AutoClient
4+
{
5+
public class App
6+
{
7+
public App(Configuration config)
8+
{
9+
Config = config;
10+
11+
Log = new LogSplitter(
12+
new FileLog(Path.Combine(config.LogPath, "autoclient")),
13+
new ConsoleLog()
14+
);
15+
16+
Generator = CreateGenerator();
17+
CidRepo = new CidRepo(config);
18+
}
19+
20+
public Configuration Config { get; }
21+
public ILog Log { get; }
22+
public IFileGenerator Generator { get; }
23+
public CancellationTokenSource Cts { get; } = new CancellationTokenSource();
24+
public CidRepo CidRepo { get; }
25+
public Performance Performance { get; } = new Performance();
26+
27+
private IFileGenerator CreateGenerator()
28+
{
29+
if (Config.FileSizeMb > 0)
30+
{
31+
return new RandomFileGenerator(Config, Log);
32+
}
33+
return new ImageGenerator(Log);
34+
}
35+
}
36+
}

Tools/AutoClientCenter/CidRepo.cs renamed to Tools/AutoClient/CidRepo.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
namespace AutoClientCenter
1+
namespace AutoClient
22
{
33
public class CidRepo
44
{
55
private readonly Random random = new Random();
66
private readonly object _lock = new object();
77
private readonly List<CidEntry> entries = new List<CidEntry>();
8+
private readonly Configuration config;
89

9-
public void Add(string cid, long knownSize)
10+
public CidRepo(Configuration config)
11+
{
12+
this.config = config;
13+
}
14+
15+
public void Add(string nodeId, string cid, long knownSize)
1016
{
1117
lock (_lock)
1218
{
13-
entries.Add(new CidEntry(cid, knownSize));
19+
entries.Add(new CidEntry(nodeId, cid, knownSize));
1420
}
1521
}
1622

@@ -25,31 +31,32 @@ public void AddEncoded(string originalCid, string encodedCid)
2531
}
2632
}
2733

28-
public void Assign(AcDownloadStep downloadStep)
34+
public string? GetForeignCid(string myNodeId)
2935
{
3036
lock (_lock)
3137
{
3238
while (true)
3339
{
34-
if (!entries.Any()) return;
40+
if (!entries.Any()) return null;
41+
var available = entries.Where(e => e.NodeId != myNodeId).ToArray();
42+
if (!available.Any()) return null;
3543

36-
var i = random.Next(0, entries.Count);
37-
var entry = entries[i];
44+
var i = random.Next(0, available.Length);
45+
var entry = available[i];
3846

39-
if (entry.CreatedUtc < (DateTime.UtcNow + TimeSpan.FromHours(18)))
47+
if (entry.CreatedUtc < (DateTime.UtcNow + TimeSpan.FromMinutes(config.ContractDurationMinutes)))
4048
{
41-
entries.RemoveAt(i);
49+
entries.Remove(entry);
4250
}
4351
else
4452
{
45-
downloadStep.Cid = entry.Cid;
46-
return;
53+
return entry.Cid;
4754
}
4855
}
4956
}
5057
}
5158

52-
public long? GetSizeKbsForCid(string cid)
59+
public long? GetSizeForCid(string cid)
5360
{
5461
lock (_lock)
5562
{
@@ -62,12 +69,14 @@ public void Assign(AcDownloadStep downloadStep)
6269

6370
public class CidEntry
6471
{
65-
public CidEntry(string cid, long knownSize)
72+
public CidEntry(string nodeId, string cid, long knownSize)
6673
{
74+
NodeId = nodeId;
6775
Cid = cid;
6876
KnownSize = knownSize;
6977
}
7078

79+
public string NodeId { get; }
7180
public string Cid { get; }
7281
public string Encoded { get; set; } = string.Empty;
7382
public long KnownSize { get; }

Tools/AutoClient/CodexUser.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using CodexOpenApi;
2+
using Logging;
3+
using Utils;
4+
5+
namespace AutoClient
6+
{
7+
public class CodexUser
8+
{
9+
private readonly App app;
10+
private readonly CodexApi codex;
11+
private readonly HttpClient client;
12+
private readonly Address address;
13+
private readonly List<Purchaser> purchasers = new List<Purchaser>();
14+
private Task starterTask = Task.CompletedTask;
15+
private readonly string nodeId = Guid.NewGuid().ToString();
16+
17+
public CodexUser(App app, CodexApi codex, HttpClient client, Address address)
18+
{
19+
this.app = app;
20+
this.codex = codex;
21+
this.client = client;
22+
this.address = address;
23+
}
24+
25+
public void Start(int index)
26+
{
27+
for (var i = 0; i < app.Config.NumConcurrentPurchases; i++)
28+
{
29+
purchasers.Add(new Purchaser(app, nodeId, new LogPrefixer(app.Log, $"({i}) "), client, address, codex));
30+
}
31+
32+
var delayPerPurchaser =
33+
TimeSpan.FromSeconds(10 * index) +
34+
TimeSpan.FromMinutes(app.Config.ContractDurationMinutes) / app.Config.NumConcurrentPurchases;
35+
36+
starterTask = Task.Run(() => StartPurchasers(delayPerPurchaser));
37+
}
38+
39+
private async Task StartPurchasers(TimeSpan delayPerPurchaser)
40+
{
41+
foreach (var purchaser in purchasers)
42+
{
43+
purchaser.Start();
44+
await Task.Delay(delayPerPurchaser);
45+
}
46+
}
47+
48+
public void Stop()
49+
{
50+
starterTask.Wait();
51+
foreach (var purchaser in purchasers)
52+
{
53+
purchaser.Stop();
54+
}
55+
}
56+
}
57+
}

Tools/AutoClient/Configuration.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ namespace AutoClient
44
{
55
public class Configuration
66
{
7-
[Uniform("codex-host", "ch", "CODEXHOST", false, "Codex Host address. (default 'http://localhost')")]
8-
public string CodexHost { get; set; } = "http://localhost";
9-
10-
[Uniform("codex-port", "cp", "CODEXPORT", false, "port number of Codex API. (8080 by default)")]
11-
public int CodexPort { get; set; } = 8080;
7+
[Uniform("codex-endpoints", "ce", "CODEXENDPOINTS", false, "Codex endpoints. Semi-colon separated. (default 'http://localhost:8080')")]
8+
public string CodexEndpoints { get; set; } = "http://localhost:8080";
129

1310
[Uniform("datapath", "dp", "DATAPATH", false, "Root path where all data files will be saved.")]
1411
public string DataPath { get; set; } = "datapath";

Tools/AutoClient/ImageGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ public interface IFileGenerator
1111

1212
public class ImageGenerator : IFileGenerator
1313
{
14-
private LogSplitter log;
14+
private readonly ILog log;
1515

16-
public ImageGenerator(LogSplitter log)
16+
public ImageGenerator(ILog log)
1717
{
1818
this.log = log;
1919
}
2020

2121
public async Task<string> Generate()
2222
{
23-
log.Log("Fetching random image from picsum.photos...");
23+
log.Debug("Fetching random image from picsum.photos...");
2424
var httpClient = new HttpClient();
2525
var thing = await httpClient.GetStreamAsync("https://picsum.photos/3840/2160");
2626

Tools/AutoClient/Performance.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace AutoClient
2+
{
3+
public class Performance
4+
{
5+
internal void DownloadFailed(Exception ex)
6+
{
7+
throw new NotImplementedException();
8+
}
9+
10+
internal void DownloadSuccessful(long? size, TimeSpan time)
11+
{
12+
throw new NotImplementedException();
13+
}
14+
15+
internal void StorageContractCancelled()
16+
{
17+
throw new NotImplementedException();
18+
}
19+
20+
internal void StorageContractErrored(string error)
21+
{
22+
throw new NotImplementedException();
23+
}
24+
25+
internal void StorageContractFinished()
26+
{
27+
throw new NotImplementedException();
28+
}
29+
30+
internal void StorageContractStarted()
31+
{
32+
throw new NotImplementedException();
33+
}
34+
35+
internal void UploadFailed(Exception exc)
36+
{
37+
throw new NotImplementedException();
38+
}
39+
40+
internal void UploadSuccessful(long length, TimeSpan time)
41+
{
42+
throw new NotImplementedException();
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)