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
Add benchmarking tool
Useful for testing performance-related changes
  • Loading branch information
dustinsoftware committed Apr 26, 2018
commit f0a5a23f9da1b3e5943611af77bc1d8757119d98
9 changes: 8 additions & 1 deletion src/React.sln
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "React.Sample.CoreMvc", "Rea
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "React.Router.Mvc4", "React.Router.Mvc4\React.Router.Mvc4.csproj", "{2170D912-86E9-4CE3-8DA4-E1DE8D958E63}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "React.Sample.Router.CoreMvc", "React.Sample.Router.CoreMvc\React.Sample.Router.CoreMvc.csproj", "{5BFA69C8-2E66-4112-AC30-CE31503F4175}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "React.Sample.Router.CoreMvc", "React.Sample.Router.CoreMvc\React.Sample.Router.CoreMvc.csproj", "{5BFA69C8-2E66-4112-AC30-CE31503F4175}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "React.Tests.Benchmarks", "..\tests\React.Tests.Benchmarks\React.Tests.Benchmarks.csproj", "{083462CB-2FC0-4508-A7ED-4B77B44C3E23}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -154,6 +156,10 @@ Global
{5BFA69C8-2E66-4112-AC30-CE31503F4175}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5BFA69C8-2E66-4112-AC30-CE31503F4175}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5BFA69C8-2E66-4112-AC30-CE31503F4175}.Release|Any CPU.Build.0 = Release|Any CPU
{083462CB-2FC0-4508-A7ED-4B77B44C3E23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{083462CB-2FC0-4508-A7ED-4B77B44C3E23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{083462CB-2FC0-4508-A7ED-4B77B44C3E23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{083462CB-2FC0-4508-A7ED-4B77B44C3E23}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -178,6 +184,7 @@ Global
{305918EF-AD05-4743-9B3A-DB1CE84D467E} = {A51CE5B6-294F-4D39-B32B-BF08DAF9B40B}
{2170D912-86E9-4CE3-8DA4-E1DE8D958E63} = {681C45FB-103C-48BC-B992-20C5B6B78F92}
{5BFA69C8-2E66-4112-AC30-CE31503F4175} = {A51CE5B6-294F-4D39-B32B-BF08DAF9B40B}
{083462CB-2FC0-4508-A7ED-4B77B44C3E23} = {F567B25C-E869-4C93-9C96-077761250F87}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DCF4A41E-C60D-4086-98A9-6F8508D7E8D0}
Expand Down
69 changes: 69 additions & 0 deletions tests/React.Tests.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using JavaScriptEngineSwitcher.ChakraCore;
using JavaScriptEngineSwitcher.Core;
using Newtonsoft.Json.Linq;
using React.Web.Mvc;

namespace React.Tests.Benchmarks
{
public static class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<ComponentRenderBenchmarks>();
}

[MemoryDiagnoser]
public class ComponentRenderBenchmarks
{
public static void PopulateTestData()
{
for (int i = 0; i < 10000; i++)
{
_testData.Add("key" + i, "value" + i);
}
}

public ComponentRenderBenchmarks()
{
PopulateTestData();

Initializer.Initialize(registration => registration.AsSingleton());
AssemblyRegistration.Container.Register<ICache, NullCache>();
AssemblyRegistration.Container.Register<IFileSystem, SimpleFileSystem>();
JsEngineSwitcher.Instance.EngineFactories.Add(new ChakraCoreJsEngineFactory());
JsEngineSwitcher.Instance.DefaultEngineName = ChakraCoreJsEngine.EngineName;

ReactSiteConfiguration.Configuration
.SetReuseJavaScriptEngines(false)
.AddScript("Sample.jsx");
}

[Benchmark]
public void HtmlHelperExtensions_React()
{
AssertContains("Hello Tester!", HtmlHelperExtensions.React(null, "HelloWorld", _testData, serverOnly: true).ToHtmlString());
}

[Benchmark]
public void Environment_CreateComponent()
{
var component = ReactEnvironment.Current.CreateComponent("HelloWorld", _testData, serverOnly: true);
AssertContains("Hello Tester!", component.RenderHtml(renderServerOnly: true));
ReactEnvironment.Current.ReturnEngineToPool();
}

private static JObject _testData = JObject.FromObject(new System.Collections.Generic.Dictionary<string, string>(){ ["name"] = "Tester" });
}

private static void AssertContains(string expected, string actual)
{
if (!actual.Contains(expected))
{
throw new InvalidOperationException($"Strings were not equal. {expected} {actual}");
}
}
}
}
29 changes: 29 additions & 0 deletions tests/React.Tests.Benchmarks/React.Tests.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="benchmarkdotnet" Version="0.10.14" />
<PackageReference Include="JavaScriptEngineSwitcher.ChakraCore" Version="2.4.15" />
<PackageReference Include="JavaScriptEngineSwitcher.ChakraCore.Native.win-x64" Version="2.4.6" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\React.Core\React.Core.csproj" />
<ProjectReference Include="..\..\src\React.Web.Mvc4\React.Web.Mvc4.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Web" />
</ItemGroup>

<ItemGroup>
<None Update="Sample.jsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions tests/React.Tests.Benchmarks/Sample.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class HelloWorld extends React.Component {
render() {
return (
<div>
Hello {this.props.name}!
All props: {JSON.stringify(this.props)}
</div>
);
}
}