-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from b3b00/technical/benchmarking
Technical/benchmarking
- Loading branch information
Showing
32 changed files
with
2,510 additions
and
13 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
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,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); | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
} |
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
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 | ||
} | ||
} |
Oops, something went wrong.