TracyWrapper 0.11.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package TracyWrapper --version 0.11.1                
NuGet\Install-Package TracyWrapper -Version 0.11.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TracyWrapper" Version="0.11.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TracyWrapper --version 0.11.1                
#r "nuget: TracyWrapper, 0.11.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TracyWrapper as a Cake Addin
#addin nuget:?package=TracyWrapper&version=0.11.1

// Install TracyWrapper as a Cake Tool
#tool nuget:?package=TracyWrapper&version=0.11.1                

Tracy Profiler Wrapper for C#

This repository contains a C# wrapper for the Tracy Profiler. It provides simple helper functions to easily profile a C# application.

The wrapper uses Tracy-CSharp bindings to interface with Tracy.

Installation

Get Tracy Profiler

This wrapper only supports Tracy v0.11.1. Download here.

From source
  1. Clone this repository via URL: https://github.com/AugsEU/TracyWrapper.git
  2. Add the .csproj to your solution.

Usage

1. Call the heartbeat function

The TracyWrapper.Profiler.HeartBeat(); function needs to be called exactly once per frame.

E.g.

// Called once per frame.
protected override void Update(GameTime gameTime)
{
	TracyWrapper.Profiler.HeartBeat();

	/* Game logic goes here */
}

2. Push and pop zones.

Push a zone to begin profiling a section of code. Pop the zone when the section is over. We can provide a name and a color so we can then identify this block when viewing in the profiler.

E.g.

public void PollInputs(TimeSpan timeStamp)
{
	TracyWrapper.Profiler.PushProfileZone("Inputs", System.Drawing.Color.AliceBlue);

	/* Input polling logic. */

	TracyWrapper.Profiler.PopProfileZone();
}

Make sure you don't forget to pop the profile zone of the profiler will crash.

3. Profile a block using the ProfileScope class

Push a zone to begin profiling a section of code. Pop the zone when the section is over. This can be more convinient but is less accurate, due to the overhead of allocating TracyWrapper.ProfileScope.

E.g.

public void PollInputs(TimeSpan timeStamp)
{
	using (new TracyWrapper.ProfileScope("Inputs", System.Drawing.Color.AliceBlue))
	{
		/* Input polling logic. This part is measured.*/
	}

    /* This code is not measured. */
}

Profiling accuracy

The Tracy profiler running natively on C++ is extremely accurate. Propertedly it is nanosecond accurate.

Using Tracy through this C# wrapper is much less accurate, due to the fact we must allocate strings to begin a profiling block, and then interface with the C++ code through bindings.

From my measurements, profiling via "PushProfileZone" and "PopProfileZone" directly is more accurate than using the TracyWrapper.ProfileScope class. From my crude measurements:

PushProfileZone/PopProfileZone : ~150ns accuracy

TracyWrapper.ProfileScope : ~310ns accuracy

Any operation that takes less than 1μs = 1000 ns should not be profiled.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.