Skip to content

Commit 5c05fbb

Browse files
authored
[Internal] Benchmark: Refactors code to make Memory Stream capacity configurable (#3624)
Co-authored-by: Sourabh Jain <[email protected]>
1 parent 814e72e commit 5c05fbb

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/BenchmarkConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ internal void Print()
143143
Utility.TeeTraceInformation($"{nameof(BenchmarkConfig)} arguments");
144144
Utility.TeeTraceInformation($"IsServerGC: {GCSettings.IsServerGC}");
145145
Utility.TeeTraceInformation("--------------------------------------------------------------------- ");
146-
Utility.TeeTraceInformation(JsonHelper.ToString(this));
146+
Utility.TeeTraceInformation(JsonHelper.ToString(
147+
input: this,
148+
capacity: 2048));
147149
Utility.TeeTraceInformation("--------------------------------------------------------------------- ");
148150
Utility.TeeTraceInformation(string.Empty);
149151
}

Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/JsonHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ internal static class JsonHelper
1717
});
1818
private const int DefaultCapacity = 1024;
1919

20-
public static string ToString<T>(T input)
20+
public static string ToString<T>(T input, int capacity = JsonHelper.DefaultCapacity)
2121
{
22-
using (MemoryStream stream = JsonHelper.ToStream(input))
22+
using (MemoryStream stream = JsonHelper.ToStream(input, capacity))
2323
using (StreamReader sr = new StreamReader(stream))
2424
{
2525
return sr.ReadToEnd();
@@ -31,15 +31,15 @@ public static T Deserialize<T>(string payload)
3131
return JsonConvert.DeserializeObject<T>(payload);
3232
}
3333

34-
public static MemoryStream ToStream<T>(T input)
34+
public static MemoryStream ToStream<T>(T input, int capacity = JsonHelper.DefaultCapacity)
3535
{
36-
byte[] blob = System.Buffers.ArrayPool<byte>.Shared.Rent(JsonHelper.DefaultCapacity);
37-
MemoryStream memStreamPayload = new MemoryStream(blob, 0, JsonHelper.DefaultCapacity, writable: true, publiclyVisible: true);
36+
byte[] blob = System.Buffers.ArrayPool<byte>.Shared.Rent(capacity);
37+
MemoryStream memStreamPayload = new MemoryStream(blob, 0, capacity, writable: true, publiclyVisible: true);
3838
memStreamPayload.SetLength(0);
3939
memStreamPayload.Position = 0;
4040
using (StreamWriter streamWriter = new StreamWriter(memStreamPayload,
4141
encoding: JsonHelper.DefaultEncoding,
42-
bufferSize: JsonHelper.DefaultCapacity,
42+
bufferSize: capacity,
4343
leaveOpen: true))
4444
{
4545
using (JsonWriter writer = new JsonTextWriter(streamWriter))

0 commit comments

Comments
 (0)