-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename, created project * Basic framework works * Create 99bottles.draco * Update LexerBenchmarks.cs * Added parser tests * Basic e2e benchmarking * Namespace sync * Cleanup * PR comment * Update SyntaxBenchmarks.cs
- Loading branch information
1 parent
a420137
commit 92ae046
Showing
28 changed files
with
299 additions
and
23 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/Draco.Compiler.Benchmarks/Draco.Compiler.Benchmarks.csproj
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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<!-- Copy benchmark inputs --> | ||
<ItemGroup> | ||
<Content Include="benchmarks\**"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Draco.Compiler\Draco.Compiler.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.8" /> | ||
<PackageReference Include="Basic.Reference.Assemblies.Net70" Version="1.4.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,36 @@ | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using Draco.Compiler.Api; | ||
using Draco.Compiler.Api.Syntax; | ||
|
||
namespace Draco.Compiler.Benchmarks; | ||
|
||
public class E2eBenchmarks : FolderBenchmarkBase | ||
{ | ||
private MemoryStream peStream = null!; | ||
|
||
public E2eBenchmarks() | ||
: base("e2e") | ||
{ | ||
} | ||
|
||
[IterationSetup] | ||
public void Setup() | ||
{ | ||
this.peStream = new(); | ||
} | ||
|
||
[Benchmark] | ||
public EmitResult Compile() | ||
{ | ||
var syntaxTree = SyntaxTree.Parse(this.Input.Code, Path.GetFullPath(this.Input.Path)); | ||
var compilation = Compilation.Create( | ||
syntaxTrees: ImmutableArray.Create(syntaxTree), | ||
metadataReferences: Basic.Reference.Assemblies.Net70.ReferenceInfos.All | ||
.Select(r => MetadataReference.FromPeStream(new MemoryStream(r.ImageBytes))) | ||
.ToImmutableArray()); | ||
return compilation.Emit(peStream: this.peStream); | ||
} | ||
} |
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,23 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Draco.Compiler.Benchmarks; | ||
|
||
public abstract class FolderBenchmarkBase | ||
{ | ||
[ParamsSource(nameof(GetSourcesFromFolder))] | ||
public SourceCodeParameter Input { get; set; } = null!; | ||
|
||
private readonly string path; | ||
|
||
protected FolderBenchmarkBase(string path) | ||
{ | ||
this.path = Path.Join("benchmarks", path); | ||
} | ||
|
||
public IEnumerable<SourceCodeParameter> GetSourcesFromFolder() => Directory | ||
.GetFiles(this.path) | ||
.Select(SourceCodeParameter.FromPath); | ||
} |
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,9 @@ | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace Draco.Compiler.Benchmarks; | ||
|
||
internal class Program | ||
{ | ||
internal static void Main(string[] args) => | ||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); | ||
} |
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,10 @@ | ||
using System.IO; | ||
|
||
namespace Draco.Compiler.Benchmarks; | ||
|
||
public sealed record class SourceCodeParameter(string Path, string Code) | ||
{ | ||
public static SourceCodeParameter FromPath(string path) => new(path, File.ReadAllText(path)); | ||
|
||
public override string ToString() => System.IO.Path.GetFileName(this.Path); | ||
} |
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,96 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using BenchmarkDotNet.Attributes; | ||
using Draco.Compiler.Api.Syntax; | ||
using Draco.Compiler.Internal.Syntax; | ||
using CompilationUnitSyntax = Draco.Compiler.Internal.Syntax.CompilationUnitSyntax; | ||
using SyntaxToken = Draco.Compiler.Internal.Syntax.SyntaxToken; | ||
|
||
namespace Draco.Compiler.Benchmarks; | ||
|
||
public class SyntaxBenchmarks : FolderBenchmarkBase | ||
{ | ||
private SyntaxToken[] tokens = null!; | ||
|
||
private Lexer lexer = null!; | ||
private Parser parser = null!; | ||
|
||
public SyntaxBenchmarks() | ||
: base("syntax") | ||
{ | ||
} | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup() | ||
{ | ||
this.tokens = LexFromBenchmarkParameter(this.Input); | ||
} | ||
|
||
[IterationSetup(Target = nameof(Lex))] | ||
public void LexSetup() | ||
{ | ||
var sourceReader = SourceReader.From(this.Input.Code); | ||
var syntaxDiagnostics = new SyntaxDiagnosticTable(); | ||
this.lexer = new Lexer(sourceReader, syntaxDiagnostics); | ||
} | ||
|
||
[IterationSetup(Target = nameof(Parse))] | ||
public void ParseSetup() | ||
{ | ||
var tokenSource = TokenSource.From(this.tokens.AsMemory()); | ||
var syntaxDiagnostics = new SyntaxDiagnosticTable(); | ||
this.parser = new Parser(tokenSource, syntaxDiagnostics); | ||
} | ||
|
||
[IterationSetup(Target = nameof(ParseWithStreamingLexer))] | ||
public void ParseWithStreamingLexerSetup() | ||
{ | ||
var syntaxDiagnostics = new SyntaxDiagnosticTable(); | ||
|
||
var sourceReader = SourceReader.From(this.Input.Code); | ||
this.lexer = new Lexer(sourceReader, syntaxDiagnostics); | ||
|
||
var tokenSource = TokenSource.From(this.lexer); | ||
this.parser = new Parser(tokenSource, syntaxDiagnostics); | ||
} | ||
|
||
[Benchmark] | ||
public int Lex() | ||
{ | ||
var count = 0; | ||
while (true) | ||
{ | ||
var token = this.lexer.Lex(); | ||
++count; | ||
if (token.Kind == TokenKind.EndOfInput) break; | ||
} | ||
return count; | ||
} | ||
|
||
[Benchmark] | ||
public object Parse() => | ||
this.parser.ParseCompilationUnit(); | ||
|
||
[Benchmark] | ||
public object ParseWithStreamingLexer() => | ||
this.parser.ParseCompilationUnit(); | ||
|
||
private static SyntaxToken[] LexFromBenchmarkParameter(SourceCodeParameter parameter) | ||
{ | ||
var sourceReader = SourceReader.From(parameter.Code); | ||
var syntaxDiagnostics = new SyntaxDiagnosticTable(); | ||
var lexer = new Lexer(sourceReader, syntaxDiagnostics); | ||
|
||
var result = new List<SyntaxToken>(); | ||
|
||
while (true) | ||
{ | ||
var token = lexer.Lex(); | ||
result.Add(token); | ||
|
||
if (token.Kind == TokenKind.EndOfInput) break; | ||
} | ||
|
||
return result.ToArray(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Draco.Compiler.Benchmarks/benchmarks/e2e/99bottles.draco
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,29 @@ | ||
import System.Console; | ||
|
||
func bottles(n: int32): string = | ||
if (n == 0) "no more bottles" | ||
else if (n == 1) "1 bottle" | ||
else "\{n} bottles"; | ||
|
||
func capitalize(s: string): string = | ||
"\{s.Substring(0, 1).ToUpper()}\{s.Substring(1)}"; | ||
|
||
func main() { | ||
var i = 99; | ||
while (i > 0) { | ||
WriteLine(""" | ||
\{capitalize(bottles(i))} of beer on the wall, | ||
\{bottles(i)} of beer. | ||
Take one down, pass it around, | ||
\{bottles(i - 1)} of beer on the wall. | ||
|
||
"""); | ||
i -= 1; | ||
} | ||
WriteLine(""" | ||
No more bottles of beer on the wall, | ||
no more bottles of beer. | ||
Go to the store, buy some more, | ||
99 bottles of beer on the wall. | ||
"""); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Draco.Compiler.Benchmarks/benchmarks/e2e/helloworld.draco
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,5 @@ | ||
import System.Console; | ||
|
||
public func main() { | ||
WriteLine("Hello, World!"); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Draco.Compiler.Benchmarks/benchmarks/syntax/99bottles.draco
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,29 @@ | ||
import System.Console; | ||
|
||
func bottles(n: int32): string = | ||
if (n == 0) "no more bottles" | ||
else if (n == 1) "1 bottle" | ||
else "\{n} bottles"; | ||
|
||
func capitalize(s: string): string = | ||
"\{s.Substring(0, 1).ToUpper()}\{s.Substring(1)}"; | ||
|
||
func main() { | ||
var i = 99; | ||
while (i > 0) { | ||
WriteLine(""" | ||
\{capitalize(bottles(i))} of beer on the wall, | ||
\{bottles(i)} of beer. | ||
Take one down, pass it around, | ||
\{bottles(i - 1)} of beer on the wall. | ||
|
||
"""); | ||
i -= 1; | ||
} | ||
WriteLine(""" | ||
No more bottles of beer on the wall, | ||
no more bottles of beer. | ||
Go to the store, buy some more, | ||
99 bottles of beer on the wall. | ||
"""); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Draco.Compiler.Benchmarks/benchmarks/syntax/helloworld.draco
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,5 @@ | ||
import System.Console; | ||
|
||
public func main() { | ||
WriteLine("Hello, World!"); | ||
} |
4 changes: 2 additions & 2 deletions
4
....Fuzzer/Components/ComponentFuzzerBase.cs → ....Fuzzer/Components/ComponentFuzzerBase.cs
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
2 changes: 1 addition & 1 deletion
2
...aco.Fuzzer/Components/IComponentFuzzer.cs → ...ler.Fuzzer/Components/IComponentFuzzer.cs
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
4 changes: 2 additions & 2 deletions
4
src/Draco.Fuzzer/Components/LexerFuzzer.cs → ...Compiler.Fuzzer/Components/LexerFuzzer.cs
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
4 changes: 2 additions & 2 deletions
4
src/Draco.Fuzzer/Components/ParserFuzzer.cs → ...ompiler.Fuzzer/Components/ParserFuzzer.cs
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
2 changes: 1 addition & 1 deletion
2
src/Draco.Fuzzer/CrashException.cs → src/Draco.Compiler.Fuzzer/CrashException.cs
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
src/Draco.Fuzzer/Generators/Charsets.cs → ...co.Compiler.Fuzzer/Generators/Charsets.cs
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
2 changes: 1 addition & 1 deletion
2
src/Draco.Fuzzer/Generators/Generator.cs → ...o.Compiler.Fuzzer/Generators/Generator.cs
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
2 changes: 1 addition & 1 deletion
2
src/Draco.Fuzzer/Generators/IGenerator.cs → ....Compiler.Fuzzer/Generators/IGenerator.cs
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
2 changes: 1 addition & 1 deletion
2
.../Generators/SequenceGenerationSettings.cs → .../Generators/SequenceGenerationSettings.cs
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
2 changes: 1 addition & 1 deletion
2
...co.Fuzzer/Generators/SequenceGenerator.cs → ...er.Fuzzer/Generators/SequenceGenerator.cs
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
2 changes: 1 addition & 1 deletion
2
...raco.Fuzzer/Generators/TriviaGenerator.cs → ...iler.Fuzzer/Generators/TriviaGenerator.cs
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
2 changes: 1 addition & 1 deletion
2
src/Draco.Fuzzer/MutationException.cs → ...raco.Compiler.Fuzzer/MutationException.cs
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
Oops, something went wrong.