Skip to content

Commit

Permalink
Merge pull request #773 from Cysharp/feature/MemoryProfilerApi
Browse files Browse the repository at this point in the history
Use Profiler API with PerformanceTest
  • Loading branch information
mayuki authored May 9, 2024
2 parents 3ddd92f + ebaeb5e commit 6ea812c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 51 deletions.
9 changes: 2 additions & 7 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<PropertyGroup>
<GrpcVersion>2.57.0</GrpcVersion>
<GrpcCCoreVersion>2.46.6</GrpcCCoreVersion>
Expand All @@ -11,7 +10,6 @@
<MicrosoftCodeAnalysisVersion>4.3.1</MicrosoftCodeAnalysisVersion>
<MicrosoftCodeAnalysisVersionUnity>3.9.0</MicrosoftCodeAnalysisVersionUnity>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Grpc" Version="$(GrpcCCoreVersion)" />
<PackageVersion Include="Grpc.AspNetCore" Version="$(GrpcVersion)" />
Expand All @@ -23,13 +21,11 @@
<PackageVersion Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics" Version="8.0.0" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" />

<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
<PackageVersion Include="PolySharp" Version="1.13.2" />
<PackageVersion Include="StackExchange.Redis" Version="2.0.601" />
</ItemGroup>

<!-- for tests -->
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
Expand All @@ -44,11 +40,10 @@
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>

<!-- for samples/perf -->
<ItemGroup>
<PackageVersion Include="ConsoleAppFramework" Version="4.2.2" />
<PackageVersion Include="JetBrains.Profiler.Api" Version="1.4.0" />
<PackageVersion Include="JetBrains.Profiler.Api" Version="1.4.3" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
</ItemGroup>
</Project>
</Project>
3 changes: 1 addition & 2 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
</packageSources>
<packageSourceMapping>
<clear />
<packageSource key="nuget">
<packageSource key="nuget.org">
<package pattern="*" />
<package pattern="Microsoft.CodeAnalysis.*" />
</packageSource>
Expand Down
17 changes: 12 additions & 5 deletions perf/BenchmarkApp/PerformanceTest.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ async Task Main(
break;
}

// Create a control channel
using var channelControl = GrpcChannel.ForAddress(config.Url);
var controlServiceClient = MagicOnionClient.Create<IPerfTestControlService>(channelControl);
controlServiceClient.SetMemoryProfilerCollectAllocations(true);

ServerInformation serverInfo;
WriteLog("Gathering the server information...");
{
using var channel = GrpcChannel.ForAddress(config.Url);
serverInfo = await MagicOnionClient.Create<IPerfTestService>(channel).GetServerInformationAsync();
serverInfo = await controlServiceClient.GetServerInformationAsync();
WriteLog($"MagicOnion {serverInfo.MagicOnionVersion}");
WriteLog($"grpc-dotnet {serverInfo.GrpcNetVersion}");
WriteLog($"{nameof(ApplicationInformation.OSDescription)}: {serverInfo.OSDescription}");
Expand All @@ -71,7 +75,7 @@ async Task Main(
results = new List<PerformanceResult>();
resultsByScenario[scenario2] = results;
}
results.Add(await RunScenarioAsync(scenario2, config));
results.Add(await RunScenarioAsync(scenario2, config, controlServiceClient));
}
}

Expand Down Expand Up @@ -125,7 +129,7 @@ async Task Main(
}
}

