-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Batches size now affects sending size accurately. Use extensions to h…
…elp test this
- Loading branch information
1 parent
e5a0915
commit b72d91c
Showing
3 changed files
with
103 additions
and
12 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/Speckle.Sdk.Dependencies/Serialization/BatchExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Buffers; | ||
|
||
namespace Speckle.Sdk.Serialisation.V2.Send; | ||
|
||
public static class BatchExtensions | ||
{ | ||
public static void TrimBatch<T>(ref IMemoryOwner<T> batch, bool isVerifiedFull) | ||
where T : IHasSize | ||
{ | ||
if (!isVerifiedFull) | ||
{ | ||
((Batch<T>)batch).TrimExcess(); | ||
} | ||
} | ||
|
||
public static void AddBatchItem<T>(this IMemoryOwner<T> batch, T item) | ||
where T : IHasSize => ((Batch<T>)batch).Add(item); | ||
|
||
public static int GetBatchSize<T>(this IMemoryOwner<T> batch, int maxBatchSize) where T : IHasSize | ||
{ | ||
var currentSize = ((Batch<T>)batch).Size; | ||
if (currentSize > maxBatchSize) | ||
{ | ||
return maxBatchSize; | ||
} | ||
|
||
return currentSize; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using FluentAssertions; | ||
using Speckle.Sdk.Serialisation.V2.Send; | ||
|
||
namespace Speckle.Sdk.Serialization.Tests; | ||
|
||
public class BatchTests | ||
{ | ||
|
||
/*** from BatchingChannelReader.cs ** | ||
bool full = GetBatchSize(c) == _batchSize; | ||
while (!full && source.TryRead(out item)) | ||
{ | ||
AddBatchItem(c, item); | ||
full = GetBatchSize(c) == _batchSize; | ||
}*/ | ||
|
||
public class TestBatchItem : IHasSize | ||
{ | ||
public int Size { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void Basics() | ||
{ | ||
using var batch = new Batch<TestBatchItem>(); | ||
batch.Add(new TestBatchItem { Size = 2 }); | ||
batch.Size.Should().Be(2); | ||
batch.Add(new TestBatchItem { Size = 2 }); | ||
batch.Size.Should().Be(4); | ||
batch.Add(new TestBatchItem { Size = 2 }); | ||
batch.Size.Should().Be(6); | ||
|
||
batch.TrimExcess(); | ||
batch.Size.Should().Be(6); | ||
batch.Items.Count.Should().Be(3); | ||
} | ||
|
||
[Fact] | ||
public void Large_Message_Problem_1() | ||
{ | ||
const int MAX_BATCH_SIZE = 5; | ||
|
||
|
||
using var batch = new Batch<TestBatchItem>(); | ||
batch.AddBatchItem(new TestBatchItem { Size = 2 }); | ||
bool full = batch.GetBatchSize(MAX_BATCH_SIZE) == MAX_BATCH_SIZE; | ||
full.Should().BeFalse(); | ||
batch.AddBatchItem(new TestBatchItem { Size = 2 }); | ||
full = batch.GetBatchSize(MAX_BATCH_SIZE) == MAX_BATCH_SIZE; | ||
full.Should().BeFalse(); | ||
batch.AddBatchItem(new TestBatchItem { Size = 2 }); | ||
full = batch.GetBatchSize(MAX_BATCH_SIZE) == MAX_BATCH_SIZE; | ||
full.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void Large_Message_Problem_2() | ||
{ | ||
const int MAX_BATCH_SIZE = 5; | ||
using var batch = new Batch<TestBatchItem>(); | ||
batch.AddBatchItem(new TestBatchItem { Size = 6 }); | ||
bool full = batch.GetBatchSize(MAX_BATCH_SIZE) == MAX_BATCH_SIZE; | ||
full.Should().BeTrue(); | ||
} | ||
} |