Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions source/Handlebars.Benchmark/RenderToString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.IO;
using BenchmarkDotNet.Attributes;
using HandlebarsDotNet;

namespace HandlebarsNet.Benchmark
{
// Render time for the string-returning template API — the most common way the
// library is consumed. Unlike the other Render* benchmarks (which write to
// TextWriter.Null and therefore only measure resolution overhead), this suite
// pays the real output cost: HTML encoding and StringBuilder-backed writes.
//
// Content=clean: values contain nothing that needs HTML escaping (typical data).
// Content=html: values are dense with characters that must be escaped (worst case).
[MemoryDiagnoser]
public class RenderToString
{
private HandlebarsTemplate<object, object> _template;

Check warning on line 18 in source/Handlebars.Benchmark/RenderToString.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Non-nullable field '_template' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

See more on https://sonarcloud.io/project/issues?id=Handlebars-Net_Handlebars.Net&issues=AZ_P_lnwIl0w-kfIIoex&open=AZ_P_lnwIl0w-kfIIoex&pullRequest=650
private object _data;

Check warning on line 19 in source/Handlebars.Benchmark/RenderToString.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Non-nullable field '_data' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

See more on https://sonarcloud.io/project/issues?id=Handlebars-Net_Handlebars.Net&issues=AZ_P_lnwIl0w-kfIIoey&open=AZ_P_lnwIl0w-kfIIoey&pullRequest=650

private const int ItemCount = 50;

private const string Source =
"<ul>" +
"{{#each items}}" +
"<li id=\"item-{{@index}}\"><span>{{name}}</span> &mdash; {{description}} " +
"<em>x{{qty}}</em>{{#if onSale}} <strong>SALE</strong>{{/if}}</li>" +
"{{/each}}" +
"</ul>";

[Params("clean", "html")]
public string Content { get; set; }

Check warning on line 32 in source/Handlebars.Benchmark/RenderToString.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

See more on https://sonarcloud.io/project/issues?id=Handlebars-Net_Handlebars.Net&issues=AZ_P_lnwIl0w-kfIIoez&open=AZ_P_lnwIl0w-kfIIoez&pullRequest=650

[GlobalSetup]
public void Setup()
{
var handlebars = Handlebars.Create();
_template = handlebars.Compile(Source);

var description = Content == "clean"
? "Reliable everyday item for home and office use"
: "Fast & reliable <everyday> item \"for\" home & office use";

var items = new List<object>(ItemCount);
for (var i = 0; i < ItemCount; i++)
{
items.Add(new
{
name = $"Product {i:D4}",
description,
qty = i * 7 + 1,
onSale = i % 3 == 0
});
}

_data = new { items };
}

[Benchmark]
public string Render() => _template(_data);
}
}
Loading