@@ -40,13 +40,15 @@ namespace Benchmarks
4040 public class HtmlTokenizerBenchmarks
4141 {
4242 static readonly string HtmlDataDir = Path . Combine ( BenchmarkHelper . ProjectDir , "TestData" , "html" ) ;
43+ static readonly byte [ ] Xamarin3 = File . ReadAllBytes ( Path . Combine ( HtmlDataDir , "xamarin3.xhtml" ) ) ;
44+ static readonly byte [ ] Papercut44 = File . ReadAllBytes ( Path . Combine ( HtmlDataDir , "papercut-4.4.html" ) ) ;
4345
4446 #region HtmlKit
4547
46- static void HtmlKit_TokenizeTextReader ( string fileName )
48+ static void HtmlKit_TokenizeTextReader ( byte [ ] rawData )
4749 {
48- var path = Path . Combine ( HtmlDataDir , fileName ) ;
49- using var reader = new StreamReader ( path ) ;
50+ using var stream = new MemoryStream ( rawData , false ) ;
51+ using var reader = new StreamReader ( stream ) ;
5052 var tokenizer = new HtmlTokenizer ( reader ) ;
5153
5254 while ( tokenizer . ReadNextToken ( out var token ) )
@@ -56,13 +58,18 @@ static void HtmlKit_TokenizeTextReader (string fileName)
5658 [ Benchmark ]
5759 public void HtmlKit_TextReader_Xamarin3 ( )
5860 {
59- HtmlKit_TokenizeTextReader ( "xamarin3.xhtml" ) ;
61+ HtmlKit_TokenizeTextReader ( Xamarin3 ) ;
6062 }
6163
62- static void HtmlKit_TokenizeStream ( string fileName )
64+ [ Benchmark ]
65+ public void HtmlKit_TextReader_Papercut44 ( )
66+ {
67+ HtmlKit_TokenizeTextReader ( Papercut44 ) ;
68+ }
69+
70+ static void HtmlKit_TokenizeStream ( byte [ ] rawData )
6371 {
64- var path = Path . Combine ( HtmlDataDir , fileName ) ;
65- using var stream = File . OpenRead ( path ) ;
72+ using var stream = new MemoryStream ( rawData , false ) ;
6673 var tokenizer = new HtmlTokenizer ( stream ) ;
6774
6875 while ( tokenizer . ReadNextToken ( out var token ) )
@@ -72,17 +79,23 @@ static void HtmlKit_TokenizeStream (string fileName)
7279 [ Benchmark ]
7380 public void HtmlKit_Stream_Xamarin3 ( )
7481 {
75- HtmlKit_TokenizeStream ( "xamarin3.xhtml" ) ;
82+ HtmlKit_TokenizeStream ( Xamarin3 ) ;
83+ }
84+
85+ [ Benchmark ]
86+ public void HtmlKit_Stream_Papercut44 ( )
87+ {
88+ HtmlKit_TokenizeStream ( Papercut44 ) ;
7689 }
7790
7891 #endregion HtmlKit
7992
8093 #region HtmlPerformanceKit
8194
82- static void HtmlPerformanceKit_TokenizeFile ( string fileName )
95+ static void HtmlPerformanceKit_TokenizeFile ( byte [ ] rawData )
8396 {
84- var path = Path . Combine ( HtmlDataDir , fileName ) ;
85- using var reader = new StreamReader ( path ) ;
97+ using var stream = new MemoryStream ( rawData , false ) ;
98+ using var reader = new StreamReader ( stream ) ;
8699 var tokenizer = new HtmlPerformanceKit . HtmlReader ( reader ) ;
87100
88101 while ( tokenizer . Read ( ) ) {
@@ -116,17 +129,22 @@ static void HtmlPerformanceKit_TokenizeFile (string fileName)
116129 [ Benchmark ]
117130 public void HtmlPerformanceKit_Xamarin3 ( )
118131 {
119- HtmlPerformanceKit_TokenizeFile ( "xamarin3.xhtml" ) ;
132+ HtmlPerformanceKit_TokenizeFile ( Xamarin3 ) ;
133+ }
134+
135+ [ Benchmark ]
136+ public void HtmlPerformanceKit_Papercut44 ( )
137+ {
138+ HtmlPerformanceKit_TokenizeFile ( Papercut44 ) ;
120139 }
121140
122141 #endregion HtmlPerformanceKit
123142
124143 #region AngleSharp
125144
126- static void AngleSharp_TokenizeFile ( string fileName )
145+ static void AngleSharp_TokenizeFile ( byte [ ] rawData )
127146 {
128- var path = Path . Combine ( HtmlDataDir , fileName ) ;
129- using var stream = File . OpenRead ( path ) ;
147+ using var stream = new MemoryStream ( rawData , false ) ;
130148 using var source = new AngleSharp . Text . TextSource ( stream ) ;
131149
132150 var tokenizer = new ASHtmlTokenizer ( source , AngleSharp . Html . HtmlEntityProvider . Resolver ) ;
@@ -140,17 +158,22 @@ static void AngleSharp_TokenizeFile (string fileName)
140158 [ Benchmark ]
141159 public void AngleSharp_Xamarin3 ( )
142160 {
143- AngleSharp_TokenizeFile ( "xamarin3.xhtml" ) ;
161+ AngleSharp_TokenizeFile ( Xamarin3 ) ;
162+ }
163+
164+ [ Benchmark ]
165+ public void AngleSharp_Papercut44 ( )
166+ {
167+ AngleSharp_TokenizeFile ( Papercut44 ) ;
144168 }
145169
146170 #endregion AngleSharp
147171
148172 #region XmlReader
149173
150- static void XmlReader_TokenizeFile ( string fileName )
174+ static void XmlReader_TokenizeFile ( byte [ ] rawData )
151175 {
152- var path = Path . Combine ( HtmlDataDir , fileName ) ;
153- using var stream = File . OpenRead ( path ) ;
176+ using var stream = new MemoryStream ( rawData , false ) ;
154177
155178 var settings = new XmlReaderSettings ( ) {
156179 DtdProcessing = DtdProcessing . Parse
@@ -187,7 +210,7 @@ static void XmlReader_TokenizeFile (string fileName)
187210 [ Benchmark ]
188211 public void XmlReader_Xamarin3 ( )
189212 {
190- XmlReader_TokenizeFile ( "xamarin3.xhtml" ) ;
213+ XmlReader_TokenizeFile ( Xamarin3 ) ;
191214 }
192215
193216 #endregion XmlReader
0 commit comments