The benchmarks test the performance of using NodeServices in a .Net Core web application to compile & serve Handlebars templates versus the standard Razor templates.
To run the benchmarks, first clone or download the project, then run dotnet run -c release in the /NodeServicesBenchmark/NodeServicesBenchmark.BenchmarkRunner directory.
The benchmark application uses BenchmarkDotNet to run the benchmarks. The benchmarks set use a TestServer, which uses the Microsoft.AspNetCore.TestHost package, to create a test instance of the benchmark website.
There are two classes which contain benchmarks; LoopBenchmarks.cs and RandomLoopBenchmarks.cs, these call out to the routes described in the Web App Details section.
In the web project there are six routes with the following in each:
- Razor template with the same data each time the page is loaded
- Handlebars template with the same data each time the page is loaded
- Cached Handlebars template with the same data each time the page is loaded
- Razor template with random data each time the page is loaded
- Handlebars template with random data each time the page is loaded
- Cached Handlebars template with random data each time the page is loaded
Both the random and non-random sets of routes are served via two view components. Each view component contains logic which decides how it is going to serve up the HTML — Razor or Handlebars.
The handlebars templates are served via NodeServices in TemplateService.cs which calls a node module called templates.js.
templates.js is a self contained, pre-bundled version of the Just Eat f-templates module which looks up Handlebars templates, partials, and resources (translations) which live in a templates directory. Because it has been pre-bundled there is no need to deploy a node_modules directory to a production server.
Node services can be called in one of two ways
The TemplateService calls INodeServices.InvokeExportAsync(), passing down some options, which then returns a Handlebars template.
The CachedTemplateService calls down to the TemplateService and then uses IMemoryCache to store the result in an in-memory cache, so any subsequent requests will return the result from the cache.