Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Jint.Benchmark to use BenchmarkDotNet #445

Merged
merged 1 commit into from
Dec 13, 2017
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,6 @@ Jint.sln.ide/*
.vs
project.lock.json
.build

.idea
BenchmarkDotNet.Artifacts
64 changes: 64 additions & 0 deletions Jint.Benchmark/EvaluationBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using BenchmarkDotNet.Attributes;
using Jurassic;

namespace Jint.Benchmark
{
[MemoryDiagnoser]
public class EvaluationBenchmark
{
private Engine sharedJint;
private ScriptEngine sharedJurassic;

private const string Script = @"
var o = {};
o.Foo = 'bar';
o.Baz = 42.0001;
o.Blah = o.Foo + o.Baz;

if(o.Blah != 'bar42.0001') throw TypeError;

function fib(n){
if(n<2) {
return n;
}

return fib(n-1) + fib(n-2);
}

if(fib(3) != 2) throw TypeError;
";

[GlobalSetup]
public void Setup()
{
sharedJint = new Engine();
sharedJurassic = new ScriptEngine();
}

[Params(10, 20)]
public int Iterations { get; set; }

[Params(true, false)]
public bool ReuseEngine { get; set; }

[Benchmark]
public void Jint()
{
for (var i = 0; i < Iterations; i++)
{
var jint = ReuseEngine ? sharedJint : new Engine();
jint.Execute(Script);
}
}

[Benchmark]
public void Jurassic()
{
for (var i = 0; i < Iterations; i++)
{
var jurassic = ReuseEngine ? sharedJurassic : new ScriptEngine();
jurassic.Execute(Script);
}
}
}
}
6 changes: 2 additions & 4 deletions Jint.Benchmark/Jint.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Jint.Benchmark</AssemblyName>
Expand All @@ -14,10 +13,9 @@
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Jint\Jint.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.10.11" />
<PackageReference Include="Jurassic" Version="3.0.0-alpha2" />
</ItemGroup>

</Project>
</Project>
98 changes: 6 additions & 92 deletions Jint.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,99 +1,13 @@
using System;
using System.Diagnostics;
using System.Reflection;
using BenchmarkDotNet.Running;

namespace Jint.Benchmark
{
class Program
public static class Program
{
private const string Script = @"
var o = {};
o.Foo = 'bar';
o.Baz = 42.0001;
o.Blah = o.Foo + o.Baz;

if(o.Blah != 'bar42.0001') throw TypeError;

function fib(n){
if(n<2) {
return n;
}

return fib(n-1) + fib(n-2);
}

if(fib(3) != 2) throw TypeError;
";

static void Main()
public static void Main(string[] args)
{
//const bool runIronJs = true;
const bool runJint = true;
const bool runJurassic = true;

const int iterations = 1000;
const bool reuseEngine = false;

var watch = new Stopwatch();


//if (runIronJs)
//{
// IronJS.Hosting.CSharp.Context ironjs;
// ironjs = new IronJS.Hosting.CSharp.Context();
// ironjs.Execute(Script);
// watch.Restart();
// for (var i = 0; i < iterations; i++)
// {
// if (!reuseEngine)
// {
// ironjs = new IronJS.Hosting.CSharp.Context();
// }

// ironjs.Execute(Script);
// }

// Console.WriteLine("IronJs: {0} iterations in {1} ms", iterations, watch.ElapsedMilliseconds);
//}

if (runJint)
{
Engine jint;
jint = new Engine();
jint.Execute(Script);

watch.Restart();
for (var i = 0; i < iterations; i++)
{
if (!reuseEngine)
{
jint = new Jint.Engine();
}

jint.Execute(Script);
}

Console.WriteLine("Jint: {0} iterations in {1} ms", iterations, watch.ElapsedMilliseconds);
}

if (runJurassic)
{
Jurassic.ScriptEngine jurassic;
jurassic = new Jurassic.ScriptEngine();
jurassic.Execute(Script);

watch.Restart();
for (var i = 0; i < iterations; i++)
{
if (!reuseEngine)
{
jurassic = new Jurassic.ScriptEngine();
}

jurassic.Execute(Script);
}

Console.WriteLine("Jurassic: {0} iterations in {1} ms", iterations, watch.ElapsedMilliseconds);
}
BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).Run(args);
}
}
}
}