forked from sebastienros/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TagBenchmarks.cs
148 lines (125 loc) · 4.14 KB
/
TagBenchmarks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
namespace Fluid.Benchmarks
{
[MemoryDiagnoser]
public class TagBenchmarks
{
private static readonly FluidParser _parser = new();
private readonly TemplateContext _context;
private readonly TestCase _rawTag;
private readonly TestCase _ifWithAnds;
private readonly TestCase _ifWithOrs;
private readonly TestCase _elseIf;
private readonly TestCase _assign;
private readonly TestCase _else;
private readonly TestCase _textSpan;
private readonly TestCase _binaryExpressions;
public TagBenchmarks()
{
_rawTag = new TestCase("before {% raw %} {{ TEST 3 }} {% endraw %} after");
_ifWithAnds = new TestCase("{% if true and false and false == false %}HIDDEN{% endif %}");
_ifWithOrs = new TestCase("{% if true == false or false or false %}HIDDEN{% endif %}");
_elseIf = new TestCase("{% if false %}{% elsif true == false or false or false %}HIDDEN{% endif %}");
_else = new TestCase("{% if false %}{% else %}SHOWN{% endif %}");
_assign = new TestCase("{% assign something = 'foo' %} {% assign another = 1234 %} {% assign last = something %}");
_textSpan = new TestCase("foo");
_binaryExpressions = new TestCase("{% if 1 == 'elvis' or 0 == 1 or 1 == 2 or 2 < 1 or 4 > 4 or 1 != 1 or 1 >= 2 or 4 <= 2 or 'abc' contains 'd' or 'abc' startswith 'd' or 'abc' endswith 'd' %}TEXT{% endif %}");
_context = new TemplateContext();
}
[Benchmark]
public object RawTag_Parse()
{
return _rawTag.Parse();
}
[Benchmark]
public string RawTag_Render()
{
return _rawTag.Render(_context);
}
[Benchmark]
public object IfStatement_Ands_Parse()
{
return _ifWithAnds.Parse();
}
[Benchmark]
public string IfStatement_Ands_Render()
{
return _ifWithAnds.Render(_context);
}
[Benchmark]
public object IfStatement_Ors_Parse()
{
return _ifWithOrs.Parse();
}
[Benchmark]
public string IfStatement_Ors_Render()
{
return _ifWithOrs.Render(_context);
}
[Benchmark]
public object ElseIfStatement_Parse()
{
return _elseIf.Parse();
}
[Benchmark]
public string ElseIfStatement_Render()
{
return _elseIf.Render(_context);
}
[Benchmark]
public object Assign_Parse()
{
return _assign.Parse();
}
[Benchmark]
public string Assign_Render()
{
return _assign.Render(_context);
}
[Benchmark]
public object Else_Parse()
{
return _else.Parse();
}
[Benchmark]
public string Else_Render()
{
return _else.Render(_context);
}
[Benchmark]
public object TextSpan_Parse()
{
return _textSpan.Parse();
}
[Benchmark]
public string TextSpan_Render()
{
return _textSpan.Render(_context);
}
[Benchmark]
public object BinaryExpressions_Parse()
{
return _binaryExpressions.Parse();
}
[Benchmark]
public string BinaryExpressions_Render()
{
return _binaryExpressions.Render(_context);
}
private sealed class TestCase
{
private readonly string _source;
private readonly IFluidTemplate _template;
public TestCase(string source)
{
_source = source;
_template = _parser.Parse(source);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Render(TemplateContext context) => _template.Render(context);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IFluidTemplate Parse() => _parser.Parse(_source);
}
}
}