Skip to content

Commit

Permalink
Merge pull request #128 from b3b00/technical/benchmarking
Browse files Browse the repository at this point in the history
Technical/benchmarking
  • Loading branch information
b3b00 authored Jun 14, 2019
2 parents 2b2d929 + f4939c6 commit ee9f058
Show file tree
Hide file tree
Showing 32 changed files with 2,510 additions and 13 deletions.
3 changes: 2 additions & 1 deletion bench/Bench.cs → bench/GenericLexerBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.IO;
using System.Linq;
using System.Collections.Generic;
using bench.json;
using BenchmarkDotNet.Analysers;
using BenchmarkDotNet.Toolchains.InProcess;

Expand All @@ -23,7 +24,7 @@ namespace bench
[MemoryDiagnoser]

[Config(typeof(Config))]
public class Bench
public class GenericLexerBench
{


Expand Down
91 changes: 91 additions & 0 deletions bench/JsonParserBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;

using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Toolchains.CsProj;

using System.IO;
using BenchmarkDotNet.Analysers;


using sly.parser;
using sly.parser.generator;
using bench.json;
using bench.json.model;



namespace bench
{

[MemoryDiagnoser]

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


private class Config : ManualConfig
{
public Config()
{
var baseJob = Job.MediumRun.With(CsProjCoreToolchain.Current.Value);
Add(baseJob.WithNuGet("sly", "2.2.5.1").WithId("2.2.5.1"));
Add(baseJob.WithNuGet("sly", "2.2.5.2").WithId("2.2.5.2"));
Add(EnvironmentAnalyser.Default);
Add(baseJob.WithNuGet("sly", "2.2.5.3").WithId("2.2.5.3"));
Add(baseJob.WithNuGet("sly", "2.3.0.1").WithId("2.3.0.1"));
}
}

private Parser<JsonTokenGeneric,JSon> BenchedParser;

private string content = "";

[GlobalSetup]
public void Setup()
{
Console.WriteLine(("SETUP"));
Console.ReadLine();
content = File.ReadAllText("test.json");
Console.WriteLine("json read.");
var jsonParser = new EbnfJsonGenericParser();
var builder = new ParserBuilder<JsonTokenGeneric, JSon>();

var result = builder.BuildParser(jsonParser, ParserType.EBNF_LL_RECURSIVE_DESCENT, "root");
Console.WriteLine("parser built.");
if (result.IsError)
{
Console.WriteLine("ERROR");
result.Errors.ForEach(e => Console.WriteLine(e));
}
else
{
Console.WriteLine("parser ok");
BenchedParser = result.Result;
}

Console.WriteLine($"parser {BenchedParser}");
}

[Benchmark]

public void TestJson()
{
if (BenchedParser == null)
{
Console.WriteLine("parser is null");
}
else
{
var ignored = BenchedParser.Parse(content);
}
}



}

}
17 changes: 13 additions & 4 deletions bench/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Diagnostics;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using sly;
using sly.lexer;
Expand All @@ -13,15 +14,23 @@ namespace bench
class Program
{

private static void BenchJSON() {
private static void BenchJson() {

var summary = BenchmarkRunner.Run<Bench>();
var summary = BenchmarkRunner.Run<JsonParserBench>();

}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
BenchJSON();
try
{
Console.WriteLine("Hello World!");
BenchJson();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
}
}
10 changes: 10 additions & 0 deletions bench/bench.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
<PackageReference Include="sly" Version="2.2.5.1" />
<PackageReference Include="System.Memory" Version="4.5.3" />
</ItemGroup>

<ItemGroup>
<None Remove="test.json" />
<Content Include="test.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="BenchmarkDotNet.Artifacts\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="BenchmarkDotNet.Artifacts\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="BenchmarkDotNet.Artifacts\**" />
</ItemGroup>

</Project>
176 changes: 176 additions & 0 deletions bench/json/EBNFJSONParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using System;
using System.Collections.Generic;
using System.Linq;
using bench.json.model;
using sly.lexer;
using sly.parser.generator;
using sly.parser.parser;

namespace bench.json
{
public class EbnfJsonParser
{
#region root

[Production("root : value")]
public JSon Root(JSon value)
{
return value;
}

#endregion

#region VALUE

[Production("value : STRING")]
public JSon StringValue(Token<JsonToken> stringToken)
{
return new JValue(stringToken.StringWithoutQuotes);
}

[Production("value : INT")]
public object IntValue(Token<JsonToken> intToken)
{
return new JValue(intToken.IntValue);
}

[Production("value : DOUBLE")]
public object DoubleValue(Token<JsonToken> doubleToken)
{
var dbl = double.MinValue;
try
{
var doubleParts = doubleToken.Value.Split('.');
dbl = double.Parse(doubleParts[0]);
if (doubleParts.Length > 1)
{
var decimalPart = double.Parse(doubleParts[1]);
for (var i = 0; i < doubleParts[1].Length; i++) decimalPart = decimalPart / 10.0;
dbl += decimalPart;
}
}
catch (Exception)
{
dbl = double.MinValue;
}

return new JValue(dbl);
}

[Production("value : BOOLEAN")]
public object BooleanValue(Token<JsonToken> boolToken)
{
return new JValue(bool.Parse(boolToken.Value));
}

[Production("value : NULL")]
public object NullValue(object forget)
{
return new JNull();
}

[Production("value : object")]
public JSon ObjectValue(JSon value)
{
return value;
}

[Production("value: list")]
public JSon ListValue(JList list)
{
return list;
}

#endregion

#region OBJECT

[Production("object: ACCG ACCD")]
public JSon EmptyObjectValue(object accg, object accd)
{
return new JObject();
}

[Production("object: ACCG members ACCD")]
public JSon AttributesObjectValue(object accg, JObject members, object accd)
{
return members;
}

#endregion

#region LIST

[Production("list: CROG CROD")]
public JSon EmptyList(object crog, object crod)
{
return new JList();
}

[Production("list: CROG listElements CROD")]
public JSon List(object crog, JList elements, object crod)
{
return elements;
}


[Production("listElements: value (COMMA [d] value)*")]
public JSon listElements(JSon head, List<Group<JsonToken, JSon>> tail)
{
var values = new JList(head);
values.AddRange(tail.Select(group => group.Value(0)).ToList());
return values;
}

[Production("additionalValue: COMMA value")]
public JSon ListElementsOne(Token<JsonToken> discardedComma, JSon value)
{
return value;
}

#endregion

#region PROPERTIES

[Production("members: property additionalProperty*")]
public object Members(JObject head, List<JSon> tail)
{
var value = new JObject();
value.Merge(head);
foreach (var p in tail) value.Merge((JObject) p);
return value;
}

[Production("additionalProperty : COMMA property")]
public object property(Token<JsonToken> comma, JObject property)
{
return property;
}

[Production("property: STRING COLON value")]
public object property(Token<JsonToken> key, object colon, JSon value)
{
return new JObject(key.StringWithoutQuotes, value);
}


//[Production("members : property COMMA members")]
//public object ManyMembers(KeyValuePair<string, object> pair, object comma, Dictionary<string, object> tail)
//{
// Dictionary<string, object> members = new Dictionary<string, object>();
// members[pair.Key] = pair.Value;
// members.Merge(tail);
// return members;
//}

//[Production("members : property")]
//public object SingleMember(KeyValuePair<string, object> pair)
//{
// Dictionary<string, object> members = new Dictionary<string, object>();
// members.Add(pair.Key, pair.Value);
// return members;
//}

#endregion
}
}
Loading

0 comments on commit ee9f058

Please sign in to comment.