Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IBM cost analysis #7146

Merged
merged 13 commits into from
Jun 26, 2024
4 changes: 4 additions & 0 deletions .build/Build.Tests.2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ partial class Build
.Produces(TestResultDirectory / "*.trx")
.Executes(() => RunTests(SourceDirectory / "HotChocolate" / "Core" / "HotChocolate.Core.sln"));

Target TestHotChocolateCostAnalysis => _ => _
.Produces(TestResultDirectory / "*.trx")
.Executes(() => RunTests(SourceDirectory / "HotChocolate" / "CostAnalysis" / "HotChocolate.CostAnalysis.sln"));

Target TestHotChocolateData => _ => _
.Produces(TestResultDirectory / "*.trx")
.Executes(() => RunTests(SourceDirectory / "HotChocolate" / "Data" / "HotChocolate.Data.sln"));
Expand Down
1 change: 1 addition & 0 deletions .build/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static class Helpers
Path.Combine("HotChocolate", "AspNetCore"),
Path.Combine("HotChocolate", "AzureFunctions"),
Path.Combine("HotChocolate", "Core"),
Path.Combine("HotChocolate", "CostAnalysis"),
Path.Combine("HotChocolate", "Caching"),
Path.Combine("HotChocolate", "Diagnostics"),
Path.Combine("HotChocolate", "Language"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<InternalsVisibleTo Include="HotChocolate.Types.Mutations" />
<InternalsVisibleTo Include="HotChocolate.AspNetCore.Tests" />
<InternalsVisibleTo Include="HotChocolate.CostAnalysis" />
<InternalsVisibleTo Include="HotChocolate.Data" />
<InternalsVisibleTo Include="HotChocolate.Data.Raven" />
<InternalsVisibleTo Include="HotChocolate.Fusion" />
Expand Down
1 change: 1 addition & 0 deletions src/HotChocolate/Core/src/Types/HotChocolate.Types.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="HotChocolate.CostAnalysis" />
<InternalsVisibleTo Include="HotChocolate.Execution" />
<InternalsVisibleTo Include="HotChocolate.Execution.Tests" />
<InternalsVisibleTo Include="HotChocolate.Types.Errors" />
Expand Down
10 changes: 10 additions & 0 deletions src/HotChocolate/CostAnalysis/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))" />

