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

3 files changed

Lines changed: 111 additions & 56 deletions

File tree

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+
usingSystem.Text;
6+
usingSystem.Threading.Tasks;
7+
usingBenchmarkDotNet.Attributes;
8+
usingMicroBenchmarks;
9+
10+
namespaceSystem.IO.Tests
11+
{
12+
[BenchmarkCategory(Categories.Libraries)]
13+
publicclassStreamReaderReadLineTests:TextReaderReadLineTests
14+
{
15+
privatebyte[]_bytes;
16+
17+
[GlobalSetup]
18+
publicvoidGlobalSetup()
19+
{
20+
_text=GenerateLinesText(LineLengthRange,16*1024);
21+
_bytes=Encoding.UTF8.GetBytes(_text);
22+
}
23+
24+
[Benchmark]
25+
publicvoidReadLine()
26+
{
27+
usingStreamReaderreader=new(newMemoryStream(_bytes));
28+
while(reader.ReadLine()!=null);
29+
}
30+
31+
[Benchmark]
32+
[BenchmarkCategory(Categories.NoWASM)]
33+
publicasyncTaskReadLineAsync()
34+
{
35+
usingStreamReaderreader=new(newMemoryStream(_bytes));
36+
while(awaitreader.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
namespaceSystem.IO.Tests
1313
{
1414
[BenchmarkCategory(Categories.Libraries)]
15-
publicclassStringReaderReadLineTests
15+
publicclassStringReaderReadLineTests:TextReaderReadLineTests
1616
{
17-
privatestring_text;
18-
19-
[ParamsSource(nameof(GetLineLengthRanges))]
20-
publicRangeLineLengthRange{get;set;}
21-
22-
publicstaticIEnumerable<Range>GetLineLengthRanges()
23-
{
24-
yieldreturnnew(){Min=0,Max=0};
25-
yieldreturnnew(){Min=1,Max=1};
26-
yieldreturnnew(){Min=1,Max=8};
27-
yieldreturnnew(){Min=9,Max=32};
28-
yieldreturnnew(){Min=33,Max=128};
29-
yieldreturnnew(){Min=129,Max=1024};
30-
yieldreturnnew(){Min=0,Max=1024};
31-
}
32-
33-
publicclassRange
34-
{
35-
publicintMin{get;set;}
36-
publicintMax{get;set;}
37-
38-
publicoverridestringToString()=>$"[{Min,4}, {Max,4}]";
39-
}
40-
4117
[GlobalSetup]
4218
publicvoidGlobalSetup()=>_text=GenerateLinesText(LineLengthRange,16*1024);
4319

@@ -55,36 +31,5 @@ public async Task ReadLineAsync()
5531
usingStringReaderreader=new(_text);
5632
while(awaitreader.ReadLineAsync()!=null);
5733
}
58-
59-
privatestaticstringGenerateLinesText(RangelineLengthRange,inttextTargetLength)
60-
{
61-
varmin=lineLengthRange.Min;
62-
varmax=lineLengthRange.Max;
63-
64-
varnewLine=Environment.NewLine;
65-
66-
varcapacity=textTargetLength+max+newLine.Length;
67-
varsb=newStringBuilder(capacity);
68-
69-
varrandom=newRandom(42);
70-
intlineCount=0;
71-
while(sb.Length<textTargetLength)
72-
{
73-
varcharsCount=random.Next(min,max);
74-
for(intc=0;c<charsCount;c++)
75-
{
76-
varch=(char)random.Next('0','z');
77-
sb.Append(ch);
78-
}
79-
sb.Append(newLine);
80-
++lineCount;
81-
}
82-
vartext=sb.ToString();
83-
84-
Console.WriteLine($"// Generated lines {lineCount} and "+
85-
$"text length {text.Length} out of capacity {capacity}");
86-
87-
returntext;
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+
usingSystem.Collections.Generic;
6+
usingSystem.Text;
7+
usingBenchmarkDotNet.Attributes;
8+
usingMicroBenchmarks;
9+
10+
namespaceSystem.IO.Tests
11+
{
12+
[BenchmarkCategory(Categories.Libraries)]
13+
publicabstractclassTextReaderReadLineTests
14+
{
15+
protectedstring_text;
16+
17+
[ParamsSource(nameof(GetLineLengthRanges))]
18+
publicRangeLineLengthRange{get;set;}
19+
20+
publicstaticIEnumerable<Range>GetLineLengthRanges()
21+
{
22+
yieldreturnnew(){Min=0,Max=0};
23+
yieldreturnnew(){Min=1,Max=1};
24+
yieldreturnnew(){Min=1,Max=8};
25+
yieldreturnnew(){Min=9,Max=32};
26+
yieldreturnnew(){Min=33,Max=128};
27+
yieldreturnnew(){Min=129,Max=1024};
28+
yieldreturnnew(){Min=1025,Max=2048};
29+
yieldreturnnew(){Min=0,Max=1024};
30+
}
31+
32+
publicclassRange
33+
{
34+
publicintMin{get;set;}
35+
publicintMax{get;set;}
36+
37+
publicoverridestringToString()=>$"[{Min,4}, {Max,4}]";
38+
}
39+
40+
protectedstaticstringGenerateLinesText(RangelineLengthRange,inttextTargetLength)
41+
{
42+
varmin=lineLengthRange.Min;
43+
varmax=lineLengthRange.Max;
44+
45+
varnewLine=Environment.NewLine;
46+
47+
varcapacity=textTargetLength+max+newLine.Length;
48+
varsb=newStringBuilder(capacity);
49+
50+
varrandom=newRandom(42);
51+
intlineCount=0;
52+
while(sb.Length<textTargetLength)
53+
{
54+
varcharsCount=random.Next(min,max);
55+
for(intc=0;c<charsCount;c++)
56+
{
57+
varch=(char)random.Next('0','z');
58+
sb.Append(ch);
59+
}
60+
sb.Append(newLine);
61+
++lineCount;
62+
}
63+
vartext=sb.ToString();
64+
65+
Console.WriteLine($"// Generated lines {lineCount} and "+
66+
$"text length {text.Length} out of capacity {capacity}");
67+
68+
returntext;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)