-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChunkingTest.cs
More file actions
373 lines (329 loc) · 14.2 KB
/
ChunkingTest.cs
File metadata and controls
373 lines (329 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Cognite.Extensions;
using Cognite.Extractor.Common;
using Cognite.Extractor.Testing;
using CogniteSdk;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
namespace ExtractorUtils.Test.Unit
{
public class ChunkingTest
{
private readonly ITestOutputHelper _output;
public ChunkingTest(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public async Task RunThrottledOK()
{
var completed = new List<int>();
var token = CancellationToken.None;
var taskNum = 0;
var generators = Enumerable.Range(1, 5).Select<int, Func<Task>>(
(i) => async () =>
{
_output.WriteLine($"Starting {i}");
await Task.Delay(i * 100, token);
_output.WriteLine($"Completed {i}");
completed.Add(i);
});
void taskDone(Task task) { _output.WriteLine($"Task completed {++taskNum}: {task.Id} - {task.Status}"); }
await generators.RunThrottled(2, taskDone, token);
Assert.Equal(5, completed.Count);
Assert.Equal(15, completed.Sum());
}
[Fact]
public async Task RunThrottledException()
{
var completed = new List<int>();
var token = CancellationToken.None;
var generators = Enumerable.Range(1, 5).Select<int, Func<Task>>(
i => async () =>
{
_output.WriteLine($"Starting {i}");
await Task.Delay(i * 100, token);
if (i == 3)
{
throw new Exception("Failed on 3!!!");
}
_output.WriteLine($"Completed {i}");
completed.Add(i);
});
await Assert.ThrowsAsync<Exception>(
async () =>
{
await generators.RunThrottled(2, token);
await Task.Delay(2000);
}
);
Assert.Contains(1, completed);
// may or may not contain 2
Assert.DoesNotContain(3, completed);
}
[Theory]
[InlineData(1, 1)]
[InlineData(10_000, 100_000)]
[InlineData(10_000, 123_456)]
[InlineData(999, 1_000_000)]
public void TestEnumerableChunkBy(int chunkSize, int datapoints)
{
var left = datapoints % chunkSize;
var numChunks = datapoints / chunkSize + (left > 0 ? 1 : 0);
Datapoint[] dps = new Datapoint[datapoints];
for (int i = 0; i < datapoints; i++)
{
dps[i] = new Datapoint(DateTime.UtcNow, i * 0.01);
}
var chunks = dps.ChunkBy(chunkSize);
var count = chunks.Count();
var sum = chunks.Select(c => c.Count()).Sum();
var last = chunks.Last().Count();
Assert.Equal(numChunks, count);
Assert.Equal(datapoints, sum);
if (left > 0)
{
Assert.Equal(left, last);
}
}
[Theory]
[InlineData(100_000, 10_000, 20000, 100, 20, 1000, 100000)]
[InlineData(100_000, 10_000, 200, 10000, 20, 10, 100000)]
[InlineData(100_000, 10_000, 20000, 5, 2, 10000, 50000)]
[InlineData(100, 10_000, 1, 10_000, 100, 1, 100)]
public void TestDictionaryChunking(
int dpChunk, int tsChunk,
int timeseries, int datapoints,
int expChunks, int expTimeseriesMax, int expDatapointsMax)
{
var dict = new List<(string, IEnumerable<int>)>();
for (int i = 0; i < timeseries; i++)
{
var points = new List<int>();
for (int j = 0; j < datapoints; j++)
{
points.Add(i * datapoints + j);
}
dict.Add(($"id{i}", points));
}
var results = dict.ChunkBy(dpChunk, tsChunk);
var min = results.Min(c => c.Min(e => e.Values.Count()));
Assert.True(min > 0);
var max = results.Max(c => c.Select(e => e.Values.Count()).Sum());
var maxTs = results.Max(x => x.Count());
Assert.Equal(expDatapointsMax, max);
Assert.Equal(expTimeseriesMax, maxTs);
Assert.Equal(expChunks, results.Count());
//var total = results.Sum(dct => dct.Values.Sum(val => val.Count()));
var total = results.SelectMany(x => x.Select(y => y.Values.Count())).Sum();
var totalTs = results.SelectMany(x => x.Select(y => y.Key)).ToHashSet().Count;
Assert.Equal(timeseries, totalTs);
Assert.Equal(datapoints * timeseries, total);
var exists = new bool[timeseries * datapoints];
foreach (var chunk in results)
{
foreach (var (id, values) in chunk)
{
foreach (var value in values)
{
exists[value] = true;
}
}
}
Assert.True(exists.All(val => val));
}
[Theory]
[InlineData(0, 2, 50)]
[InlineData(5, 0, 0)]
[InlineData(2, 2, 50)]
public async Task TestTaskThrottler(int maxParallelism, int maxPerUnit, int timespanMs)
{
// Running this test in github actions is pretty unreliable...
for (int i = 0; i < 5; i++)
{
try
{
using var throttler = new TaskThrottler(maxParallelism, true, maxPerUnit, TimeSpan.FromMilliseconds(timespanMs));
var generators = Enumerable.Range(0, 20)
.Select<int, Func<Task>>(_ => () => Task.Delay(100))
.ToList();
var start = DateTime.UtcNow;
foreach (var generator in generators) throttler.EnqueueTask(generator);
await throttler.WaitForCompletion();
var end = DateTime.UtcNow;
double minElapsedTimeParallel = maxParallelism == 0
? 0
: 2000 / maxParallelism;
double minElapsedTimeLimit = maxPerUnit == 0 || timespanMs == 0
? 0
: 20 / maxPerUnit * timespanMs;
var minElapsedTime = Math.Max(minElapsedTimeParallel, minElapsedTimeLimit);
var realMs = (end - start).TotalMilliseconds;
// Task overhead
var flex = 100 + realMs * 0.2;
Assert.True(realMs > minElapsedTime - flex && realMs < minElapsedTime + flex,
$"Execution took {realMs}ms but should be between {minElapsedTime - flex} and {minElapsedTime + flex}" +
$". Parallel: {minElapsedTimeParallel}, limit: {minElapsedTimeLimit}");
break;
}
catch (TrueException)
{
if (i == 4) throw;
}
}
}
[Fact]
public async Task TestTaskThrottlerResults()
{
using var throttler = new TaskThrottler(0);
static Task okGenerator() => Task.Delay(100);
static async Task badGenerator()
{
await Task.Delay(100);
Assert.True(false);
}
var result = await throttler.EnqueueAndWait(okGenerator);
Assert.Equal(0, result.Index);
Assert.Null(result.Exception);
Assert.True(result.IsCompleted);
// Turns out system clock resolution isn't actually precise enough to guarantee that delay takes as long as it says...
Assert.True((result.CompletionTime - result.StartTime).Value.TotalMilliseconds >= 80,
$"Expected task to take at least 100ms, but it took {(result.CompletionTime - result.StartTime).Value.TotalMilliseconds}ms");
var badResult = await throttler.EnqueueAndWait(badGenerator);
Assert.Equal(1, badResult.Index);
Assert.NotNull(badResult.Exception);
Assert.True(badResult.Exception is AggregateException exc && exc.InnerExceptions.First() is TrueException);
using var throttler2 = new TaskThrottler(0, true);
result = await throttler2.EnqueueAndWait(okGenerator);
Assert.Equal(0, result.Index);
Assert.Null(result.Exception);
Assert.True(result.IsCompleted);
Assert.True((result.CompletionTime - result.StartTime).Value.TotalMilliseconds >= 80);
await Assert.ThrowsAsync<AggregateException>(() => throttler2.EnqueueAndWait(badGenerator));
}
[Theory]
[InlineData(1, new[] { 3, 2, 1, 1 })]
[InlineData(2, new[] { 3, 2, 2 })]
[InlineData(3, new[] { 3, 3, 1 })]
[InlineData(4, new[] { 3, 4 })]
[InlineData(5, new[] { 5, 2 })]
[InlineData(6, new[] { 6, 1 })]
[InlineData(7, new[] { 7 })]
public void TestChunkByHierarchy(int maxSize, int[] lengths)
{
var assets = new List<AssetCreate>()
{
new AssetCreate { ExternalId = "1" },
new AssetCreate { ExternalId = "6", ParentExternalId = "5" },
new AssetCreate { ExternalId = "5", ParentExternalId = "4" },
new AssetCreate { ExternalId = "3", ParentExternalId = "2" },
new AssetCreate { ExternalId = "2" },
new AssetCreate { ExternalId = "4", ParentExternalId = "2" },
new AssetCreate { ExternalId = "7", ParentExternalId = "0" }
};
var result = assets.ChunkByHierarchy(maxSize, create => create.ExternalId, create => create.ParentExternalId).ToList();
Assert.Equal(7, result.Aggregate(0, (seed, res) => seed + res.Count()));
Assert.Equal(lengths, result.Select(res => res.Count()));
}
private async Task RunWithTimeout(Task task, int timeoutMs)
{
var retTask = await Task.WhenAny(task, Task.Delay(timeoutMs));
Assert.Equal(task, retTask);
Assert.True(task.IsCompleted);
}
[Fact(Timeout = 200000)]
public async Task TestPeriodicScheduler()
{
using var source = new CancellationTokenSource();
using var scheduler = new PeriodicScheduler(source.Token);
var loopTask = scheduler.WaitForAll();
int periodicRuns = 0;
// Schedule periodic
scheduler.SchedulePeriodicTask("periodic", TimeSpan.FromMilliseconds(100), async token =>
{
periodicRuns++;
await Task.Delay(100);
});
Assert.Throws<InvalidOperationException>(() =>
scheduler.SchedulePeriodicTask("periodic", TimeSpan.Zero, token => Task.CompletedTask));
// Schedule anonymous periodic
scheduler.SchedulePeriodicTask(null, TimeSpan.FromMilliseconds(100), async token =>
{
await Task.Delay(100);
});
Assert.Equal(2, scheduler.Count);
int singleRuns = 0;
// Schedule single
scheduler.ScheduleTask("single", async token =>
{
singleRuns++;
await Task.Delay(500);
});
// Schedule anonymous single
scheduler.ScheduleTask(null, async token =>
{
await Task.Delay(100);
});
Assert.Equal(4, scheduler.Count);
await TestUtils.WaitForCondition(() => scheduler.Count == 2, 1);
Assert.Equal(2, scheduler.Count);
// Schedule interally looping task
bool shouldLoop = true;
scheduler.ScheduleTask("intLoop", async token =>
{
while (shouldLoop)
{
await Task.Delay(100);
}
});
Assert.Throws<InvalidOperationException>(() => scheduler.ScheduleTask("intLoop", token => Task.CompletedTask));
await Task.Delay(500);
// Wait for single to terminate
await RunWithTimeout(scheduler.WaitForTermination("single"), 5000);
Assert.Equal(1, singleRuns);
// Wait for internally looping to terminate
var intTask = scheduler.WaitForTermination("intLoop");
shouldLoop = false;
await RunWithTimeout(intTask, 1000);
// pause periodic
await Task.Delay(500);
Assert.True(periodicRuns > 1);
scheduler.TryPauseTask("periodic", true);
int numRuns = periodicRuns;
await Task.Delay(500);
// It might run once more, if it was already scheduled to run
Assert.True(periodicRuns <= numRuns + 1);
// Test waiting to run
int infRuns = 0;
scheduler.SchedulePeriodicTask("infinitePeriodic", Timeout.InfiniteTimeSpan, token =>
{
infRuns++;
}, false);
await Task.Delay(400);
Assert.Equal(0, infRuns);
scheduler.TryTriggerTask("infinitePeriodic");
await Task.Delay(400);
Assert.Equal(1, infRuns);
Assert.True(scheduler.ContainsTask("periodic"));
Assert.Equal(3, scheduler.Count);
await RunWithTimeout(scheduler.ExitAndWaitForTermination("periodic"), 1000);
scheduler.SchedulePeriodicTask(null, TimeSpan.FromSeconds(1), async token =>
{
await Task.Delay(100);
});
scheduler.ScheduleTask("failing", async token =>
{
await Task.Delay(100);
throw new CogniteUtilsException();
});
var ex = await Assert.ThrowsAsync<AggregateException>(async () => await loopTask);
Assert.IsType<CogniteUtilsException>(ex.InnerException);
source.Cancel();
}
}
}