<PropertyGroup>
<TargetFrameworks>$(Library3TargetFrameworks)</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
36 changes: 36 additions & 0 deletions src/HotChocolate/CostAnalysis/HotChocolate.CostAnalysis.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3F8C3AE9-6085-43F1-A593-CC7C0B9A4989}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.CostAnalysis", "src\CostAnalysis\HotChocolate.CostAnalysis.csproj", "{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{10D8894D-98C1-4F32-A6DE-8E2268E0D69B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.CostAnalysis.Tests", "test\CostAnalysis.Tests\HotChocolate.CostAnalysis.Tests.csproj", "{6F56773B-1192-4951-8F4A-6C2E5835410D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}.Release|Any CPU.Build.0 = Release|Any CPU
{6F56773B-1192-4951-8F4A-6C2E5835410D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F56773B-1192-4951-8F4A-6C2E5835410D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F56773B-1192-4951-8F4A-6C2E5835410D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F56773B-1192-4951-8F4A-6C2E5835410D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314} = {3F8C3AE9-6085-43F1-A593-CC7C0B9A4989}
{6F56773B-1192-4951-8F4A-6C2E5835410D} = {10D8894D-98C1-4F32-A6DE-8E2268E0D69B}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Reflection;
using HotChocolate.CostAnalysis.Directives;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;

namespace HotChocolate.CostAnalysis.Attributes;

/// <summary>
/// Applies the <c>@cost</c> directive. The purpose of the <c>cost</c> directive is to define a
/// <c>weight</c> for GraphQL types, fields, and arguments. Static analysis can use these weights
/// when calculating the overall cost of a query or response.
/// </summary>
[AttributeUsage(
AttributeTargets.Class
| AttributeTargets.Enum
| AttributeTargets.Method
| AttributeTargets.Parameter
| AttributeTargets.Property
| AttributeTargets.Struct)]
public sealed class CostAttribute : DescriptorAttribute
glen-84 marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly string _weight;

/// <summary>
/// Initializes a new instance of <see cref="CostAttribute"/>.
/// </summary>
/// <param name="weight">
/// The <c>weight</c> argument defines what value to add to the overall cost for every
/// appearance, or possible appearance, of a type, field, argument, etc.
/// </param>
public CostAttribute(string weight)
{
if (weight is null)
{
throw new ArgumentNullException(nameof(weight));
}

_weight = weight;
}

protected internal override void TryConfigure(
IDescriptorContext context,
IDescriptor descriptor,
ICustomAttributeProvider element)
{
switch (descriptor)
{
case IArgumentDescriptor argumentDescriptor:
argumentDescriptor.Directive(new CostDirective(_weight));
break;

case IEnumTypeDescriptor enumTypeDescriptor:
enumTypeDescriptor.Directive(new CostDirective(_weight));
break;

case IInputFieldDescriptor inputFieldDescriptor:
inputFieldDescriptor.Directive(new CostDirective(_weight));
break;

case IObjectFieldDescriptor objectFieldDescriptor:
objectFieldDescriptor.Directive(new CostDirective(_weight));
break;

case IObjectTypeDescriptor objectTypeDescriptor:
objectTypeDescriptor.Directive(new CostDirective(_weight));
break;

case IScalarTypeDescriptor scalarTypeDescriptor:
scalarTypeDescriptor.Directive(new CostDirective(_weight));
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Reflection;
using HotChocolate.CostAnalysis.Directives;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;

namespace HotChocolate.CostAnalysis.Attributes;

/// <summary>
/// Applies the <c>@listSize</c> directive. The purpose of the <c>@listSize</c> directive is to
/// either inform the static analysis about the size of returned lists (if that information is
/// statically available), or to point the analysis to where to find that information.
/// </summary>
public sealed class ListSizeAttribute : ObjectFieldDescriptorAttribute
{
private readonly int? _assumedSize;

/// <summary>
/// The maximum length of the list returned by this field.
/// </summary>
public int AssumedSize
{
get => _assumedSize ?? 0;
init => _assumedSize = value;
}

/// <summary>
/// The arguments of this field with numeric type that are slicing arguments. Their value
/// determines the size of the returned list.
/// </summary>
public string[]? SlicingArguments { get; init; }

/// <summary>
/// The subfield(s) that the list size applies to.
/// </summary>
public string[]? SizedFields { get; init; }

/// <summary>
/// Whether to require a single slicing argument in the query. If that is not the case (i.e., if
/// none or multiple slicing arguments are present), the static analysis will throw an error.
/// </summary>
public bool RequireOneSlicingArgument { get; init; } = true;

protected override void OnConfigure(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member)
{
descriptor.Directive(
new ListSizeDirective(
_assumedSize,
SlicingArguments?.ToList(),
SizedFields?.ToList(),
RequireOneSlicingArgument));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Utilities;

namespace HotChocolate.CostAnalysis.Caching;

internal sealed class DefaultCostMetricsCache(int capacity = 100) : ICostMetricsCache
{
private readonly Cache<CostMetrics> _cache = new(capacity);

public int Capacity => _cache.Capacity;

public int Count => _cache.Usage;

public bool TryGetCostMetrics(
string operationId,
[NotNullWhen(true)] out CostMetrics? costMetrics)
=> _cache.TryGet(operationId, out costMetrics);

public void TryAddCostMetrics(
string operationId,
CostMetrics costMetrics)
=> _cache.GetOrCreate(operationId, () => costMetrics);

public void Clear() => _cache.Clear();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Diagnostics.CodeAnalysis;

namespace HotChocolate.CostAnalysis.Caching;

internal interface ICostMetricsCache
{
/// <summary>
/// Gets the maximum number of <c>CostMetrics</c> instances that can be cached. The default
/// value is <c>100</c>. The minimum allowed value is <c>10</c>.
/// </summary>
int Capacity { get; }

/// <summary>
/// Gets the number of <c>CostMetrics</c> instances residing in the cache.
/// </summary>
int Count { get; }

/// <summary>
/// Tries to get a <c>CostMetrics</c> instance by <paramref name="operationId" />.
/// </summary>
/// <param name="operationId">
/// The internal operation ID.
/// </param>
/// <param name="costMetrics">
/// The <c>CostMetrics</c> instance that is associated with the ID or null
/// if no <c>CostMetrics</c> instance was found that matches the specified ID.
/// </param>
/// <returns>
/// <c>true</c> if a <c>CostMetrics</c> instance was found that matches the specified
/// <paramref name="operationId"/>, otherwise <c>false</c>.
/// </returns>
bool TryGetCostMetrics(
string operationId,
[NotNullWhen(true)] out CostMetrics? costMetrics);

/// <summary>
/// Tries to add a new <c>CostMetrics</c> instance to the cache.
/// </summary>
/// <param name="operationId">
/// The internal operation ID.
/// </param>
/// <param name="costMetrics">
/// The <c>CostMetrics</c> instance that shall be cached.
/// </param>
void TryAddCostMetrics(
string operationId,
CostMetrics costMetrics);

/// <summary>
/// Clears all items from the cache.
/// </summary>
void Clear();
}
7 changes: 7 additions & 0 deletions src/HotChocolate/CostAnalysis/src/CostAnalysis/Cost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace HotChocolate.CostAnalysis;

/// <summary>https://ibm.github.io/graphql-specs/cost-spec.html#sec-__cost</summary>
internal sealed class Cost(CostMetrics requestCosts)
{
public CostMetrics RequestCosts { get; } = requestCosts;
}
Loading
Loading