Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Jint.Benchmark/Jint.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</PropertyGroup>
<ItemGroup>
<None Include=".\Scripts\*.*" CopyToOutputDirectory="PreserveNewest" />
<None Include=".\Scripts\template-rendering\**\*.*" CopyToOutputDirectory="PreserveNewest" LinkBase="Scripts\template-rendering" />
<None Include="..\Jint.Tests.CommonScripts\Scripts\**" CopyToOutputDirectory="PreserveNewest" LinkBase="Scripts" />
</ItemGroup>
<ItemGroup>
Expand Down
71 changes: 71 additions & 0 deletions Jint.Benchmark/PreparedScriptBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using BenchmarkDotNet.Attributes;

namespace Jint.Benchmark;

/// <summary>
/// Replicates the exact pattern from JavaScriptEngineSwitcher's JsExecutionHeavyBenchmark:
/// a Handlebars template rendering library is loaded on 4 separate engine instances,
/// each calling renderTemplate() with different content items.
/// See: https://github.com/Taritsyn/JavaScriptEngineSwitcher/blob/master/test/JavaScriptEngineSwitcher.Benchmarks/JsExecutionHeavyBenchmark.cs
/// </summary>
[MemoryDiagnoser]
public class PreparedScriptBenchmark
{
private string _libraryCode = null!;
private Prepared<Script> _preparedLibrary;
private ContentItem[] _contentItems = null!;

[GlobalSetup]
public void Setup()
{
var handlebars = File.ReadAllText("Scripts/template-rendering/lib/handlebars.js");
var helpers = File.ReadAllText("Scripts/template-rendering/lib/helpers.js");
_libraryCode = handlebars + "\n" + helpers;
_preparedLibrary = Engine.PrepareScript(_libraryCode);

var contentDir = "Scripts/template-rendering/content";
string[] names = ["hello-world", "contacts", "js-engines", "web-browser-family-tree"];
_contentItems = names.Select(name => new ContentItem(
File.ReadAllText(Path.Combine(contentDir, name, "template.handlebars")),
File.ReadAllText(Path.Combine(contentDir, name, "data.json"))
)).ToArray();
}

[Benchmark(Baseline = true)]
public void ExecuteStringOnMultipleEngines()
{
RenderTemplates(withPrecompilation: false);
}

[Benchmark]
public void ExecutePreparedOnMultipleEngines()
{
RenderTemplates(withPrecompilation: true);
}

private void RenderTemplates(bool withPrecompilation)
{
// First engine: load library and render first item
var engine = new Engine();
if (withPrecompilation)
engine.Execute(_preparedLibrary);
else
engine.Execute(_libraryCode);

engine.Invoke("renderTemplate", _contentItems[0].Template, _contentItems[0].Data);

// Remaining engines: each loads the same library and renders one item
for (int i = 1; i < _contentItems.Length; i++)
{
engine = new Engine();
if (withPrecompilation)
engine.Execute(_preparedLibrary);
else
engine.Execute(_libraryCode);

engine.Invoke("renderTemplate", _contentItems[i].Template, _contentItems[i].Data);
}
}

private sealed record ContentItem(string Template, string Data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"lastName": "Пупкин",
"firstName": "Василий",
"patronymic": "Иванович",
"address": {
"postalCode": 392032,
"country": "Россия",
"state": "Тамбовская область",
"city": "Тамбов",
"street": "Магистральная",
"houseNumber": "41к7",
"floor": 8,
"apartmentNumber": 115
},
"homePhone": "+7 (4752) 555-55-55",
"mobilePhone": "+7 (999) 689-99-99",
"email": "pupkin@mail.ru",
"website": "http:\/\/pupkin.narod.ru",
"icq": 698426795
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<address>
<strong><abbr title="Фамилия Имя Отчество">Ф.И.О.</abbr>:</strong> {{lastName}} {{firstName}} {{patronymic}}<br />
<strong>Адрес:</strong>
{{#with address}}
{{postalCode}}, {{country}}, {{state}}, {{city}}, ул. {{street}}, д. {{houseNumber}}, кв. {{apartmentNumber}}
{{/with}}
<br />
<strong>Дом. тел.:</strong> {{homePhone}}<br />
<strong>Моб. тел.:</strong> {{mobilePhone}}<br />
<strong>Email:</strong> <a href="mailto:{{email}}">{{email}}</a><br />
<strong>Сайт:</strong> {{{link website website false}}}<br />
<strong>ICQ:</strong> <img src="http://status.icq.com/online.gif?icq={{icq}}&img=26" alt="ICQ статус" />{{icq}}
</address>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"lastName": "Pupkin",
"firstName": "Vasilii",
"patronymic": "Ivanovich",
"occupation": "Enikeyschik"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="greeting">
{{!-- Welcome by first name and patronymic --}}
Hello, {{firstName}} {{patronymic}}!
</div>
Loading