Skip to content

Commit 529f33c

Browse files
authored
Add StreamReaderReadLineTests and base TextReaderReadLineTests (#2170)
* Add StreamReaderReadLineTests and base TextReaderReadLineTests * change to create memorystream on each invocation instead
1 parent a82415e commit 529f33c

File tree

3 files changed

+111
-56
lines changed

3 files changed

+111
-56
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 StreamReaderReadLineTests : TextReaderReadLineTests
14+
{
15+
private byte[] _bytes;
16+
17+
[GlobalSetup]
18+
public void GlobalSetup()
19+
{
20+
_text = GenerateLinesText(LineLengthRange, 16 * 1024);
21+
_bytes = Encoding.UTF8.GetBytes(_text);
22+
}
23+
24+
[Benchmark]
25+
public void ReadLine()
26+
{
27+
using StreamReader reader = new (new MemoryStream(_bytes));
28+
while (reader.ReadLine() != null) ;
29+
}
30+
31+
[Benchmark]
32+
[BenchmarkCategory(Categories.NoWASM)]
33+
public async Task ReadLineAsync()
34+
{
35+
using StreamReader reader = new(new MemoryStream(_bytes));
36+
while (await reader.ReadLineAsync() != null) ;
37+
}
38+
}
39+
}

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

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,8 @@
1212
namespace System.IO.Tests
1313
{
1414
[BenchmarkCategory(Categories.Libraries)]
15-
public class StringReaderReadLineTests
15+
public class StringReaderReadLineTests : TextReaderReadLineTests
1616
{
17-
private string _text;
18-
19-
[ParamsSource(nameof(GetLineLengthRanges))]
20-
public Range LineLengthRange { get; set; }
21-
22-
public static IEnumerable<Range> GetLineLengthRanges()
23-
{
24-
yield return new() { Min = 0, Max = 0 };
25-
yield return new() { Min = 1, Max = 1 };
26-
yield return new() { Min = 1, Max = 8 };
27-
yield return new() { Min = 9, Max = 32 };
28-
yield return new() { Min = 33, Max = 128 };
29-
yield return new() { Min = 129, Max = 1024 };
30-
yield return new() { Min = 0, Max = 1024 };
31-
}
32-
33-
public class Range
34-
{
35-
public int Min { get; set; }
36-
public int Max { get; set; }
37-
38-
public override string ToString() => $"[{Min,4}, {Max,4}]";
39-
}
40-
4117
[GlobalSetup]
4218
public void GlobalSetup() => _text = GenerateLinesText(LineLengthRange, 16 * 1024);
4319

@@ -55,36 +31,5 @@ public async Task ReadLineAsync()
5531
using StringReader reader = new(_text);
5632
while (await reader.ReadLineAsync() != null) ;
5733
}
58-
59-
private static string GenerateLinesText(Range lineLengthRange, int textTargetLength)
60-
{
61-
var min = lineLengthRange.Min;
62-
var max = lineLengthRange.Max;
63-
64-
var newLine = Environment.NewLine;
65-
66-
var capacity = textTargetLength + max + newLine.Length;
67-
var sb = new StringBuilder(capacity);
68-
69-
var random = new Random(42);
70-
int lineCount = 0;
71-
while(sb.Length < textTargetLength)
72-
{
73-
var charsCount = random.Next(min, max);
74-
for (int c = 0; c < charsCount; c++)
75-
{
76-
var ch = (char)random.Next('0', 'z');
77-
sb.Append(ch);
78-
}
79-
sb.Append(newLine);
80-
++lineCount;
81-
}
82-
var text = sb.ToString();
83-
84-
Console.WriteLine($"// Generated lines {lineCount} and " +
85-
$"text length {text.Length} out of capacity {capacity}");
86-
87-
return text;
88-
}
8934
}
9035
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.Collections.Generic;
6+
using System.Text;
7+
using BenchmarkDotNet.Attributes;
8+
using MicroBenchmarks;
9+
10+
namespace System.IO.Tests
11+
{
12+
[BenchmarkCategory(Categories.Libraries)]
13+
public abstract class TextReaderReadLineTests
14+
{
15+
protected string _text;
16+
17+
[ParamsSource(nameof(GetLineLengthRanges))]
18+
public Range LineLengthRange { get; set; }
19+
20+
public static IEnumerable<Range> GetLineLengthRanges()
21+
{
22+
yield return new() { Min = 0, Max = 0 };
23+
yield return new() { Min = 1, Max = 1 };
24+
yield return new() { Min = 1, Max = 8 };
25+
yield return new() { Min = 9, Max = 32 };
26+
yield return new() { Min = 33, Max = 128 };
27+
yield return new() { Min = 129, Max = 1024 };
28+
yield return new() { Min = 1025, Max = 2048 };
29+
yield return new() { Min = 0, Max = 1024 };
30+
}
31+
32+
public class Range
33+
{
34+
public int Min { get; set; }
35+
public int Max { get; set; }
36+
37+
public override string ToString() => $"[{Min,4}, {Max,4}]";
38+
}
39+
40+
protected static string GenerateLinesText(Range lineLengthRange, int textTargetLength)
41+
{
42+
var min = lineLengthRange.Min;
43+
var max = lineLengthRange.Max;
44+
45+
var newLine = Environment.NewLine;
46+
47+
var capacity = textTargetLength + max + newLine.Length;
48+
var sb = new StringBuilder(capacity);
49+
50+
var random = new Random(42);
51+
int lineCount = 0;
52+
while (sb.Length < textTargetLength)
53+
{
54+
var charsCount = random.Next(min, max);
55+
for (int c = 0; c < charsCount; c++)
56+
{
57+
var ch = (char)random.Next('0', 'z');
58+
sb.Append(ch);
59+
}
60+
sb.Append(newLine);
61+
++lineCount;
62+
}
63+
var text = sb.ToString();
64+
65+
Console.WriteLine($"// Generated lines {lineCount} and " +
66+
$"text length {text.Length} out of capacity {capacity}");
67+
68+
return text;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)