Skip to content

Add microbenchmark for core UnmanagedMemoryStream scenario #3435

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 3 commits into from
Jan 30, 2024
Merged
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
@@ -0,0 +1,53 @@
// 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.IO;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;

namespace System.IO.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public unsafe class UnmanagedMemoryStreamTests
{
private const int Length = 10000;
private byte* _buffer;

[GlobalSetup]
public void Setup()
{
_buffer = (byte*)Marshal.AllocCoTaskMem(Length);
}

[GlobalCleanup]
public void Cleanup()
{
Marshal.FreeCoTaskMem((IntPtr)_buffer);
}

[Benchmark]
public void ReadAsBytes()
{
using (var ums = new UnmanagedMemoryStream(_buffer, Length))
{
while (ums.ReadByte() >= 0)
{
}
}
}

[Benchmark]
public void ReadAsArrays()
{
var array = new byte[128];
using (var ums = new UnmanagedMemoryStream(_buffer, Length))
{
while (ums.Read(array, 0, array.Length) != 0)
{
}
}
}
}
}