-
Notifications
You must be signed in to change notification settings - Fork 296
Add MemoryStream benchmarks #5140
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
src/benchmarks/micro/libraries/System.IO/MemoryStreamChunkedTests.cs
This file contains hidden or 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,150 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using BenchmarkDotNet.Attributes; | ||
| using MicroBenchmarks; | ||
|
|
||
| namespace System.IO.Tests | ||
| { | ||
| /// <summary> | ||
| /// Chunked / buffered operations. Fixed 64KB stream, chunk size controls call count. | ||
| /// Read/Write variants loop over the stream in ChunkSize increments. | ||
| /// CopyTo / CopyToAsync pass ChunkSize as the bufferSize parameter. | ||
| /// </summary> | ||
| [BenchmarkCategory(Categories.Libraries)] | ||
| public class MemoryStreamChunkedTests | ||
| { | ||
| private const int StreamSize = 65536; | ||
|
|
||
| private MemoryStream _stream; | ||
| private byte[] _buffer; | ||
|
|
||
| [Params( | ||
| 1, // 65536 calls, per-call overhead dominates | ||
| 4096)] // 16 calls, default StreamReader/BufferedStream buffer size | ||
| public int ChunkSize { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| byte[] backing = new byte[StreamSize]; | ||
| new Random(42).NextBytes(backing); | ||
| _buffer = new byte[ChunkSize]; | ||
| _stream = new MemoryStream(backing, writable: true); | ||
| } | ||
|
|
||
| [GlobalCleanup] | ||
| public void Cleanup() | ||
| { | ||
| _stream.Dispose(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [MemoryRandomization] | ||
| public int ReadByteArray() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| int count = 0; | ||
| int n; | ||
| while ((n = s.Read(_buffer, 0, _buffer.Length)) > 0) | ||
| count += n; | ||
| return count; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [MemoryRandomization] | ||
| public int ReadSpan() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| int count = 0; | ||
| int n; | ||
| while ((n = s.Read(_buffer.AsSpan())) > 0) | ||
| count += n; | ||
| return count; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [BenchmarkCategory(Categories.NoWASM)] | ||
| [MemoryRandomization] | ||
| public async Task<int> ReadAsyncMemory() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| int count = 0; | ||
| int n; | ||
| while ((n = await s.ReadAsync(_buffer, CancellationToken.None)) > 0) | ||
| count += n; | ||
| return count; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [MemoryRandomization] | ||
| public void WriteByteArray() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| int remaining = StreamSize; | ||
| while (remaining > 0) | ||
| { | ||
| int chunk = Math.Min(_buffer.Length, remaining); | ||
| s.Write(_buffer, 0, chunk); | ||
| remaining -= chunk; | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [MemoryRandomization] | ||
| public void WriteSpan() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| int remaining = StreamSize; | ||
| while (remaining > 0) | ||
| { | ||
| int chunk = Math.Min(_buffer.Length, remaining); | ||
| s.Write(_buffer.AsSpan(0, chunk)); | ||
| remaining -= chunk; | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [BenchmarkCategory(Categories.NoWASM)] | ||
| [MemoryRandomization] | ||
| public async Task WriteAsyncMemory() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| int remaining = StreamSize; | ||
| while (remaining > 0) | ||
| { | ||
| int chunk = Math.Min(_buffer.Length, remaining); | ||
| await s.WriteAsync(_buffer.AsMemory(0, chunk), CancellationToken.None); | ||
| remaining -= chunk; | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [MemoryRandomization] | ||
| public void CopyToWithBufferSize() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| s.CopyTo(Stream.Null, ChunkSize); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [BenchmarkCategory(Categories.NoWASM)] | ||
| [MemoryRandomization] | ||
| public async Task CopyToAsyncWithBufferSize() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| await s.CopyToAsync(Stream.Null, ChunkSize); | ||
| } | ||
| } | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
src/benchmarks/micro/libraries/System.IO/MemoryStreamTests.cs
This file contains hidden or 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,81 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Threading.Tasks; | ||
| using BenchmarkDotNet.Attributes; | ||
| using MicroBenchmarks; | ||
|
|
||
| namespace System.IO.Tests | ||
| { | ||
| /// <summary> | ||
| /// Operations that do not receive a user buffer. Backing size controls iteration count. | ||
| /// ReadByte / WriteByte exercise per-byte overhead; CopyTo / CopyToAsync use the | ||
| /// default buffer size overload. | ||
| /// </summary> | ||
| [BenchmarkCategory(Categories.Libraries)] | ||
| public class MemoryStreamTests | ||
| { | ||
| private MemoryStream _stream; | ||
|
|
||
| [Params( | ||
| 1024, // small stream | ||
| 65536)] // large stream | ||
| public int Size { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| byte[] backing = new byte[Size]; | ||
| new Random(42).NextBytes(backing); | ||
| _stream = new MemoryStream(backing, writable: true); | ||
| } | ||
|
|
||
| [GlobalCleanup] | ||
| public void Cleanup() | ||
| { | ||
| _stream.Dispose(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [MemoryRandomization] | ||
| public int ReadByte() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| int count = 0; | ||
| while (s.ReadByte() != -1) | ||
| count++; | ||
| return count; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [MemoryRandomization] | ||
| public void WriteByte() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| for (int i = 0; i < Size; i++) | ||
| s.WriteByte((byte)i); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [MemoryRandomization] | ||
| public void CopyTo() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| s.CopyTo(Stream.Null); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| [BenchmarkCategory(Categories.NoWASM)] | ||
| [MemoryRandomization] | ||
| public async Task CopyToAsync() | ||
| { | ||
| MemoryStream s = _stream; | ||
| s.Position = 0; | ||
| await s.CopyToAsync(Stream.Null); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.