Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce allocations and make things go faster (#13)
- Loading branch information
1 parent
2541d46
commit 1c5d809
Showing
11 changed files
with
925 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,6 @@ nuget.exe | |
project.lock.json | ||
.vs | ||
.build/ | ||
.testPublish/ | ||
.testPublish/ | ||
.idea | ||
BenchmarkDotNet.Artifacts* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<AssemblyName>Esprima.Benchmark</AssemblyName> | ||
<OutputType>Exe</OutputType> | ||
<PackageId>Esprima.Benchmark</PackageId> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\test\Esprima.Tests\Fixtures\3rdparty\**" CopyToOutputDirectory="PreserveNewest" LinkBase="3rdparty" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Esprima\Esprima.csproj" /> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.10.11" /> | ||
</ItemGroup> | ||
|
||
</Project> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Esprima.Benchmark | ||
{ | ||
[MemoryDiagnoser] | ||
public class FileParsingBenchmark | ||
{ | ||
private static readonly Dictionary<string, string> files = new Dictionary<string, string> | ||
{ | ||
{"underscore-1.5.2", null}, | ||
{"backbone-1.1.0", null}, | ||
{"mootools-1.4.5", null}, | ||
{"jquery-1.9.1", null}, | ||
{"yui-3.12.0", null}, | ||
{"jquery.mobile-1.4.2", null}, | ||
{"angular-1.2.5", null} | ||
}; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
foreach (var fileName in files.Keys.ToList()) | ||
{ | ||
files[fileName] = File.ReadAllText($"3rdparty/{fileName}.js"); | ||
} | ||
} | ||
|
||
[ParamsSource(nameof(FileNames))] | ||
public string FileName { get; set; } | ||
|
||
public IEnumerable<string> FileNames() | ||
{ | ||
foreach (var entry in files) | ||
{ | ||
yield return entry.Key; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void ParseProgram() | ||
{ | ||
var parser = new JavaScriptParser(files[FileName]); | ||
parser.ParseProgram(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using BenchmarkDotNet.Running; | ||
|
||
namespace Esprima.Benchmark | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var scripts =new [] { | ||
"underscore-1.5.2", | ||
"backbone-1.1.0", | ||
"mootools-1.4.5", | ||
"jquery-1.9.1", | ||
"yui-3.12.0", | ||
"jquery.mobile-1.4.2", | ||
"angular-1.2.5" | ||
}; | ||
|
||
foreach(var script in scripts) | ||
{ | ||
Bench(script); | ||
} | ||
} | ||
|
||
public static void Bench(string script) | ||
{ | ||
var repeat = 120; | ||
var exclude = 10; | ||
|
||
var filename = $"../../test/Esprima.Tests/Fixtures/3rdparty/{script}.js"; | ||
var content = File.ReadAllText(filename); | ||
|
||
var sw = new Stopwatch(); | ||
var results = new List<long>(); | ||
|
||
for(var i=0; i<repeat; i++) | ||
{ | ||
var parser = new JavaScriptParser(content); | ||
sw.Restart(); | ||
|
||
parser.ParseProgram(); | ||
results.Add(sw.ElapsedMilliseconds); | ||
} | ||
|
||
var average = results | ||
.OrderBy(x => x) | ||
.Skip(exclude) | ||
.Take(repeat - exclude * 2) | ||
.Average(); | ||
|
||
Console.WriteLine("{0} ({2}KB): {1} ms", script, Math.Round(average, 1), content.Length / 1024); | ||
BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).Run(args); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.