Skip to content

Commit

Permalink
Merge pull request #475 from b3b00/technical/memory-profiling
Browse files Browse the repository at this point in the history
Technical/memory profiling
  • Loading branch information
b3b00 authored Aug 23, 2024
2 parents fbd97a0 + 244d34b commit 4247d46
Show file tree
Hide file tree
Showing 26 changed files with 464 additions and 64 deletions.
10 changes: 6 additions & 4 deletions src/benchCurrent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ static class Program

private static void BenchJson() {

var summary = BenchmarkRunner.Run<JsonParserBench>();
// var summary = BenchmarkRunner.Run<JsonParserBench>();
//
// var summary2 = BenchmarkRunner.Run<BackTrackBench>();

var summary2 = BenchmarkRunner.Run<BackTrackBench>();
var summary3 = BenchmarkRunner.Run<WhileBench>();
// var summary3 = BenchmarkRunner.Run<WhileBench>();

var summary4 = BenchmarkRunner.Run<SimpleExpressionBench>();

}
static void Main(string[] args)
Expand Down
114 changes: 114 additions & 0 deletions src/benchCurrent/SimpleExpressionBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Toolchains.CsProj;
using expressionparser;
using simpleExpressionParser;
using sly.parser;
using sly.parser.generator;
using ExpressionToken = simpleExpressionParser.ExpressionToken;

namespace benchCurrent
{

[MemoryDiagnoser]

[Config(typeof(Config))]
public class SimpleExpressionBench
{


private class Config : ManualConfig
{
public Config()
{
var baseJob = Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp70);
}
}

private Parser<ExpressionToken, double> BenchedParser;
private Parser<GenericExpressionToken, double> BenchedGenericParser;

private string content = "";

[GlobalSetup]
public void Setup()
{
Console.WriteLine(("SETUP"));
content = "1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20";

SimpleExpressionParser p = new SimpleExpressionParser();
var builder = new ParserBuilder<ExpressionToken, double>();

var result = builder.BuildParser(p, ParserType.EBNF_LL_RECURSIVE_DESCENT, "root");

if (result.IsError)
{
result.Errors.ForEach(x => Console.WriteLine(x.Message));
Environment.Exit(1);
}
else
{
Console.WriteLine("parser ok");
BenchedParser = result.Result;
}

GenericSimpleExpressionParser gp = new GenericSimpleExpressionParser();
var genericbuilder = new ParserBuilder<GenericExpressionToken, double>();

var genericresult = genericbuilder.BuildParser(gp, ParserType.EBNF_LL_RECURSIVE_DESCENT, "root");

if (genericresult.IsError)
{
genericresult.Errors.ForEach(x => Console.WriteLine(x.Message));
Environment.Exit(2);
}
else
{
Console.WriteLine("parser ok");
BenchedGenericParser = genericresult.Result;
}
}



[Benchmark]

public void TestExpressionRegex()
{
if (BenchedParser == null)
{
Console.WriteLine("regex parser is null");
}
else
{
for (int i = 0; i < 50; i++)
{
var ignored = BenchedParser.Parse(content);
}
}
}

// [Benchmark]

public void TestExpressionGeneric()
{
if (BenchedGenericParser == null)
{
Console.WriteLine("generic parser is null");
}
else
{
for (int i = 0; i < 50; i++)
{
var ignored = BenchedGenericParser.Parse(content);
}
}
}



}

}
2 changes: 2 additions & 0 deletions src/benchCurrent/benchCurrent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\samples\expressionParser\expressionParser.csproj" />
<ProjectReference Include="..\samples\IndentedWhile\indentedWhile.csproj" />
<ProjectReference Include="..\samples\SimpleExpressionParser\SimpleExpressionParser.csproj" />
<ProjectReference Include="..\sly\sly.csproj" />
</ItemGroup>

Expand Down
Binary file added src/match.dmw
Binary file not shown.
24 changes: 23 additions & 1 deletion src/samples/ParserExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using XML;
using Xunit;
using ExpressionContext = postProcessedLexerParser.expressionModel.ExpressionContext;
using ExpressionToken = simpleExpressionParser.ExpressionToken;
using IfThenElse = indented.IfThenElse;

namespace ParserExample
Expand Down Expand Up @@ -131,6 +132,26 @@ public static object Rec(List<object> args)
}


