Skip to content

Commit

Permalink
Benchmark Tests (ChilliCream#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jul 10, 2018
1 parent ba7cbc0 commit 1fc2617
Show file tree
Hide file tree
Showing 45 changed files with 1,637 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,5 @@ __pycache__/
lcov.info
coverage.json
.DS_Store

BenchmarkDotNet.Artifacts
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/Benchmark.Tests/bin/Debug/netcoreapp2.1/Benchmark.Tests.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
2 changes: 2 additions & 0 deletions run-benchmarks.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dotnet build src -c release
dotnet "src/Benchmark.Tests/bin/Release/netcoreapp2.1/Benchmark.Tests.dll"
2 changes: 2 additions & 0 deletions run-benchmarks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dotnet build src -c release
sudo dotnet "src/Benchmark.Tests/bin/Release/netcoreapp2.1/Benchmark.Tests.dll"
34 changes: 34 additions & 0 deletions src/Benchmark.Tests/Benchmark.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>HotChocolate.Benchmark.Tests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.13" />
<PackageReference Include="Moq" Version="4.8.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\IntrospectionQuery.graphql" />
<None Remove="Resources\KitchenSinkQuery.graphql" />
<None Remove="Resources\KitchenSinkSchema.graphql" />
<None Remove="Resources\SimpleQuery.graphql" />
<None Remove="Resources\SimpleSchema.graphql" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\IntrospectionQuery.graphql" />
<EmbeddedResource Include="Resources\KitchenSinkQuery.graphql" />
<EmbeddedResource Include="Resources\KitchenSinkSchema.graphql" />
<EmbeddedResource Include="Resources\SimpleQuery.graphql" />
<EmbeddedResource Include="Resources\SimpleSchema.graphql" />
</ItemGroup>

</Project>
145 changes: 145 additions & 0 deletions src/Benchmark.Tests/Execution/CharacterRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System.Collections.Generic;
using System.Linq;

namespace HotChocolate.Benchmark.Tests.Execution
{
public class CharacterRepository
{
private Dictionary<string, ICharacter> _characters;
private Dictionary<string, Starship> _starships;

public CharacterRepository()
{
_characters = CreateCharacters().ToDictionary(t => t.Id);
_starships = CreateStarships().ToDictionary(t => t.Id);
}

public ICharacter GetHero(Episode episode)
{
if (episode == Episode.Empire)
{
return _characters["1000"];
}
return _characters["2001"];
}

public ICharacter GetCharacter(string id)
{
if (_characters.TryGetValue(id, out ICharacter c))
{
return c;
}
return null;
}

public Human GetHuman(string id)
{
if (_characters.TryGetValue(id, out ICharacter c)
&& c is Human h)
{
return h;
}
return null;
}

public Droid GetDroid(string id)
{
if (_characters.TryGetValue(id, out ICharacter c)
&& c is Droid d)
{
return d;
}
return null;
}

public IEnumerable<object> Search(string text)
{
foreach(ICharacter character in _characters.Values
.Where(t => t.Name.Contains(text)))
{
yield return character;
}

foreach(Starship starship in _starships.Values
.Where(t => t.Name.Contains(text)))
{
yield return starship;
}
}

private static IEnumerable<ICharacter> CreateCharacters()
{
yield return new Human
{
Id = "1000",
Name = "Luke Skywalker",
Friends = new[] { "1002", "1003", "2000", "2001" },
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
HomePlanet = "Tatooine"
};

yield return new Human
{
Id = "1001",
Name = "Darth Vader",
Friends = new[] { "1004" },
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
HomePlanet = "Tatooine"
};

yield return new Human
{
Id = "1002",
Name = "Han Solo",
Friends = new[] { "1000", "1003", "2001" },
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi }
};

yield return new Human
{
Id = "1003",
Name = "Leia Organa",
Friends = new[] { "1000", "1002", "2000", "2001" },
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
HomePlanet = "Alderaan"
};

yield return new Human
{
Id = "1004",
Name = "Wilhuff Tarkin",
Friends = new[] { "1001" },
AppearsIn = new[] { Episode.NewHope }
};

yield return new Droid
{
Id = "2000",
Name = "C-3PO",
Friends = new[] { "1000", "1002", "1003", "2001" },
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
PrimaryFunction = "Protocol"
};

yield return new Droid
{
Id = "2001",
Name = "R2-D2",
Friends = new[] { "1000", "1002", "1003" },
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
PrimaryFunction = "Astromech"
};
}

private static IEnumerable<Starship> CreateStarships()
{
yield return new Starship
{
Id = "3000",
Name = "TIE Advanced x1",
Length = 9.2
};
}
}

}
47 changes: 47 additions & 0 deletions src/Benchmark.Tests/Execution/CharacterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using HotChocolate.Resolvers;
using HotChocolate.Types;

