forked from dotnet/performance
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add microbenchmark for core UnmanagedMemoryStream scenario
Related to dotnet/runtime#93766
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/benchmarks/micro/libraries/System.IO/UnmanagedMemoryStreamTests.cs
This file contains 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,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[1]; | ||
using (var ums = new UnmanagedMemoryStream(buffer, Length)) | ||
{ | ||
while (ums.Read(array, 0, array.Length) != 0) | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
} |