private static void BenchSimpleExpression()
{
GenericSimpleExpressionParser p = new GenericSimpleExpressionParser();
var builder = new ParserBuilder<GenericExpressionToken, double>();

var Parser = builder.BuildParser(p, ParserType.EBNF_LL_RECURSIVE_DESCENT, "root");
if (Parser.IsOk)
{
for (int i = 0; i < 50; i++)
{
var r = Parser.Result.Parse("1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20");
if (r.IsOk)
{
Console.WriteLine(r.Result);
}
}

}
}

private static void TestFactorial()
{
var whileParser = new WhileParserGeneric();
Expand Down Expand Up @@ -1183,7 +1204,8 @@ print a
}
private static void Main(string[] args)
{
IndentRefactoring();
BenchSimpleExpression();
// IndentRefactoring();
//NodeNames();
// BroadWindow();
// return;
Expand Down
58 changes: 58 additions & 0 deletions src/samples/SimpleExpressionParser/ExpressionToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using sly.i18n;
using sly.lexer;

namespace simpleExpressionParser
{
public enum ExpressionToken
{
// float number
[Lexeme("[0-9]+\\.[0-9]+")] DOUBLE = 1,

// integer
[Lexeme("[0-9]+")] INT = 3,

[Lexeme("[a-zA-Z]+")] IDENTIFIER = 4,

// the + operator
[LexemeLabel("en","plus sign")]
[LexemeLabel("fr","plus")]
[Lexeme("\\+")] PLUS = 5,

// the - operator
[LexemeLabel("en","minus sign")]
[LexemeLabel("fr","moins")]
[Lexeme("\\-")] MINUS = 6,

// the * operator
[LexemeLabel("en","times sign")]
[LexemeLabel("fr","multiplication")]
[Lexeme("\\*")] TIMES = 7,

// the / operator
[LexemeLabel("en","divide sign")]
[LexemeLabel("fr","division")]
[Lexeme("\\/")] DIVIDE = 8,

// a left paranthesis (
[LexemeLabel("en","opening parenthesis")]
[LexemeLabel("fr","parenthèse ouvrante")]
[Lexeme("\\(")] LPAREN = 9,

// a right paranthesis )
[LexemeLabel("en","closing parenthesis")]
[LexemeLabel("fr","parenthèse fermante")]
[Lexeme("\\)")] RPAREN = 10,

[LexemeLabel("fr","point d'exclamation")]
[LexemeLabel("en","exclamation point")]
[Lexeme("!")] FACTORIAL = 13,


// a whitespace
[Lexeme("[ \\t]+", true)] WS = 11,

[Lexeme("[\\n\\r]+", true, true)] EOL = 12


}
}
44 changes: 44 additions & 0 deletions src/samples/SimpleExpressionParser/GenericExpressionToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using sly.i18n;
using sly.lexer;

namespace simpleExpressionParser;

public enum GenericExpressionToken
{
// float number
[Double] DOUBLE = 1,

// integer
[Int] INT = 3,

[AlphaNumId] IDENTIFIER = 4,

// the + operator
[LexemeLabel("en", "plus sign")] [LexemeLabel("fr", "plus")]
[Sugar("+")]
PLUS = 5,

// the - operator
[LexemeLabel("en", "minus sign")] [LexemeLabel("fr", "moins")] [Sugar("-")]
MINUS = 6,

// the * operator
[LexemeLabel("en", "times sign")] [LexemeLabel("fr", "multiplication")] [Sugar("*")]
TIMES = 7,

// the / operator
[LexemeLabel("en", "divide sign")] [LexemeLabel("fr", "division")] [Sugar("/")]
DIVIDE = 8,

// a left paranthesis (
[LexemeLabel("en", "opening parenthesis")] [LexemeLabel("fr", "parenthèse ouvrante")] [Sugar("(")]
LPAREN = 9,

// a right paranthesis )
[LexemeLabel("en", "closing parenthesis")] [LexemeLabel("fr", "parenthèse fermante")] [Sugar(")")]
RPAREN = 10,

[LexemeLabel("fr", "point d'exclamation")] [LexemeLabel("en", "exclamation point")] [Sugar("!")]
FACTORIAL = 13,

}
Loading

0 comments on commit 4247d46

Please sign in to comment.