Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Ported BufferReaderBench to BenchmarkDotNet
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina committed May 2, 2018
1 parent d48f2c0 commit ad7adff
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static bool TryParse(ref BufferReader reader, out bool value)
}

Span<byte> tempSpan = stackalloc byte[5];
var copied = BufferReaderExtensions.Peek(reader, tempSpan);
var copied = Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Advance(consumed);
Expand All @@ -43,7 +43,7 @@ public static bool TryParse(ref BufferReader reader, out int value)
}

Span<byte> tempSpan = stackalloc byte[15];
var copied = BufferReaderExtensions.Peek(reader, tempSpan);
var copied = Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Advance(consumed);
Expand All @@ -66,7 +66,7 @@ public static bool TryParse(ref BufferReader reader, out ulong value)
}

Span<byte> tempSpan = stackalloc byte[30];
var copied = BufferReaderExtensions.Peek(reader, tempSpan);
var copied = Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Advance(consumed);
Expand Down
11 changes: 0 additions & 11 deletions tests/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<AssemblyOriginatorKeyFile>../../tools/test_key.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<Description></Description>
<Copyright>Microsoft Corporation, All rights reserved</Copyright>
<PackageTags></PackageTags>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIconUrl></PackageIconUrl>
<PackageProjectUrl></PackageProjectUrl>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>
<ItemGroup>
Expand Down
65 changes: 65 additions & 0 deletions tests/Benchmarks/Buffers/Reader.cs
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);
}
}
}

79 changes: 0 additions & 79 deletions tests/Benchmarks/BytesReaderBench.cs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Program
/// <param name="args"></param>
public static void Main(string[] args)
=> BenchmarkSwitcher
.FromAssembly(typeof(Program).Assembly)
.FromAssembly(typeof(Program).Assembly)
.Run(args/*, CreateClrVsCoreConfig() uncomment it to run Clr vs .NET Core comparison*/);

private static IConfig CreateClrVsCoreConfig()
Expand Down
3 changes: 0 additions & 3 deletions tests/Benchmarks/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyProduct("Benchmarks.Tests")]
[assembly: AssemblyTrademark("")]

// Setting ComVisible to false makes the types in this assembly not visible
Expand Down
40 changes: 40 additions & 0 deletions tests/System.Buffers.ReaderWriter.Tests/BufferReaderTests.cs
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);
}
}
}
}

0 comments on commit ad7adff

Please sign in to comment.