diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_text.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_text.cs index e788d9c2c11..afdbd211f50 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_text.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_text.cs @@ -20,7 +20,7 @@ public static bool TryParse(ref BufferReader reader, out bool value) } Span 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); @@ -43,7 +43,7 @@ public static bool TryParse(ref BufferReader reader, out int value) } Span 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); @@ -66,7 +66,7 @@ public static bool TryParse(ref BufferReader reader, out ulong value) } Span 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); diff --git a/tests/Benchmarks/Benchmarks.csproj b/tests/Benchmarks/Benchmarks.csproj index 2c7e84ad1ad..1fd913c7291 100644 --- a/tests/Benchmarks/Benchmarks.csproj +++ b/tests/Benchmarks/Benchmarks.csproj @@ -3,21 +3,10 @@ netcoreapp2.1 Exe - False ../../tools/test_key.snk true true - Microsoft Corporation, All rights reserved - - - - - - False - false - false - false $(NoWarn);CS0618 diff --git a/tests/Benchmarks/Buffers/Reader.cs b/tests/Benchmarks/Buffers/Reader.cs new file mode 100644 index 00000000000..ae43b1070ae --- /dev/null +++ b/tests/Benchmarks/Buffers/Reader.cs @@ -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 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(s_array); + } + + [Benchmark(Baseline = true)] + public void ParseInt32Utf8Parser() + { + var span = new ReadOnlySpan(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); + } + } +} + diff --git a/tests/Benchmarks/BytesReaderBench.cs b/tests/Benchmarks/BytesReaderBench.cs deleted file mode 100644 index 980c0887531..00000000000 --- a/tests/Benchmarks/BytesReaderBench.cs +++ /dev/null @@ -1,79 +0,0 @@ -// 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 Microsoft.Xunit.Performance; -using System; -using System.Buffers; -using System.Buffers.Reader; -using System.Buffers.Text; -using System.Text; - -public class BytesReaderBench -{ - static byte[] s_data; - static ReadOnlySequence s_buffer; - - static BytesReaderBench() - { - int sections = 100000; - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < sections; i++) - { - sb.Append("1234 "); - } - s_data = Encoding.UTF8.GetBytes(sb.ToString()); - s_buffer = new ReadOnlySequence(s_data); - } - - [Benchmark] - static void ParseInt32BufferReader() - { - foreach (var iteration in Benchmark.Iterations) { - var reader = BufferReader.Create(s_buffer); - - using (iteration.StartMeasurement()) { - - while(BufferReaderExtensions.TryParse(ref reader, out int value)) { - reader.Advance(1); - } - } - } - } - - [Benchmark] - static void ParseInt32Utf8Parser() - { - var data = new ReadOnlySpan(s_data); - - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - int totalConsumed = 0; - while(Utf8Parser.TryParse(data.Slice(totalConsumed), out int value, out int consumed)) - { - totalConsumed += consumed + 1; - } - } - } - } - - [Benchmark] - static void ParseInt32BufferReaderRaw() - { - foreach (var iteration in Benchmark.Iterations) - { - var buffer = new ReadOnlySequence(s_data); - var reader = BufferReader.Create(buffer); - - using (iteration.StartMeasurement()) - { - while(Utf8Parser.TryParse(reader.CurrentSegment.Slice(reader.ConsumedBytes), out int value, out int consumed)){ - reader.Advance(consumed + 1); - } - } - } - } -} - diff --git a/tests/Benchmarks/Program.cs b/tests/Benchmarks/Program.cs index 3ad28a19c1e..6bb81146548 100644 --- a/tests/Benchmarks/Program.cs +++ b/tests/Benchmarks/Program.cs @@ -17,7 +17,7 @@ public class Program /// 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() diff --git a/tests/Benchmarks/Properties/AssemblyInfo.cs b/tests/Benchmarks/Properties/AssemblyInfo.cs index 6a7e6730a1d..ef55d690025 100644 --- a/tests/Benchmarks/Properties/AssemblyInfo.cs +++ b/tests/Benchmarks/Properties/AssemblyInfo.cs @@ -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 diff --git a/tests/System.Buffers.ReaderWriter.Tests/BufferReaderTests.cs b/tests/System.Buffers.ReaderWriter.Tests/BufferReaderTests.cs new file mode 100644 index 00000000000..5a2cad2ee6e --- /dev/null +++ b/tests/System.Buffers.ReaderWriter.Tests/BufferReaderTests.cs @@ -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 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(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); + } + } + } +}