Skip to content

Commit

Permalink
[Internal] Benchmark: Refactors code to make Memory Stream capacity c…
Browse files Browse the repository at this point in the history
…onfigurable (#3624)

Co-authored-by: Sourabh Jain <sourabhjain@microsoft.com>
  • Loading branch information
sourabh1007 and sourabh1007 committed Jan 4, 2023
1 parent 814e72e commit 5c05fbb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ internal void Print()
Utility.TeeTraceInformation($"{nameof(BenchmarkConfig)} arguments");
Utility.TeeTraceInformation($"IsServerGC: {GCSettings.IsServerGC}");
Utility.TeeTraceInformation("--------------------------------------------------------------------- ");
Utility.TeeTraceInformation(JsonHelper.ToString(this));
Utility.TeeTraceInformation(JsonHelper.ToString(
input: this,
capacity: 2048));
Utility.TeeTraceInformation("--------------------------------------------------------------------- ");
Utility.TeeTraceInformation(string.Empty);
}
Expand Down
12 changes: 6 additions & 6 deletions Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ internal static class JsonHelper
});
private const int DefaultCapacity = 1024;

public static string ToString<T>(T input)
public static string ToString<T>(T input, int capacity = JsonHelper.DefaultCapacity)
{
using (MemoryStream stream = JsonHelper.ToStream(input))
using (MemoryStream stream = JsonHelper.ToStream(input, capacity))
using (StreamReader sr = new StreamReader(stream))
{
return sr.ReadToEnd();
Expand All @@ -31,15 +31,15 @@ public static T Deserialize<T>(string payload)
return JsonConvert.DeserializeObject<T>(payload);
}

public static MemoryStream ToStream<T>(T input)
public static MemoryStream ToStream<T>(T input, int capacity = JsonHelper.DefaultCapacity)
{
byte[] blob = System.Buffers.ArrayPool<byte>.Shared.Rent(JsonHelper.DefaultCapacity);
MemoryStream memStreamPayload = new MemoryStream(blob, 0, JsonHelper.DefaultCapacity, writable: true, publiclyVisible: true);
byte[] blob = System.Buffers.ArrayPool<byte>.Shared.Rent(capacity);
MemoryStream memStreamPayload = new MemoryStream(blob, 0, capacity, writable: true, publiclyVisible: true);
memStreamPayload.SetLength(0);
memStreamPayload.Position = 0;
using (StreamWriter streamWriter = new StreamWriter(memStreamPayload,
encoding: JsonHelper.DefaultEncoding,
bufferSize: JsonHelper.DefaultCapacity,
bufferSize: capacity,
leaveOpen: true))
{
using (JsonWriter writer = new JsonTextWriter(streamWriter))
Expand Down

0 comments on commit 5c05fbb

Please sign in to comment.