.NET Memory Profiler - NMP Data Collector

NMP Data Collector is a VSTest data collector, available as a NuGet package . It makes it very easy to run unit tests under the profiler within a build process, e.g. a CI/CD pipeline.

To use the data collector for a project, the SciTech.NmpDataCollector package should be added to the project. The data collector is then enabled in a few different ways, depending on how the tests are run:

Visual Studio console test runner

The data collector is enabled by using the /Collect command line argument to vstest.console.exe:

vstest.console.exe <Test library> /Collect:"NMP Data Collector"

.NET Core/.NET 5 test runner

The data collector is enabled by using the --collect command line option to the dotnet test command:

dotnet.exe test <Test library or project> --collect "NMP Data Collector"

Visual Studio 2017/2019 Test explorer

To run the data collector using the Visual Studio Test explorer, it has to be configured using a .runsettings file. The data collector is enabled by specifying "NMP Data Collector" as the DataCollector in the .runsettings file:

<?xml version="1.0"?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="NMP Data Collector" />
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

CI/CD Build pipeline

The data collector can also be enabled in a build pipeline. Below you will find an example of how it can be enabled in an Azure pipeline.

- task: DotNetCoreCLI@2
  displayName: 'Run tests under profiler'
  inputs:
    command: 'test'
    projects: '<Test project>.csproj'
    arguments: '-c Release --collect "NMP Data Collector"'

Run settings

In addition to enabling the data collector for the Visual Studio test explorer, the .runsettings file can be used to configure the collector and the profiler.

Attach sessions to test cases

Profiler sessions can be included as attachments to the test session or individual test cases by enabling the configuration options: SendSessionForTestCase and SendSessionForTestSession (available options are Always, WhenFailed, and Never).

The configuration below will always attach the profiler session to the test session, and additionally attach profiler sessions to each failed test.

<?xml version="1.0"?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="NMP Data Collector">
                <Configuration>
                    <SendSessionForTestSession>Always<SendSessionForTestSession>
                    <SendSessionForTestCase>WhenFailed</SendSessionForTestCase>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

If a test fails, the session will be included as an attachement. For instance in an Azure pipeline:

Failed test session as case attachment

Use the MemProfiler.BeginTest API to associate profiler snapshots with a unit test. BeginTest can be called from the test itself.

[TestMethod]
public void TestMethod()
{
    Assert.IsTrue(MemProfiler.AreAssertionsEnabled);

    using( MemProfiler.BeginTest(nameof(TestMethod));
    {
        // Do test...
    }
}

Or in the test setup method, with a matching call to EndTest in the tear down method:

[SetUp]
public void Setup()
{
    MemProfiler.BeginTest(TestContext.CurrentContext.Test.Name);
}

[TearDown]
public void TearDown()
{
    MemProfiler.EndTest();
}

See the API examples for more information about the .NET Memory Profiler API.

Configure Profiler Settings

Profiler settings for the data collector can be configured using a profiler project file. The project file is specified using the ConfigFile element in the Configuration section. Only the settings from the project file will be used, the process to launch (if specified) will be ignored.

<?xml version="1.0"?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="NMP Data Collector">
                <Configuration>
                    <ConfigFile>DataCollectorSettings.prfproj</ConfigFile>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

The following NuGet packages related to automated memory testing using .NET Memory Profiler are available:

© Copyright 2001-2021. SciTech Software AB
All rights reserved.

CONTACT INFO

SciTech Software AB
Ynglingavägen 1-3
SE-177 57 Järfälla
Sweden

E-mail: mail@scitech.se
Telephone: +46-706868081