Skip to content

Commit e3d3da3

Browse files
authored
HttpRequestStreamReader overrides for Read Span, ReadAsync Memory, ReadLine and ReadLineAsync (#18802)
1 parent ce85f28 commit e3d3da3

File tree

5 files changed

+458
-59
lines changed

5 files changed

+458
-59
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Diagnostics;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using BenchmarkDotNet.Attributes;
6+
7+
namespace Microsoft.AspNetCore.WebUtilities
8+
{
9+
public class HttpRequestStreamReaderReadLineBenchmark
10+
{
11+
private MemoryStream _stream;
12+
13+
[Params(200, 1000, 1025, 1600)] // Default buffer length is 1024
14+
public int Length { get; set; }
15+
16+
[GlobalSetup]
17+
public void GlobalSetup()
18+
{
19+
var data = new char[Length];
20+
21+
data[Length - 2] = '\r';
22+
data[Length - 1] = '\n';
23+
24+
_stream = new MemoryStream(Encoding.UTF8.GetBytes(data));
25+
}
26+
27+
[Benchmark]
28+
public async Task<string> ReadLineAsync()
29+
{
30+
var reader = CreateReader();
31+
var result = await reader.ReadLineAsync();
32+
Debug.Assert(result.Length == Length - 2);
33+
return result;
34+
}
35+
36+
[Benchmark]
37+
public string ReadLine()
38+
{
39+
var reader = CreateReader();
40+
var result = reader.ReadLine();
41+
Debug.Assert(result.Length == Length - 2);
42+
return result;
43+
}
44+
45+
[Benchmark]
46+
public HttpRequestStreamReader CreateReader()
47+
{
48+
_stream.Seek(0, SeekOrigin.Begin);
49+
return new HttpRequestStreamReader(_stream, Encoding.UTF8);
50+
}
51+
}
52+
}

src/Http/WebUtilities/perf/Microsoft.AspNetCore.WebUtilities.Performance/Microsoft.AspNetCore.WebUtilities.Performance.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>

src/Http/WebUtilities/ref/Microsoft.AspNetCore.WebUtilities.netcoreapp.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,13 @@ protected override void Dispose(bool disposing) { }
145145
public override int Peek() { throw null; }
146146
public override int Read() { throw null; }
147147
public override int Read(char[] buffer, int index, int count) { throw null; }
148-
[System.Diagnostics.DebuggerStepThroughAttribute]
148+
public override int Read(System.Span<char> buffer) { throw null; }
149149
public override System.Threading.Tasks.Task<int> ReadAsync(char[] buffer, int index, int count) { throw null; }
150+
[System.Diagnostics.DebuggerStepThroughAttribute]
151+
public override System.Threading.Tasks.ValueTask<int> ReadAsync(System.Memory<char> buffer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
152+
public override string ReadLine() { throw null; }
153+
[System.Diagnostics.DebuggerStepThroughAttribute]
154+
public override System.Threading.Tasks.Task<string> ReadLineAsync() { throw null; }
150155
}
151156
public partial class HttpResponseStreamWriter : System.IO.TextWriter
152157
{

0 commit comments

Comments
 (0)