namespace HotChocolate.Benchmark.Tests.Execution
{
public class CharacterType
: InterfaceType
{
protected override void Configure(IInterfaceTypeDescriptor descriptor)
{
descriptor.Name("Character");
descriptor.Field("id").Type<NonNullType<StringType>>();
descriptor.Field("name").Type<StringType>();
descriptor.Field("friends").Type<ListType<CharacterType>>();
descriptor.Field("appearsIn").Type<ListType<EpisodeType>>();
descriptor.Field("height").Type<FloatType>()
.Argument("unit", a => a.Type<EnumType<Unit>>());
}

public static IEnumerable<ICharacter> GetCharacter(
IResolverContext context)
{
ICharacter character = context.Parent<ICharacter>();
CharacterRepository repository = context.Service<CharacterRepository>();
foreach (string friendId in character.Friends)
{
ICharacter friend = repository.GetCharacter(friendId);
if (friend != null)
{
yield return friend;
}
}
}

public static double GetHeight(
IResolverContext context)
{
double height = context.Parent<ICharacter>().Height;
if (context.Argument<Unit?>("unit") == Unit.Foot)
{
return height * 3.28084d;
}
return height;
}
}
}
21 changes: 21 additions & 0 deletions src/Benchmark.Tests/Execution/Droid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;

namespace HotChocolate.Benchmark.Tests.Execution
{
public class Droid
: ICharacter
{
public string Id { get; set; }

public string Name { get; set; }

public IReadOnlyList<string> Friends { get; set; }

public IReadOnlyList<Episode> AppearsIn { get; set; }

public string PrimaryFunction { get; set; }

public double Height { get; } = 1.72d;
}

}
20 changes: 20 additions & 0 deletions src/Benchmark.Tests/Execution/DroidType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using HotChocolate.Types;

namespace HotChocolate.Benchmark.Tests.Execution
{
public class DroidType
: ObjectType<Droid>
{
protected override void Configure(IObjectTypeDescriptor<Droid> descriptor)
{
descriptor.Interface<CharacterType>();
descriptor.Field(t => t.Friends)
.Type<ListType<CharacterType>>()
.Resolver(c => CharacterType.GetCharacter(c));
descriptor.Field(t => t.Height)
.Argument("unit", a => a.Type<EnumType<Unit>>())
.Resolver(c => CharacterType.GetHeight(c));
}
}

}
10 changes: 10 additions & 0 deletions src/Benchmark.Tests/Execution/Episode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace HotChocolate.Benchmark.Tests.Execution
{
public enum Episode
{
NewHope,
Empire,
Jedi
}

}
10 changes: 10 additions & 0 deletions src/Benchmark.Tests/Execution/EpisodeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using HotChocolate.Types;

namespace HotChocolate.Benchmark.Tests.Execution
{
public class EpisodeType
: EnumType<Episode>
{
}

}
20 changes: 20 additions & 0 deletions src/Benchmark.Tests/Execution/Human.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;

namespace HotChocolate.Benchmark.Tests.Execution
{
public class Human
: ICharacter
{
public string Id { get; set; }

public string Name { get; set; }

public IReadOnlyList<string> Friends { get; set; }

public IReadOnlyList<Episode> AppearsIn { get; set; }

public string HomePlanet { get; set; }

public double Height { get; } = 1.72d;
}
}
20 changes: 20 additions & 0 deletions src/Benchmark.Tests/Execution/HumanType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using HotChocolate.Types;

namespace HotChocolate.Benchmark.Tests.Execution
{
public class HumanType
: ObjectType<Human>
{
protected override void Configure(IObjectTypeDescriptor<Human> descriptor)
{
descriptor.Interface<CharacterType>();
descriptor.Field(t => t.Friends)
.Type<ListType<CharacterType>>()
.Resolver(c => CharacterType.GetCharacter(c));
descriptor.Field(t => t.Height)
.Argument("unit", a => a.Type<EnumType<Unit>>())
.Resolver(c => CharacterType.GetHeight(c));
}
}

}
14 changes: 14 additions & 0 deletions src/Benchmark.Tests/Execution/ICharacter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace HotChocolate.Benchmark.Tests.Execution
{
public interface ICharacter
{
string Id { get; }
string Name { get; }
IReadOnlyList<string> Friends { get; }
IReadOnlyList<Episode> AppearsIn { get; }
double Height { get; }
}

}
Loading

0 comments on commit 1fc2617

Please sign in to comment.