Skip to content
Peter edited this page Feb 3, 2024 · 2 revisions

Welcome to the S&box Performance Tracing wiki!

Example

using PerformanceTracing;
using PerformanceTracing.Traces;

// Optional to customize your storage provider.
var options = new JsonSerializerOptions
{
    WriteIndented = true
};
var provider = new JsonStorageProvider( options );

// Start a new trace.
Tracing.Start( TracingOptions.Default
    .WithAppendStackTrace( true ) // Whether or not to add the stack traces to all events.
    .WithAppendCallerPath( true ) // Whether or not to append the calling methods file and line location to events.
    .WithUseSimpleNames( false ) // Whether or not basic method names should be converted to their fully qualified name.
    .WithMaxConcurrentTraces( TraceType.Performance, 100 ) // Limits the number of traces of a specific type to exist at once.
    .WithStorageProvider( provider ) // Sets a custom storage provider to use. Defaults to JsonStorageProvider.
);

// Stop any existing trace.
Tracing.Stop();

// Embeds metadata to the trace.
Tracing.AddMetaData( "game", "Grubs" );

// Built-in saving methods.
Tracing.CopyToClipboard() // Copies all data to your clipboard.
Tracing.SaveToFile( "trace.json", FileSystem.Data ); // Saves all data to a file on the system.
var memoryStream = Tracing.GetStream() // Gets a stream of the trace data for you to use however you want.

//
// Trace Usage
//

// Performance trace.
{
    using var _ = PerformanceTrace.New(); // Tracks the duration taken inside the given block of code.
    // Your code...
}

// Counter trace.
var counter = CounterTrace.New( "Some Variable", 0 ); // Sends events when changes are made.
for ( var i = 1; i <= 10; i++ )
    counter.Update( i );

// Marker trace.
MarkerTrace.New( "Game has started" ); // Marks a point in time that something has occurred.
Clone this wiki locally