This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ported BufferReaderBench to BenchmarkDotNet
- Loading branch information
1 parent
d48f2c0
commit ad7adff
Showing
7 changed files
with
109 additions
and
97 deletions.
There are no files selected for viewing
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
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
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,65 @@ | ||
// 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 BenchmarkDotNet.Attributes; | ||
using System; | ||
using System.Buffers; | ||
using System.Buffers.Reader; | ||
using System.Buffers.Text; | ||
using System.Text; | ||
|
||
public class Reader | ||
{ | ||
static byte[] s_array; | ||
static ReadOnlySequence<byte> s_ros; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
var sections = 100000; | ||
var section = "1234 "; | ||
var builder = new StringBuilder(sections * section.Length); | ||
for (int i = 0; i < sections; i++) | ||
{ | ||
builder.Append(section); | ||
} | ||
s_array = Encoding.UTF8.GetBytes(builder.ToString()); | ||
s_ros = new ReadOnlySequence<byte>(s_array); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void ParseInt32Utf8Parser() | ||
{ | ||
var span = new ReadOnlySpan<byte>(s_array); | ||
|
||
int totalConsumed = 0; | ||
while (Utf8Parser.TryParse(span.Slice(totalConsumed), out int value, out int consumed)) | ||
{ | ||
totalConsumed += consumed + 1; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void ParseInt32BufferReader() | ||
{ | ||
var reader = BufferReader.Create(s_ros); | ||
|
||
while (BufferReaderExtensions.TryParse(ref reader, out int value)) | ||
{ | ||
reader.Advance(1); // advance past the delimiter | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void ParseInt32BufferReaderRaw() | ||
{ | ||
var reader = BufferReader.Create(s_ros); | ||
|
||
while (Utf8Parser.TryParse(reader.CurrentSegment.Slice(reader.ConsumedBytes), out int value, out int consumed)) | ||
{ | ||
reader.Advance(consumed + 1); | ||
} | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
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
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
40 changes: 40 additions & 0 deletions
40
tests/System.Buffers.ReaderWriter.Tests/BufferReaderTests.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,40 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Buffers.Reader; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace System.Buffers.Tests | ||
{ | ||
public class BufferReaderTests | ||
{ | ||
static byte[] s_array; | ||
static ReadOnlySequence<byte> s_ros; | ||
|
||
static BufferReaderTests() | ||
{ | ||
var sections = 100000; | ||
var section = "1234 "; | ||
var builder = new StringBuilder(sections * section.Length); | ||
for (int i = 0; i < sections; i++) | ||
{ | ||
builder.Append(section); | ||
} | ||
s_array = Encoding.UTF8.GetBytes(builder.ToString()); | ||
s_ros = new ReadOnlySequence<byte>(s_array); | ||
} | ||
|
||
[Fact] | ||
public void TryParseRos() | ||
{ | ||
var reader = BufferReader.Create(s_ros); | ||
|
||
while (BufferReaderExtensions.TryParse(ref reader, out int value)) | ||
{ | ||
reader.Advance(1); // advance past the delimiter | ||
Assert.Equal(1234, value); | ||
} | ||
} | ||
} | ||
} |