Poor Benchmark Performance
See original GitHub issueI’m evaluating various C# templating libraries. I saw that the Cottle library had a benchmark that included RazorLight, so I added RazorEngineCore to it.
The results are very bad, so I’m thinking there must be something wrong with how I wrote the benchmark.
Here are some of the results:
As you can see, RazorEngineCore is ~321 times slower than the next slowest library.
Here’s the implementation:
const string content = @"
<ul id='products'>
@foreach (var product in Model.Products)
{
<li>
<h2>@product.Name</h2>
<p>@product.Description.Substring(0, System.Math.Min(product.Description.Length, 15)) - Only @product.Price.ToString(""f1"", System.Globalization.CultureInfo.CreateSpecificCulture(""en-US""))$</p>
</li>
}
</ul>";
var context = new { Products = CompareEngine.Products };
return () =>
{
var engine = new RazorEngine();
return () => engine.Compile(content).Run(context);
};
For comparison, here’s the code for the RazorLight benchmark:
const string content = @"
<ul id='products'>
@foreach (var product in Model.Products)
{
<li>
<h2>@product.Name</h2>
<p>@product.Description.Substring(0, System.Math.Min(product.Description.Length, 15)) - Only @product.Price.ToString(""f1"", System.Globalization.CultureInfo.CreateSpecificCulture(""en-US""))$</p>
</li>
}
</ul>";
var context = new { CompareEngine.Products };
return () =>
{
var engine = new RazorLightEngineBuilder().UseProject(new StringRazorLightProject(content)).Build();
return () => engine.CompileRenderAsync(string.Empty, context).Result;
};
Issue Analytics
- State:
- Created a year ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
PerformanceTest FAQ - Poor PC Performance
We receive many questions from users about poor PC performance, and whether there is anything a user can do to improve the speed...
Read more >UserBenchmark Test showing poor performance, brand ...
Don't use user benchmark for one thing. It's not indicative of real performance, is often inaccurate and is all around crap. Try something...
Read more >[SOLVED] - UserBenchmark bad results
Today i tested my pc on userbenchmark and got poor results for my pc saying its not running up to standard and dont...
Read more >"Poor" Benchmark Performance
Hey, after seeing a youtube video where someone compared Chrome, Edge, Firefox, Brave and Vivaldi on mobile and desktop and showed there, ...
Read more >My PC according to user benchmark is performing way ...
What can I do to increase gaming performance? I linked my PC build/bench. What is causing me to get low frames? 1,303 Views....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
What about this?
In RazorEngineCore you do:
I.e. it’s creating a file in the filesystem
Whereas the equivalent use of RazorSourceDocument in RazorLight is this:
(And note that in the above benchmark, it uses TextSourceRazorProjectItem, not the file system project item - so
stream
above comes fromnew MemoryStream(Encoding.UTF8.GetBytes(_content));
)So I don’t think the issue is just that RazorLight is caching here and that’s why it does better on the benchmark - it looks to me like RazorEngineCore will be reading and writing to the filesystem whereas RazorLight will just be using data held in memory. That alone seems like it could explain the benchmark performance.
This is the benchmark code for RazorLight:
It’s not calling
UseMemoryCachingProvider
, so my impression is that it won’t cache the templates. I’ll dig into that a bit further to confirm - I haven’t looked much at the RazorLight source code.Update: I wrote this test in the RazorLight repo:
Which is how the Cottle benchmark works. I stepped through the code compiled that same template multiple times and I can confirm RazorLight is NOT caching the template with the above code (because IsCachingEnabled returns false) which means it really is hundreds of times faster than RazorEngineCore. My guess is that it’s because of the lack of writing to the filesystem which I described above.
I don’t think an informal benchmark like that is an accurate representation of performance - like you say, when you set it up like that, the order of execution matters (and that doesn’t account for GC, JIT optimizations, etc). That’s why benchmark frameworks like BenchmarkDotNet (https://github.com/dotnet/BenchmarkDotNet) are used.
But no worries - I won’t take up any more of your time. Best of luck with your library!