Skip to content

Commit 1fc2617

Browse files
authored
Benchmark Tests (ChilliCream#154)
1 parent ba7cbc0 commit 1fc2617

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1637
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,5 @@ __pycache__/
290290
lcov.info
291291
coverage.json
292292
.DS_Store
293+
294+
BenchmarkDotNet.Artifacts

.vscode/launch.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
"program": "${workspaceFolder}/src/Benchmark.Tests/bin/Debug/netcoreapp2.1/Benchmark.Tests.dll",
13+
"args": [],
14+
"cwd": "${workspaceFolder}",
15+
"console": "internalConsole",
16+
"stopAtEntry": false,
17+
"internalConsoleOptions": "openOnSessionStart"
18+
},
19+
{
20+
"name": ".NET Core Attach",
21+
"type": "coreclr",
22+
"request": "attach",
23+
"processId": "${command:pickProcess}"
24+
}
25+
]
26+
}

run-benchmarks.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dotnet build src -c release
2+
dotnet "src/Benchmark.Tests/bin/Release/netcoreapp2.1/Benchmark.Tests.dll"

run-benchmarks.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dotnet build src -c release
2+
sudo dotnet "src/Benchmark.Tests/bin/Release/netcoreapp2.1/Benchmark.Tests.dll"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<RootNamespace>HotChocolate.Benchmark.Tests</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="BenchmarkDotNet" Version="0.10.13" />
11+
<PackageReference Include="Moq" Version="4.8.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\Core\Core.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<None Remove="Resources\IntrospectionQuery.graphql" />
20+
<None Remove="Resources\KitchenSinkQuery.graphql" />
21+
<None Remove="Resources\KitchenSinkSchema.graphql" />
22+
<None Remove="Resources\SimpleQuery.graphql" />
23+
<None Remove="Resources\SimpleSchema.graphql" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<EmbeddedResource Include="Resources\IntrospectionQuery.graphql" />
28+
<EmbeddedResource Include="Resources\KitchenSinkQuery.graphql" />
29+
<EmbeddedResource Include="Resources\KitchenSinkSchema.graphql" />
30+
<EmbeddedResource Include="Resources\SimpleQuery.graphql" />
31+
<EmbeddedResource Include="Resources\SimpleSchema.graphql" />
32+
</ItemGroup>
33+
34+
</Project>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace HotChocolate.Benchmark.Tests.Execution
5+
{
6+
public class CharacterRepository
7+
{
8+
private Dictionary<string, ICharacter> _characters;
9+
private Dictionary<string, Starship> _starships;
10+
11+
public CharacterRepository()
12+
{
13+
_characters = CreateCharacters().ToDictionary(t => t.Id);
14+
_starships = CreateStarships().ToDictionary(t => t.Id);
15+
}
16+
17+
public ICharacter GetHero(Episode episode)
18+
{
19+
if (episode == Episode.Empire)
20+
{
21+
return _characters["1000"];
22+
}
23+
return _characters["2001"];
24+
}
25+
26+
public ICharacter GetCharacter(string id)
27+
{
28+
if (_characters.TryGetValue(id, out ICharacter c))
29+
{
30+
return c;
31+
}
32+
return null;
33+
}
34+
35+
public Human GetHuman(string id)
36+
{
37+
if (_characters.TryGetValue(id, out ICharacter c)
38+
&& c is Human h)
39+
{
40+
return h;
41+
}
42+
return null;
43+
}
44+
45+
public Droid GetDroid(string id)
46+
{
47+
if (_characters.TryGetValue(id, out ICharacter c)
48+
&& c is Droid d)
49+
{
50+
return d;
51+
}
52+
return null;
53+
}
54+
55+
public IEnumerable<object> Search(string text)
56+
{
57+
foreach(ICharacter character in _characters.Values
58+
.Where(t => t.Name.Contains(text)))
59+
{
60+
yield return character;
61+
}
62+
63+
foreach(Starship starship in _starships.Values
64+
.Where(t => t.Name.Contains(text)))
65+
{
66+
yield return starship;
67+
}
68+
}
69+
70+
private static IEnumerable<ICharacter> CreateCharacters()
71+
{
72+
yield return new Human
73+
{
74+
Id = "1000",
75+
Name = "Luke Skywalker",
76+
Friends = new[] { "1002", "1003", "2000", "2001" },
77+
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
78+
HomePlanet = "Tatooine"
79+
};
80+
81+
yield return new Human
82+
{
83+
Id = "1001",
84+
Name = "Darth Vader",
85+
Friends = new[] { "1004" },
86+
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
87+
HomePlanet = "Tatooine"
88+
};
89+
90+
yield return new Human
91+
{
92+
Id = "1002",
93+
Name = "Han Solo",
94+
Friends = new[] { "1000", "1003", "2001" },
95+
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi }
96+
};
97+
98+
yield return new Human
99+
{
100+
Id = "1003",
101+
Name = "Leia Organa",
102+
Friends = new[] { "1000", "1002", "2000", "2001" },
103+
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
104+
HomePlanet = "Alderaan"
105+
};
106+
107+
yield return new Human
108+
{
109+
Id = "1004",
110+
Name = "Wilhuff Tarkin",
111+
Friends = new[] { "1001" },
112+
AppearsIn = new[] { Episode.NewHope }
113+
};
114+
115+
yield return new Droid
116+
{
117+
Id = "2000",
118+
Name = "C-3PO",
119+
Friends = new[] { "1000", "1002", "1003", "2001" },
120+
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
121+
PrimaryFunction = "Protocol"
122+
};
123+
124+
yield return new Droid
125+
{
126+
Id = "2001",
127+
Name = "R2-D2",
128+
Friends = new[] { "1000", "1002", "1003" },
129+
AppearsIn = new[] { Episode.NewHope, Episode.Empire, Episode.Jedi },
130+
PrimaryFunction = "Astromech"
131+
};
132+
}
133+
134+
private static IEnumerable<Starship> CreateStarships()
135+
{
136+
yield return new Starship
137+
{
138+
Id = "3000",
139+
Name = "TIE Advanced x1",
140+
Length = 9.2
141+
};
142+
}
143+
}
144+
145+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.Generic;
2+
using HotChocolate.Resolvers;
3+
using HotChocolate.Types;
4+
5+
namespace HotChocolate.Benchmark.Tests.Execution
6+
{
7+
public class CharacterType
8+
: InterfaceType
9+
{
10+
protected override void Configure(IInterfaceTypeDescriptor descriptor)
11+
{
12+
descriptor.Name("Character");
13+
descriptor.Field("id").Type<NonNullType<StringType>>();
14+
descriptor.Field("name").Type<StringType>();
15+
descriptor.Field("friends").Type<ListType<CharacterType>>();
16+
descriptor.Field("appearsIn").Type<ListType<EpisodeType>>();
17+
descriptor.Field("height").Type<FloatType>()
18+
.Argument("unit", a => a.Type<EnumType<Unit>>());
19+
}
20+
21+
public static IEnumerable<ICharacter> GetCharacter(
22+
IResolverContext context)
23+
{
24+
ICharacter character = context.Parent<ICharacter>();
25+
CharacterRepository repository = context.Service<CharacterRepository>();
26+
foreach (string friendId in character.Friends)
27+
{
28+
ICharacter friend = repository.GetCharacter(friendId);
29+
if (friend != null)
30+
{
31+
yield return friend;
32+
}
33+
}
34+
}
35+
36+
public static double GetHeight(
37+
IResolverContext context)
38+
{
39+
double height = context.Parent<ICharacter>().Height;
40+
if (context.Argument<Unit?>("unit") == Unit.Foot)
41+
{
42+
return height * 3.28084d;
43+
}
44+
return height;
45+
}
46+
}
47+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Generic;
2+
3+
namespace HotChocolate.Benchmark.Tests.Execution
4+
{
5+
public class Droid
6+
: ICharacter
7+
{
8+
public string Id { get; set; }
9+
10+
public string Name { get; set; }
11+
12+
public IReadOnlyList<string> Friends { get; set; }
13+
14+
public IReadOnlyList<Episode> AppearsIn { get; set; }
15+
16+
public string PrimaryFunction { get; set; }
17+
18+
public double Height { get; } = 1.72d;
19+
}
20+
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using HotChocolate.Types;
2+
3+
namespace HotChocolate.Benchmark.Tests.Execution
4+
{
5+
public class DroidType
6+
: ObjectType<Droid>
7+
{
8+
protected override void Configure(IObjectTypeDescriptor<Droid> descriptor)
9+
{
10+
descriptor.Interface<CharacterType>();
11+
descriptor.Field(t => t.Friends)
12+
.Type<ListType<CharacterType>>()
13+
.Resolver(c => CharacterType.GetCharacter(c));
14+
descriptor.Field(t => t.Height)
15+
.Argument("unit", a => a.Type<EnumType<Unit>>())
16+
.Resolver(c => CharacterType.GetHeight(c));
17+
}
18+
}
19+
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace HotChocolate.Benchmark.Tests.Execution
2+
{
3+
public enum Episode
4+
{
5+
NewHope,
6+
Empire,
7+
Jedi
8+
}
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using HotChocolate.Types;
2+
3+
namespace HotChocolate.Benchmark.Tests.Execution
4+
{
5+
public class EpisodeType
6+
: EnumType<Episode>
7+
{
8+
}
9+
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
3+
namespace HotChocolate.Benchmark.Tests.Execution
4+
{
5+
public class Human
6+
: ICharacter
7+
{
8+
public string Id { get; set; }
9+
10+
public string Name { get; set; }
11+
12+
public IReadOnlyList<string> Friends { get; set; }
13+
14+
public IReadOnlyList<Episode> AppearsIn { get; set; }
15+
16+
public string HomePlanet { get; set; }
17+
18+
public double Height { get; } = 1.72d;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using HotChocolate.Types;
2+
3+
namespace HotChocolate.Benchmark.Tests.Execution
4+
{
5+
public class HumanType
6+
: ObjectType<Human>
7+
{
8+
protected override void Configure(IObjectTypeDescriptor<Human> descriptor)
9+
{
10+
descriptor.Interface<CharacterType>();
11+
descriptor.Field(t => t.Friends)
12+
.Type<ListType<CharacterType>>()
13+
.Resolver(c => CharacterType.GetCharacter(c));
14+
descriptor.Field(t => t.Height)
15+
.Argument("unit", a => a.Type<EnumType<Unit>>())
16+
.Resolver(c => CharacterType.GetHeight(c));
17+
}
18+
}
19+
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
3+
namespace HotChocolate.Benchmark.Tests.Execution
4+
{
5+
public interface ICharacter
6+
{
7+
string Id { get; }
8+
string Name { get; }
9+
IReadOnlyList<string> Friends { get; }
10+
IReadOnlyList<Episode> AppearsIn { get; }
11+
double Height { get; }
12+
}
13+
14+
}

0 commit comments

Comments
 (0)