1+ using System . Collections . Generic ;
2+ using System . IO ;
3+ using System . Text . Json ;
4+ using BenchmarkDotNet . Attributes ;
5+ using HandlebarsDotNet ;
6+ using HandlebarsDotNet . Extension . Json ;
7+
8+ namespace HandlebarsNet . Extension . Benchmark
9+ {
10+ public class EndToEnd
11+ {
12+ private object _data ;
13+ private HandlebarsTemplate < TextWriter , object , object > _default ;
14+
15+ [ Params ( 5 ) ]
16+ public int N { get ; set ; }
17+
18+ [ Params ( "json" ) ]
19+ public string DataType { get ; set ; }
20+
21+ [ GlobalSetup ]
22+ public void Setup ( )
23+ {
24+ const string template = @"
25+ childCount={{level1.Count}}
26+ {{#each level1}}
27+ id={{id}}
28+ childCount={{level2.Count}}
29+ index=[{{@../../index}}:{{@../index}}:{{@index}}]
30+ pow1=[{{pow1 @index}}]
31+ pow2=[{{pow2 @index}}]
32+ pow3=[{{pow3 @index}}]
33+ pow4=[{{pow4 @index}}]
34+ pow5=[{{#pow5 @index}}empty{{/pow5}}]
35+ {{#each level2}}
36+ id={{id}}
37+ childCount={{level3.Count}}
38+ index=[{{@../../index}}:{{@../index}}:{{@index}}]
39+ pow1=[{{pow1 @index}}]
40+ pow2=[{{pow2 @index}}]
41+ pow3=[{{pow3 @index}}]
42+ pow4=[{{pow4 @index}}]
43+ pow5=[{{#pow5 @index}}empty{{/pow5}}]
44+ {{#each level3}}
45+ id={{id}}
46+ index=[{{@../../index}}:{{@../index}}:{{@index}}]
47+ pow1=[{{pow1 @index}}]
48+ pow2=[{{pow2 @index}}]
49+ pow3=[{{pow3 @index}}]
50+ pow4=[{{pow4 @index}}]
51+ pow5=[{{#pow5 @index}}empty{{/pow5}}]
52+ {{/each}}
53+ {{/each}}
54+ {{/each}}" ;
55+
56+ switch ( DataType )
57+ {
58+ case "json" :
59+ var json = JsonSerializer . Serialize ( new { level1 = ObjectLevel1Generator ( ) } ) ;
60+ _data = JsonDocument . Parse ( json ) ;
61+ break ;
62+ }
63+
64+ var handlebars = Handlebars . Create ( ) ;
65+
66+ handlebars . Configuration . UseJson ( ) ;
67+
68+ handlebars . RegisterHelper ( "pow1" , ( output , context , arguments ) => output . WriteSafeString ( ( ( int ) arguments [ 0 ] * ( int ) arguments [ 0 ] ) . ToString ( ) ) ) ;
69+ handlebars . RegisterHelper ( "pow2" , ( output , context , arguments ) => output . WriteSafeString ( ( ( int ) arguments [ 0 ] * ( int ) arguments [ 0 ] ) . ToString ( ) ) ) ;
70+ handlebars . RegisterHelper ( "pow5" , ( output , options , context , arguments ) => output . WriteSafeString ( ( ( int ) arguments [ 0 ] * ( int ) arguments [ 0 ] ) . ToString ( ) ) ) ;
71+
72+ using ( var reader = new StringReader ( template ) )
73+ {
74+ _default = handlebars . Compile ( reader ) ;
75+ }
76+
77+ handlebars . RegisterHelper ( "pow3" , ( output , context , arguments ) => output . WriteSafeString ( ( ( int ) arguments [ 0 ] * ( int ) arguments [ 0 ] ) . ToString ( ) ) ) ;
78+ handlebars . RegisterHelper ( "pow4" , ( output , context , arguments ) => output . WriteSafeString ( ( ( int ) arguments [ 0 ] * ( int ) arguments [ 0 ] ) . ToString ( ) ) ) ;
79+
80+ List < object > ObjectLevel1Generator ( )
81+ {
82+ var level = new List < object > ( ) ;
83+ for ( int i = 0 ; i < N ; i ++ )
84+ {
85+ level . Add ( new
86+ {
87+ id = $ "{ i } ",
88+ level2 = ObjectLevel2Generator ( i )
89+ } ) ;
90+ }
91+
92+ return level ;
93+ }
94+
95+ List < object > ObjectLevel2Generator ( int id1 )
96+ {
97+ var level = new List < object > ( ) ;
98+ for ( int i = 0 ; i < N ; i ++ )
99+ {
100+ level . Add ( new
101+ {
102+ id = $ "{ id1 } -{ i } ",
103+ level3 = ObjectLevel3Generator ( id1 , i )
104+ } ) ;
105+ }
106+
107+ return level ;
108+ }
109+
110+ List < object > ObjectLevel3Generator ( int id1 , int id2 )
111+ {
112+ var level = new List < object > ( ) ;
113+ for ( int i = 0 ; i < N ; i ++ )
114+ {
115+ level . Add ( new
116+ {
117+ id = $ "{ id1 } -{ id2 } -{ i } "
118+ } ) ;
119+ }
120+
121+ return level ;
122+ }
123+ }
124+
125+ [ Benchmark ]
126+ public void Default ( ) => _default ( TextWriter . Null , _data ) ;
127+ }
128+ }
0 commit comments