Perform Unit Testing together with the Profiler
It may sometimes be useful to perform unit testing (e.g. using NUnit) together with the profiler, especially when the .NET Memory Profiler API is used.
There are a few different ways of running unit tests under the profiler:
Use the Run tests under profiler command in Visual Studio (Enterprise edition only)
Start the unit test runner using the Profile unit tests command
Use the NmpDataCollector task in MSBuild (and Visual Studio)
Use NmpCore to start the unit test runner
Use the NmpCore task in MSBuild (and Visual Studio)
Use the Run tests under profiler command in Visual Studio
In the Enterprise edition you can easily run unit tests under the profiler by using the command “Profiler->Run tests under the Profiler” in Visual Studio. For more information, see Run Tests under the Profiler and the What's new page.
Start the unit test runner using the Profile unit tests command
The Profile unit tests command (under the File menu) can also be used to profile unit tests interactively).
Select the Profile unit tests command from the menu or toolbar.
Select the unit test framework to use (VS Test, .NET test, NUnit, or XUnit).
It is recommended that the Tool path to the unit test runner is specified. The profiler will try to locate VS Test and .NET Test, but NUNit and XUnit will only be found when using the NmpCore task in a project where the unit test runner is added as a NuGet package.
Specify any additional arguments needed for the test runner.
Click Next to specify additional session settings, or Finish to accept the default settings.
Use the NmpDataCollector to profile unit test during build
The NmpDataCollector is a VSTest data collector that can be added to a unit test project. It makes it easy to profile unit tests during build, either within Visual Studio or in a build pipeline. For more information, see the NmpDataCollector page.
Use NmpCore to start the unit test runner
The NmpCore tool is well suited to run automated memory unit tests. The unit test runner can be started in the same way as when using the Profile application command in .NET Memory Profiler, i.e. by specifying the unit test runner and its arguments. The following command line can, for instance, be used to start NUnit under NmpCore:
NmpCore [path]\nunit-console.exe [arguments to NUnit]
If using a test framework that may spawn separate test processes (e.g. VSTest), it may be necessary to make sure that all created processes are profiled, using the /profileprocess argument, or to prevent separate processes using the VSTest argument /NoIsolation. For example:
NmpCore /profileprocess:all [path]\vstest.console.exe [arguments to VSTest]
or
NmpCore [path]\vstest.console.exe /NoIsolation [additional arguments to VSTest]
Note
If multiple processes are included in a session, it can only be opened using the Enterprise edition of .NET Memory Profiler.
For more information about command line arguments, see Command Line Arguments.
Another option is to create a unit test project in .NET Memory Profiler and then use that project to profiler the test runner.
Create a new profiler project in .NET Memory Profiler (File->New profiler project)
Select Unit Tests as the process to profile.
Select the unit test framework to use (VS Test, NUnit, XUnit, or .NET Core test).
It is recommended that the Tool path to the unit test runner is specified. The profiler will try to locate VS Test and .NET Core Test, but NUNit and XUnit will only be found when using the NmpCore task in a project where the unit test runner is added as a NuGet package.
Specify any additional arguments needed for the test runner.
Click Next to specify additional session settings, or Finish to accept the default settings.
Save the project.
Use NmpCore to start profiling using the saved project. For example by using the command:
NmpCore /project:[path]\TestProject.prfproj
It is also possible to start this project from .NET Memory Profiler, e.g. by right-clicking the project and then select Start profiler project.
The main advantage of using the Unit Test profiling type is that it automatically profiles multiple processes (e.g. additional processes created by the test runner) and that the created session files can be opened by the Professional edition, even if it contains multiple processes.
Use the NmpCore task in MSBuild
The NmpCore MSBuild task can also be used to start profiling unit tests within the build process, even though it is recommended to use NmpDataCollector instead. The NmpCore task is available as a NuGet package: SciTech.NmpCore.Task. To automatically run a profiler project after each build:
Add the SciTech.NmpCore.Task package to the Visual Studio project.
Add a profiler project to a Visual Studio project. See Use NmpCore to start the unit test runner for information about how the create the profiler project.
Set the Build action to NmpCore for the profiler project.
For more information about the NmpCore task, see NmpCore MSBuild Task.