async Task<PerformanceResult> RunScenarioAsync(ScenarioType scenario, ScenarioConfiguration config)
async Task<PerformanceResult> RunScenarioAsync(ScenarioType scenario, ScenarioConfiguration config, IPerfTestControlService controlService)
{
Func<IScenario> scenarioFactory = scenario switch
{
Expand Down Expand Up @@ -170,16 +174,19 @@ async Task<PerformanceResult> RunScenarioAsync(ScenarioType scenario, ScenarioCo
}
}

await controlService.CreateMemoryProfilerSnapshotAsync("Before Warmup");
WriteLog("Warming up...");
await Task.Delay(TimeSpan.FromSeconds(config.Warmup));
ctx.Ready();
await controlService.CreateMemoryProfilerSnapshotAsync("After Warmup/Run");
WriteLog("Warmup completed");

WriteLog("Running...");
cts.CancelAfter(TimeSpan.FromSeconds(config.Duration));
await Task.WhenAll(tasks);
ctx.Complete();
WriteLog("Completed.");
await controlService.CreateMemoryProfilerSnapshotAsync("Completed");

var result = ctx.GetResult();
WriteLog($"Requests per Second: {result.RequestsPerSecond:0.000} rps");
Expand Down
40 changes: 40 additions & 0 deletions perf/BenchmarkApp/PerformanceTest.Server/PerfTestControlService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using JetBrains.Profiler.Api;
using MagicOnion;
using MagicOnion.Server;
using PerformanceTest.Shared;

namespace PerformanceTest.Server;

public class PerfTestControlService : ServiceBase<IPerfTestControlService>, IPerfTestControlService
{
public UnaryResult<ServerInformation> GetServerInformationAsync()
{
return UnaryResult.FromResult(new ServerInformation(
Environment.MachineName,
ApplicationInformation.Current.MagicOnionVersion,
ApplicationInformation.Current.GrpcNetVersion,
ApplicationInformation.Current.MessagePackVersion,
ApplicationInformation.Current.MemoryPackVersion,
ApplicationInformation.Current.IsReleaseBuild,
ApplicationInformation.Current.FrameworkDescription,
ApplicationInformation.Current.OSDescription,
ApplicationInformation.Current.OSArchitecture,
ApplicationInformation.Current.ProcessArchitecture,
ApplicationInformation.Current.IsServerGC,
ApplicationInformation.Current.ProcessorCount,
ApplicationInformation.Current.IsAttached));
}

public UnaryResult SetMemoryProfilerCollectAllocations(bool enable)
{
MemoryProfiler.CollectAllocations(enable);
return UnaryResult.CompletedResult;
}

public UnaryResult CreateMemoryProfilerSnapshotAsync(string name)
{
MemoryProfiler.GetSnapshot(name);
return UnaryResult.CompletedResult;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" />
<PackageReference Include="JetBrains.Profiler.Api" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using MagicOnion;
using MemoryPack;
using MessagePack;
using System.Runtime.InteropServices;

namespace PerformanceTest.Shared;

public interface IPerfTestControlService : IService<IPerfTestControlService>
{
UnaryResult<ServerInformation> GetServerInformationAsync();

UnaryResult SetMemoryProfilerCollectAllocations(bool enable);
UnaryResult CreateMemoryProfilerSnapshotAsync(string name);
}

[MessagePackObject(true)]
[MemoryPackable]
public partial class ServerInformation
{
public string MachineName { get; set; }
public string? MagicOnionVersion { get; }
public string? GrpcNetVersion { get; }
public string? MessagePackVersion { get; }
public string? MemoryPackVersion { get; }
public bool IsReleaseBuild { get; }
public string FrameworkDescription { get; }
public string OSDescription { get; }
public Architecture OSArchitecture { get; }
public Architecture ProcessArchitecture { get; }
public bool IsServerGC { get; }
public int ProcessorCount { get; }
public bool IsAttached { get; }

public ServerInformation(string machineName, string? magicOnionVersion, string? grpcNetVersion, string? messagePackVersion, string? memoryPackVersion, bool isReleaseBuild, string frameworkDescription, string osDescription, Architecture osArchitecture, Architecture processArchitecture, bool isServerGC, int processorCount, bool isAttached)
{
MachineName = machineName;
MagicOnionVersion = magicOnionVersion;
GrpcNetVersion = grpcNetVersion;
MessagePackVersion = messagePackVersion;
MemoryPackVersion = memoryPackVersion;
IsReleaseBuild = isReleaseBuild;
FrameworkDescription = frameworkDescription;
OSDescription = osDescription;
OSArchitecture = osArchitecture;
ProcessArchitecture = processArchitecture;
IsServerGC = isServerGC;
ProcessorCount = processorCount;
IsAttached = isAttached;
}
}
37 changes: 0 additions & 37 deletions perf/BenchmarkApp/PerformanceTest.Shared/IPerfTestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,10 @@ namespace PerformanceTest.Shared;

public interface IPerfTestService : IService<IPerfTestService>
{
UnaryResult<ServerInformation> GetServerInformationAsync();
UnaryResult<Nil> UnaryParameterless();
UnaryResult<string> UnaryArgRefReturnRef(string arg1, int arg2, int arg3);
UnaryResult<string> UnaryArgDynamicArgumentTupleReturnRef(string arg1, int arg2, int arg3, int arg4);
UnaryResult<int> UnaryArgDynamicArgumentTupleReturnValue(string arg1, int arg2, int arg3, int arg4);
UnaryResult<(int StatusCode, byte[] Data)> UnaryLargePayloadAsync(string arg1, int arg2, int arg3, int arg4, byte[] arg5);
UnaryResult<ComplexResponse> UnaryComplexAsync(string arg1, int arg2, int arg3, int arg4);
}

[MessagePackObject(true)]
[MemoryPackable]
public partial class ServerInformation
{
public string MachineName { get; set; }
public string? MagicOnionVersion { get; }
public string? GrpcNetVersion { get; }
public string? MessagePackVersion { get; }
public string? MemoryPackVersion { get; }
public bool IsReleaseBuild { get; }
public string FrameworkDescription { get; }
public string OSDescription { get; }
public Architecture OSArchitecture { get; }
public Architecture ProcessArchitecture { get; }
public bool IsServerGC { get; }
public int ProcessorCount { get; }
public bool IsAttached { get; }

public ServerInformation(string machineName, string? magicOnionVersion, string? grpcNetVersion, string? messagePackVersion, string? memoryPackVersion, bool isReleaseBuild, string frameworkDescription, string osDescription, Architecture osArchitecture, Architecture processArchitecture, bool isServerGC, int processorCount, bool isAttached)
{
MachineName = machineName;
MagicOnionVersion = magicOnionVersion;
GrpcNetVersion = grpcNetVersion;
MessagePackVersion = messagePackVersion;
MemoryPackVersion = memoryPackVersion;
IsReleaseBuild = isReleaseBuild;
FrameworkDescription = frameworkDescription;
OSDescription = osDescription;
OSArchitecture = osArchitecture;
ProcessArchitecture = processArchitecture;
IsServerGC = isServerGC;
ProcessorCount = processorCount;
IsAttached = isAttached;
}
}

0 comments on commit 6ea812c

Please sign in to comment.