Profiler
SwiftlyS2 provides a profiler service that allows you to measure and track the performance of your plugin code. This is useful for identifying bottlenecks and optimizing your plugin's performance.
Recording Profiles
Start and Stop Recording
Use Core.Profiler.StartRecording and Core.Profiler.StopRecording to measure the execution time of code blocks.
Core.Profiler.StartRecording("MyOperation");
// Your code here
PerformSomeOperation();
Core.Profiler.StopRecording("MyOperation");The profiler will automatically calculate the duration between the start and stop calls and record it.
Manual Time Recording
If you've already calculated the duration externally, you can record it directly using Core.Profiler.RecordTime.
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
// Your code here
PerformSomeOperation();
stopwatch.Stop();
Core.Profiler.RecordTime("MyOperation", stopwatch.ElapsedTicks * (1_000_000.0 / Stopwatch.Frequency)); // measurement in microsecondsProfile Individual Components
Break down complex operations into smaller, measurable components.
public void ProcessPlayerData(IPlayer player)
{
Core.Profiler.StartRecording("ProcessPlayerData.LoadData");
var data = LoadPlayerData(player.SteamID);
Core.Profiler.StopRecording("ProcessPlayerData.LoadData");
Core.Profiler.StartRecording("ProcessPlayerData.ValidateData");
ValidateData(data);
Core.Profiler.StopRecording("ProcessPlayerData.ValidateData");
Core.Profiler.StartRecording("ProcessPlayerData.ApplyData");
ApplyData(player, data);
Core.Profiler.StopRecording("ProcessPlayerData.ApplyData");
}Use Descriptive Names
Choose clear, hierarchical names for your profiles to make them easier to identify.
// Good - Clear and hierarchical
Core.Profiler.StartRecording("Database.PlayerStats.Load");
Core.Profiler.StartRecording("Menu.Creation.BuildOptions");
// Avoid - Too generic
Core.Profiler.StartRecording("Operation");
Core.Profiler.StartRecording("Process");Naming Conventions
Use a consistent naming convention for your profiles to organize them effectively:
category.operation.detailExamples:
Database.Players.LoadDatabase.Players.SaveMenu.Main.CreateMenu.Main.OpenCommands.Teleport.ExecuteEvents.PlayerSpawn.Process
Reference
See IContextedProfilerService for more details.