Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Benchmark: Refactors code to make Memory Stream capacity configurable. #3624

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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