PerformanceAnalyzer

Class in ChivalrousGames.Common.Diagnostics

Description

The performance analyzer is a tool to get performance metrics from code. Use the methods provided by this class in Unity scripts to analyze performance. The analyzer will create a game object that holds a PerformanceReport that holds the data. This is a project development editor tool. A release build should not include this code. Compiler conditions can help avoid adding and removing code from a project multiple times.

Usage Example 1: basic usage

void Start() {
	PerformanceAnalyzer.BeginReport("MyPerfReport");
	method1();
	PerformanceAnalyzer.EndReport();
}
public void method1() {
	PerformanceAnalyzer.BeginEntryTracking("method1");
	//Process some stuff here
	PerformanceAnalyzer.PauseEntryTracking("method1");
	method2();
	PerformanceAnalyzer.ResumeEntryTracking("method1");
	//Some more processing
	PerformanceAnalyzer.EndEntryTracking("method1");
}
public void method2() {
	PerformanceAnalyzer.BeginEntryTracking("method2");
	//Process some stuff here
	PerformanceAnalyzer.EndEntryTracking("method2");
}

Usage Example 2: with compiler conditions

#define ANALYZE_PERF
void Start() {
	#if ANALYZE_PERF
	PerformanceAnalyzer.BeginReport("MyPerfReport");
	#endif
	method1();
	#if ANALYZE_PERF
	PerformanceAnalyzer.EndReport();
	#endif
}
public void method1() {
	#if ANALYZE_PERF
	PerformanceAnalyzer.BeginEntryTracking("method1");
	#endif
	//Process some stuff here
	#if ANALYZE_PERF
	PerformanceAnalyzer.PauseEntryTracking("method1");
	#endif
	method2();
	#if ANALYZE_PERF
	PerformanceAnalyzer.ResumeEntryTracking("method1");
	#endif
	//Some more processing
	#if ANALYZE_PERF
	PerformanceAnalyzer.EndEntryTracking("method1");
	#endif
}
public void method2() {
	#if ANALYZE_PERF
	PerformanceAnalyzer.BeginEntryTracking("method2");
	#endif
	//Process some stuff here
	#if ANALYZE_PERF
	PerformanceAnalyzer.EndEntryTracking("method2");
	#endif
}

Static Methods

BeginReport
public static void BeginReport(string reportName);
Parameters
reportName string Name given to the performance report game object
Description

Creates and adds a new game object to the scene. Attaches a PerformanceReport MonoBehaviour to the game object.

BeginEntryTracking
public static void BeginEntryTracking (string name);
Parameters
name string Name given to the report entry
Description

Adds an entry in the performance report if there is none or reuses an existing entry with that name. Increments the number of iterations of this entry. Begins to track time spent for that entry. This can be used at the beginning of a method to track the process time spent by that method.

PauseEntryTracking
public static void PauseEntryTracking (string name);
Parameters
name string Name of the report entry for which to pause time tracking
Description

Adds an entry in the performance report if there is none or reuses an existing entry with that name. Pauses time spent by that entry. This method is useful when trying to track time spent by a parent method and a child. Pause the caller entry method before it calls the child and resume after the child method returns. See the usage example of the class and see how method1 and method2 are wrapped around “pause” and “resume” calls.

ResumeEntryTracking
public static void ResumeEntryTracking (string name);
Parameters
name string Name of the report entry for which to resume time tracking
Description

Adds an entry in the performance report if there is none or reuses an existing entry with that name. Resumes time spent by that entry. This method is useful when trying to track time spent by a parent method and a child. Pause the caller entry method before it calls the child and resume after the child method returns. See the usage example of the class and see how method1 and method2 are wrapped around “pause” and “resume” calls.

EndEntryTracking
public static void EndEntryTracking (string name);
Parameters
name string Name of the report entry for which to stop tracking time for this iteration
Description

This method will stop tracking time for a report entry. Time can be tracked for another iteration of taht entry by calling BeginEntryTracking once more.

EndReport
public static void EndReport();
Description

Finishes the performance report. This should only be called once and then the other methods should not be called for that report again.