Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Simplify implementation
  • Loading branch information
martinothamar committed Mar 31, 2024
commit 999dae773b2c3569c68f655c91acbf4d982ac9b2
22 changes: 11 additions & 11 deletions src/BenchmarkDotNet.Diagnostics.dotMemory/DotMemoryDiagnoser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Validators;
using RunMode = BenchmarkDotNet.Diagnosers.RunMode;

Expand All @@ -22,7 +21,7 @@ public class DotMemoryDiagnoser : IProfiler
private readonly Uri? nugetUrl;
private readonly string? toolsDownloadFolder;

private DotMemoryToolBase? tool;
private DotMemoryTool? tool;

public DotMemoryDiagnoser(Uri? nugetUrl = null, string? toolsDownloadFolder = null)
{
Expand All @@ -44,13 +43,6 @@ public void Handle(HostSignal signal, DiagnoserActionParameters parameters)
{
var logger = parameters.Config.GetCompositeLogger();
var job = parameters.BenchmarkCase.Job;
bool isInProcess = job.GetToolchain().IsInProcess;
if (tool is null || (isInProcess ? tool is ExternalDotMemoryTool : tool is InProcessDotMemoryTool))
{
tool = isInProcess
? new InProcessDotMemoryTool(logger, nugetUrl, downloadTo: toolsDownloadFolder)
: new ExternalDotMemoryTool(logger, nugetUrl, downloadTo: toolsDownloadFolder);
}

var runtimeMoniker = job.Environment.GetRuntime().RuntimeMoniker;
if (!IsSupported(runtimeMoniker))
Expand All @@ -62,13 +54,21 @@ public void Handle(HostSignal signal, DiagnoserActionParameters parameters)
switch (signal)
{
case HostSignal.BeforeAnythingElse:
tool.Init(parameters);
if (tool is not null)
throw new InvalidOperationException("DotMemory tool is already initialized");
tool = new DotMemoryTool(logger, nugetUrl, downloadTo: toolsDownloadFolder);
tool.Init();
break;
case HostSignal.BeforeActualRun:
if (tool is null)
throw new InvalidOperationException("DotMemory tool is not initialized");
snapshotFilePaths.Add(tool.Start(parameters));
break;
case HostSignal.AfterActualRun:
tool.Stop(parameters);
if (tool is null)
throw new InvalidOperationException("DotMemory tool is not initialized");
tool.Stop();
tool = null;
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using BenchmarkDotNet.Diagnosers;
Expand All @@ -8,22 +9,22 @@

namespace BenchmarkDotNet.Diagnostics.dotMemory
{
internal abstract class DotMemoryToolBase
internal sealed class DotMemoryTool
{
private readonly ILogger logger;
private readonly Uri? nugetUrl;
private readonly NuGetApi nugetApi;
private readonly string? downloadTo;

protected DotMemoryToolBase(ILogger logger, Uri? nugetUrl = null, NuGetApi nugetApi = NuGetApi.V3, string? downloadTo = null)
public DotMemoryTool(ILogger logger, Uri? nugetUrl = null, NuGetApi nugetApi = NuGetApi.V3, string? downloadTo = null)
{
this.logger = logger;
this.nugetUrl = nugetUrl;
this.nugetApi = nugetApi;
this.downloadTo = downloadTo;
}

public void Init(DiagnoserActionParameters parameters)
public void Init()
{
try
{
Expand All @@ -39,10 +40,6 @@ public void Init(DiagnoserActionParameters parameters)
}
}

protected abstract void Attach(DiagnoserActionParameters parameters, string snapshotFile);
protected abstract void Snapshot(DiagnoserActionParameters parameters);
protected abstract void Detach();

public string Start(DiagnoserActionParameters parameters)
{
string snapshotFile = ArtifactFileNameHelper.GetFilePath(parameters, "snapshots", DateTime.Now, "dmw", ".0000".Length);
Expand Down Expand Up @@ -76,12 +73,12 @@ public string Start(DiagnoserActionParameters parameters)
return snapshotFile;
}

public void Stop(DiagnoserActionParameters parameters)
public void Stop()
{
try
{
logger.WriteLineInfo("Taking dotMemory snapshot...");
Snapshot(parameters);
Snapshot();
logger.WriteLineInfo("dotMemory snapshot is successfully taken");
}
catch (Exception e)
Expand All @@ -101,7 +98,24 @@ public void Stop(DiagnoserActionParameters parameters)
}
}

protected string GetRunnerPath()
private void Attach(DiagnoserActionParameters parameters, string snapshotFile)
{
var config = new DotMemory.Config();

var pid = parameters.Process.Id;
var currentPid = Process.GetCurrentProcess().Id;
if (pid != currentPid)
config = config.ProfileExternalProcess(pid);

config = config.SaveToFile(snapshotFile);
DotMemory.Attach(config);
}

private void Snapshot() => DotMemory.GetSnapshot();

private void Detach() => DotMemory.Detach();

private string GetRunnerPath()
{
var consoleRunnerPackageField = typeof(DotMemory).GetField("ConsoleRunnerPackage", BindingFlags.NonPublic | BindingFlags.Static);
if (consoleRunnerPackageField == null)
Expand Down
126 changes: 0 additions & 126 deletions src/BenchmarkDotNet.Diagnostics.dotMemory/ExternalDotMemoryTool.cs

This file was deleted.

This file was deleted.

15 changes: 12 additions & 3 deletions tests/BenchmarkDotNet.IntegrationTests/DotMemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,26 @@ public void DotMemorySmokeTest()
Output.WriteLine("Snapshots:");
foreach (string snapshot in snapshots)
Output.WriteLine("* " + snapshot);
Assert.Equal(2, snapshots.Count);
Assert.Equal(4, snapshots.Count);
}

[DotMemoryDiagnoser]
public class Benchmarks
{
[Benchmark]
public int Foo()
public int Foo0()
{
var list = new List<object>();
for (int i = 0; i < 4; i++)
for (int i = 0; i < 1000; i++)
list.Add(new object());
return list.Count;
}

[Benchmark]
public int Foo1()
{
var list = new List<object>();
for (int i = 0; i < 1000; i++)
list.Add(new object());
return list.Count;
}
Expand Down