-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
443 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
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; | ||
|
||
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"); | ||
|
||
_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); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.