Table of Contents

Class ProfilerSession

Namespace
AiDotNet.Diagnostics
Assembly
AiDotNet.dll

Instance-based performance profiler for ML operations.

public class ProfilerSession
Inheritance
ProfilerSession
Inherited Members

Remarks

Overview: ProfilerSession provides comprehensive performance monitoring for machine learning workloads including timing, memory tracking, and hierarchical call analysis.

Facade Pattern: This class follows the AiDotNet facade pattern. Users don't create ProfilerSession directly; instead, they configure profiling through AiModelBuilder.ConfigureProfiling() and access results through AiModelResult.ProfilingReport.

Production-Ready Features: - O(1) streaming statistics using Welford's algorithm - Bounded memory with reservoir sampling for percentiles - Configurable sampling rate for high-frequency operations - Thread-safe timing collection - Hierarchical call tree tracking - Memory allocation monitoring - Statistical aggregation (min, max, mean, p95, p99) - Profile scope pattern (using blocks)

Internal Usage Example:

// Configure through facade
var config = new ProfilingConfig { Enabled = true, SamplingRate = 0.1 };
var session = new ProfilerSession(config);

// Profile a region
using (session.Scope("Forward Pass"))
{
    model.Forward(input);
}

// Get report
var report = session.GetReport();

Constructors

ProfilerSession(ProfilingConfig?)

Creates a new profiler session with the specified configuration.

public ProfilerSession(ProfilingConfig? config = null)

Parameters

config ProfilingConfig

Profiling configuration. If null, uses industry-standard defaults.

Properties

Config

Gets the configuration for this profiler session.

public ProfilingConfig Config { get; }

Property Value

ProfilingConfig

Elapsed

Gets the elapsed time since this session started.

public TimeSpan Elapsed { get; }

Property Value

TimeSpan

IsEnabled

Gets whether the profiler session is currently enabled.

public bool IsEnabled { get; }

Property Value

bool

OperationCount

Gets the total number of unique operations tracked.

public int OperationCount { get; }

Property Value

int

Methods

Disable()

Disables profiling for this session.

public void Disable()

Enable()

Enables profiling for this session.

public void Enable()

GetOperationNames()

Gets all operation names that have been profiled.

public IReadOnlyList<string> GetOperationNames()

Returns

IReadOnlyList<string>

GetReport()

Gets a comprehensive profiling report.

public ProfileReport GetReport()

Returns

ProfileReport

A ProfileReport containing all collected data.

GetStats(string)

Gets timing statistics for a specific operation.

public ProfilerStats? GetStats(string name)

Parameters

name string

Operation name.

Returns

ProfilerStats

Statistics or null if not found.

GetSummary()

Gets a summary string of profiling results.

public string GetSummary()

Returns

string

Reset()

Resets all collected profiling data.

public void Reset()

Scope(string)

Creates a scoped profiler that automatically records duration.

public ProfilerSessionScope Scope(string name)

Parameters

name string

Name of the operation being profiled.

Returns

ProfilerSessionScope

A disposable scope that stops timing when disposed.

Start(string)

Starts a manual profiler timer.

public ProfilerSessionTimer Start(string name)

Parameters

name string

Name of the operation being profiled.

Returns

ProfilerSessionTimer

A timer that must be stopped manually.