66using InfiniBlazor . Markdown ;
77using InfiniBlazor . Markdown . Syntax ;
88using Microsoft . Extensions . DependencyInjection ;
9+ using System . Text ;
910
1011namespace Benchmarks . InfiniBlazor . Markdown ;
1112// ---------------------------------------------------------------------------------------------------------------------
@@ -26,6 +27,9 @@ public void Setup() {
2627 ServiceProvider provider = serviceCollection . BuildServiceProvider ( ) ;
2728
2829 Parser = provider . GetRequiredService < IMarkdownParser > ( ) ;
30+
31+ // Warmup to avoid first-hit cache effects
32+ _ = Parser . Markdown . SerializeToSyntaxTree ( "warmup" ) ;
2933 }
3034
3135 public sealed record BenchmarkCase ( string Name , string Markdown ) {
@@ -39,6 +43,8 @@ public sealed record BenchmarkCase(string Name, string Markdown) {
3943 // Data
4044 // -----------------------------------------------------------------------------------------------------------------
4145 private static IEnumerable < BenchmarkCase > Cases => [
46+ new ( "Paragraph_Base" , "This is a paragraph." ) , // baseline
47+
4248 #region BlockQuote
4349 new ( "BlockQuote_1Line" , "> Blockquote text" ) ,
4450 new ( "BlockQuote_2Lines" , """
@@ -51,6 +57,8 @@ public sealed record BenchmarkCase(string Name, string Markdown) {
5157 > with multiple lines.
5258 """
5359 ) ,
60+ new ( "BlockQuote_10Lines" , RepeatLines ( "> line" , 10 ) ) ,
61+ new ( "BlockQuote_100Lines" , RepeatLines ( "> line" , 100 ) ) ,
5462 #endregion
5563
5664 #region Bold
@@ -85,6 +93,16 @@ public void MyMethod() {
8593 something code related
8694 ```
8795 """ ) ,
96+ new ( "CodeBlock_50Lines" , $ """
97+ ```csharp
98+ { RepeatLines ( "Console.WriteLine(\" Hello\" );" , 50 ) }
99+ ```
100+ """ ) ,
101+ new ( "CodeBlock_100Lines" , $ """
102+ ```csharp
103+ { RepeatLines ( "Console.WriteLine(\" Hello\" );" , 100 ) }
104+ ```
105+ """ ) ,
88106 #endregion
89107
90108 #region CodeInline
@@ -161,44 +179,51 @@ something code related
161179 - list item 1
162180 - list item 2
163181 """ ) ,
182+ new ( "List_UnOrdered_50" , RepeatLines ( "- item" , 50 ) ) ,
183+ new ( "List_UnOrdered_100" , RepeatLines ( "- item" , 100 ) ) ,
184+
164185 new ( "List_Ordered" , """
165186 1. list item 1
166187 2. list item 2
167188 """ ) ,
168- new ( "List_Task" , """
169- - [ ] task item 1
170- """ ) ,
171- new ( "List_Task_checked" , """
172- - [x] task item 1
173- """ ) ,
189+ new ( "List_Ordered_50" , RepeatLines ( "1. item" , 50 ) ) ,
190+ new ( "List_Ordered_100" , RepeatLines ( "1. item" , 100 ) ) ,
191+
192+ new ( "List_Task" , "- [ ] task item 1" ) ,
193+ new ( "List_Task_50" , RepeatLines ( "- [ ] item" , 50 ) ) ,
194+ new ( "List_Task_100" , RepeatLines ( "- [ ] item" , 100 ) ) ,
195+
196+ new ( "List_Task_checked" , "- [x] task item 1" ) ,
197+ new ( "List_Task_checked_50" , RepeatLines ( "- [x] item" , 50 ) ) ,
198+ new ( "List_Task_checked_100" , RepeatLines ( "- [x] item" , 100 ) ) ,
174199 #endregion
175-
200+
176201 #region NewLine
177202 new ( "NewLine" , """
178203 line 1
179204 line 2
180205 """ ) ,
181206 #endregion
182-
207+
183208 #region Paragraph
184209 new ( "Paragraph" , "This is a paragraph." ) ,
185210 #endregion
186-
211+
187212 #region Strikethrough
188213 new ( "Strikethrough" , "~~strikethrough~~" ) ,
189214 new ( "Strikethrough_2InLine" , "~~strikethrough~~ ~~again~~" ) ,
190215 #endregion
191-
216+
192217 #region Subscript
193218 new ( "Subscript" , "~sub-script~" ) ,
194219 new ( "Subscript_2InLine" , "~sub-script~ ~again~" ) ,
195220 #endregion
196-
221+
197222 #region Superscript
198223 new ( "Superscript" , "^sup-script^" ) ,
199224 new ( "Superscript_2InLine" , "^sup-script^ ^again^" ) ,
200225 #endregion
201-
226+
202227 #region Table
203228 new ( "Table" , """
204229 | Header 1 | Header 2 |
@@ -219,43 +244,80 @@ line 2
219244 | Row 3 | Data 3 |
220245 """ ) ,
221246 #endregion
222-
247+
223248 #region Tag
224249 new ( "Tag" , "#tag" ) ,
225250 #endregion
226-
251+
227252 #region Template
228253 new ( "Template" , "{{template}}" ) ,
229254 #endregion
230-
255+
231256 #region Underline
232257 new ( "Underline" , "_underline_" ) ,
233258 #endregion
234-
259+
235260 #region User
236261 new ( "User" , "@user" ) ,
237262 #endregion
238-
263+
239264 #region WikiLink
240265 new ( "WikiLink" , "[[WikiLink]]" ) ,
241266 #endregion
242-
267+
243268 #region Wrapper
244269 new ( "Wrapper" , "<|color=red>red text</>" ) ,
245270 #endregion
246-
271+
247272 #region BoldAndItalic
248273 new ( "BoldAndItalic" , "***bold and italic***" ) ,
249274 new ( "BoldAndItalic_AfterEachOther" , "**bold** and *italic*" ) ,
250275 #endregion
276+
277+ #region Mixed
278+ new ( "Mixed_RealWorld" , """
279+ # Title
280+
281+ Intro paragraph with *italic*, **bold**, and a [link](https://example.com).
282+
283+ - item one
284+ - item two
285+ - nested item
286+
287+ > A note block
288+ > with multiple lines.
289+
290+ ```csharp
291+ public record Sample(int Id, string Name);
292+ ```
293+ """ ) ,
294+ #endregion
251295 ] ;
252296
253297 // -----------------------------------------------------------------------------------------------------------------
254298 // Benchmarks
255299 // -----------------------------------------------------------------------------------------------------------------
300+ [ Benchmark ( Baseline = true ) ]
301+ public IMdSyntaxTree SerializeToSyntaxTree_ParagraphBaseline ( ) {
302+ IMdSyntaxTree tree = Parser . Markdown . SerializeToSyntaxTree ( "This is a paragraph." ) ;
303+ return tree ;
304+ }
305+
256306 [ Benchmark ]
257307 public IMdSyntaxTree SerializeToSyntaxTree ( ) {
258308 IMdSyntaxTree tree = Parser . Markdown . SerializeToSyntaxTree ( InputCase . Markdown ) ;
259309 return tree ;
260310 }
311+
312+ // -----------------------------------------------------------------------------------------------------------------
313+ // Helpers
314+ // -----------------------------------------------------------------------------------------------------------------
315+ private static string RepeatLines ( string line , int count ) {
316+ var sb = new StringBuilder ( ) ;
317+ for ( int i = 0 ; i < count ; i ++ ) {
318+ sb . AppendLine ( line ) ;
319+ }
320+
321+ return sb . ToString ( ) ;
322+ }
261323}
0 commit comments