Skip to content

Commit d64f22a

Browse files
Trapovdanmoseleyadamsitnik
authored
Add String/StreamReader.ReadToEnd benchmarks (dotnet#2177)
Co-authored-by: Dan Moseley <[email protected]> Co-authored-by: Adam Sitnik <[email protected]>
1 parent c8f95e1 commit d64f22a

File tree

6 files changed

+84
-3
lines changed

6 files changed

+84
-3
lines changed

docs/microbenchmark-design-guidelines.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ reportedResult = 1/16*SpanCtor + 1*Slice
242242

243243
**Note:** `OperationsPerInvoke` should be big enough to amortize the "setup" cost.
244244

245+
**Tip:** To quickly run your new benchmark just enough to see how long the iterations are, you can add `--iterationCount 1 --warmupCount 0 --invocationCount 1 --unrollFactor 1 --strategy ColdStart` to the end of your command line. It won't give accurate results, but can help you determine whether you need to add more operations to bring the iteration time up to the 100ms goal.
246+
245247
## Test Cases
246248

247249
### Code Paths

src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void SetupConvertTryFromBase64Chars()
9090
[WarmupCount(30)] // make sure it's promoted to Tier 1
9191
public class Base64EncodeDecodeInPlaceTests
9292
{
93-
[Params(1000 * 1000 * 200)] // allows for stable iteraiton around 200ms
93+
[Params(1000 * 1000 * 200)] // allows for stable iteration around 200ms
9494
public int NumberOfBytes { get; set; }
9595

9696
private byte[] _source;

src/benchmarks/micro/libraries/System.Collections/Iterate/IterateFor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public T List()
7777
var collection = _list;
7878
for(int i = 0; i < collection.Count; i++)
7979
result = collection[i];
80-
return result;;
80+
return result;
8181
}
8282

8383
[GlobalSetup(Target = nameof(IList))]

src/benchmarks/micro/libraries/System.IO/StreamReaderReadLineTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public void ReadLine()
2828
{
2929
while (reader.ReadLine() != null) ;
3030
}
31-
3231
}
3332

3433
[Benchmark]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using BenchmarkDotNet.Attributes;
8+
using MicroBenchmarks;
9+
10+
namespace System.IO.Tests
11+
{
12+
[BenchmarkCategory(Categories.Libraries)]
13+
public class StreamReaderReadToEndTests : TextReaderReadLineTests
14+
{
15+
private StreamReader _reader;
16+
17+
[GlobalSetup]
18+
public void GlobalSetup()
19+
{
20+
_text = GenerateLinesText(LineLengthRange, 48 * 1024 * 1024);
21+
_reader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(_text)));
22+
}
23+
24+
[GlobalCleanup]
25+
public void GlobalCleanup()
26+
{
27+
_reader?.Dispose();
28+
}
29+
30+
[Benchmark]
31+
public string ReadToEnd()
32+
{
33+
_reader.BaseStream.Position = 0;
34+
_reader.DiscardBufferedData();
35+
return _reader.ReadToEnd();
36+
}
37+
38+
[Benchmark]
39+
[BenchmarkCategory(Categories.NoWASM)]
40+
public Task<string> ReadToEndAsync()
41+
{
42+
_reader.BaseStream.Position = 0;
43+
_reader.DiscardBufferedData();
44+
return _reader.ReadToEndAsync();
45+
}
46+
}
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Threading.Tasks;
6+
using BenchmarkDotNet.Attributes;
7+
using MicroBenchmarks;
8+
9+
namespace System.IO.Tests
10+
{
11+
[BenchmarkCategory(Categories.Libraries)]
12+
public class StringReaderReadToEndTests : TextReaderReadLineTests
13+
{
14+
[GlobalSetup]
15+
public void GlobalSetup() => _text = GenerateLinesText(LineLengthRange, 48 * 1024 * 1024);
16+
17+
[Benchmark]
18+
public string ReadToEnd()
19+
{
20+
// StringReaders cannot be reset, so we are forced to include the constructor in the benchmark
21+
using StringReader reader = new StringReader(_text);
22+
return reader.ReadToEnd();
23+
}
24+
25+
[BenchmarkCategory(Categories.NoWASM)]
26+
[Benchmark]
27+
public Task<string> ReadToEndAsync()
28+
{
29+
using StringReader reader = new StringReader(_text);
30+
return reader.ReadToEndAsync();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)