Skip to content

Commit

Permalink
#228 - Introduce benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Jul 27, 2019
1 parent 65b0bb1 commit 0f6329e
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ TestResults
packages
.idea/
.vs/
BenchmarkDotNet.Artifacts/

desktop.ini
.DS_Store

Expand Down
33 changes: 33 additions & 0 deletions xFunc.Benchmark/Benchmarks/LexerBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using xFunc.Maths.Tokenization;
using xFunc.Maths.Tokenization.Tokens;

namespace xFunc.Benchmark.Benchmarks
{

public class LexerBenchmark
{

private ILexer lexer;

[Params(10, 100, 1000)]
public int Iterations;

[GlobalSetup]
public void Setup()
{
lexer = new Lexer();
}

[Benchmark]
public void TestLexer()
{
IEnumerable<IToken> tokens = null;
for (var i = 0; i < Iterations; i++)
tokens = lexer.Tokenize("(100.1 + (sin(cos(tan(ctg(x)))) * 3) / (func(a, b, c) ^ 2)) - (cos(y) - 111.3) & (true | false -> true <-> false) + (det({{1, 2}, {3, 4}}) * log(2, 3))");
}

}

}
119 changes: 119 additions & 0 deletions xFunc.Benchmark/Benchmarks/ParserBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using BenchmarkDotNet.Attributes;
using System.Collections.Generic;
using xFunc.Maths;
using xFunc.Maths.Expressions;
using xFunc.Maths.Tokenization;
using xFunc.Maths.Tokenization.Tokens;

namespace xFunc.Benchmark.Benchmarks
{
public class ParserBenchmark
{
private Parser processor;

private IList<IToken> tokens;

[Params(10, 100, 1000)]
public int Iterations;

[GlobalSetup]
public void Setup()
{
processor = new Parser();

// (100.1 + (sin(cos(tan(ctg(x)))) * 3) / (func(a, b, c) ^ 2)) - (cos(y) - 111.3) & (true | false -> true <-> false) + (det({{1, 2}, {3, 4}}) * log(2, 3))
tokens = new TokensBuilder()
.OpenBracket()
.Number(100.1)
.Operation(Operations.Addition)
.OpenBracket()
.Function(Functions.Sine, 1)
.OpenBracket()
.Function(Functions.Cosine, 1)
.OpenBracket()
.Function(Functions.Tangent, 1)
.OpenBracket()
.Function(Functions.Cotangent, 1)
.OpenBracket()
.VariableX()
.CloseBracket()
.CloseBracket()
.CloseBracket()
.CloseBracket()
.Operation(Operations.Multiplication)
.Number(3)
.CloseBracket()
.Operation(Operations.Division)
.OpenBracket()
.UserFunction("func", 3)
.OpenBracket()
.Variable("a")
.Comma()
.Variable("b")
.Comma()
.Variable("c")
.CloseBracket()
.Operation(Operations.Exponentiation)
.Number(2)
.CloseBracket()
.CloseBracket()
.Operation(Operations.Subtraction)
.OpenBracket()
.Function(Functions.Cosine, 1)
.OpenBracket()
.VariableY()
.CloseBracket()
.Operation(Operations.Subtraction)
.Number(111.3)
.CloseBracket()
.Operation(Operations.And)
.OpenBracket()
.True()
.Operation(Operations.Or)
.False()
.Operation(Operations.Implication)
.True()
.Operation(Operations.Equality)
.False()
.CloseBracket()
.Operation(Operations.Addition)
.OpenBracket()
.Function(Functions.Determinant, 1)
.OpenBracket()
.Function(Functions.Matrix, 2)
.OpenBrace()
.Function(Functions.Vector, 2)
.OpenBrace()
.Number(1)
.Comma()
.Number(2)
.CloseBrace()
.Comma()
.Function(Functions.Vector, 2)
.OpenBrace()
.Number(3)
.Comma()
.Number(4)
.CloseBrace()
.CloseBrace()
.CloseBracket()
.Operation(Operations.Multiplication)
.Function(Functions.Log, 2)
.OpenBracket()
.Number(2)
.Comma()
.Number(3)
.CloseBracket()
.CloseBracket()
.Tokens;
}

[Benchmark]
public void Parse()
{
IExpression exp = null;
for (var i = 0; i < Iterations; i++)
exp = processor.Parse(tokens);
}
}
}
28 changes: 28 additions & 0 deletions xFunc.Benchmark/Benchmarks/ProcessorBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using BenchmarkDotNet.Attributes;
using xFunc.Maths;
using xFunc.Maths.Expressions;

namespace xFunc.Benchmark.Benchmarks
{
public class ProcessorBenchmark
{
private Processor processor;

[Params(10, 100, 1000)]
public int Iterations;

[GlobalSetup]
public void Setup()
{
processor = new Processor();
}

[Benchmark]
public void Parse()
{
IExpression exp = null;
for (var i = 0; i < Iterations; i++)
exp = processor.Parse("(100.1 + (sin(cos(tan(ctg(x)))) * 3) / (func(a, b, c) ^ 2)) - (cos(y) - 111.3) & (true | false -> true <-> false) + (det({{1, 2}, {3, 4}}) * log(2, 3))");
}
}
}
24 changes: 24 additions & 0 deletions xFunc.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.CsProj;

namespace xFunc.Benchmark
{
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner
.Run(typeof(Program).Assembly,
ManualConfig.Create(DefaultConfig.Instance)
.With(Job.MediumRun
.WithLaunchCount(1)
.With(CsProjCoreToolchain.NetCoreApp22))
.With(MemoryDiagnoser.Default)
.StopOnFirstError()
);
}
}
}
16 changes: 16 additions & 0 deletions xFunc.Benchmark/xFunc.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\xFunc.Maths\xFunc.Maths.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions xFunc.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xFunc.Tests", "xFunc.Tests\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xFunc.DotnetTool", "xFunc.DotnetTool\xFunc.DotnetTool.csproj", "{907CDF76-60C1-4894-9CB3-496FFFF92E13}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "xFunc.Benchmark", "xFunc.Benchmark\xFunc.Benchmark.csproj", "{EC5FB562-9BFE-4BD9-A3E4-666D3021AF1F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,6 +35,10 @@ Global
{907CDF76-60C1-4894-9CB3-496FFFF92E13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{907CDF76-60C1-4894-9CB3-496FFFF92E13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{907CDF76-60C1-4894-9CB3-496FFFF92E13}.Release|Any CPU.Build.0 = Release|Any CPU
{EC5FB562-9BFE-4BD9-A3E4-666D3021AF1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC5FB562-9BFE-4BD9-A3E4-666D3021AF1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC5FB562-9BFE-4BD9-A3E4-666D3021AF1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC5FB562-9BFE-4BD9-A3E4-666D3021AF1F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 0f6329e

Please sign in to comment.