CodeTimer¶
CodeTimer is the static façade and sole public entry point of the library. It starts and stops named or method-scoped timers keyed by a hierarchical string, aggregates elapsed time in a process-wide static dictionary, and produces an XML summary of captured results.
Purpose¶
- Provide start/stop/reset operations for hierarchical, named or method-scoped timers.
- Gate timing collection through a global
Enabledswitch and (for method-based overloads) a per-method CodeTimerAttribute. - Produce an
XDocumentsummary of all captured timer data viaResults()/Results(bool).
Context¶
- Namespace:
SAIF.Framework.CodeTimer - Assembly/package:
SAIF.Framework.CodeTimer.dll(also packaged as source into theSAIF.BOF.FrameworkNuGet package via nuspec file glob)
Public members¶
| Member | Signature | Purpose |
|---|---|---|
| Enabled | public static bool Enabled |
Global on/off switch for all timing; checked in the private key-based Start(string) overload. |
| GetCodeTimerParent | public static string GetCodeTimerParent(string method, string parentTimerKey) |
Builds a ;-delimited hierarchical key from a parent key and method/timer name. |
| Start | public static string Start(MethodBase method) |
Starts timing for a method, honoring CodeTimerAttribute.Enabled if present. Returns the resolved key. |
| Start | public static string Start(MethodBase method, string parentTimerKey) |
Starts timing for a method nested under the given parent key. |
| Start | public static string Start(MethodBase method, string customName, string parentTimerKey) |
Starts timing for a method using a custom name suffix and parent key. |
| Stop | public static CodeTimerEntry Stop(MethodBase method) |
Stops timing for a method and returns the updated CodeTimerEntry. |
| Stop | public static CodeTimerEntry Stop(MethodBase method, string parentTimerKey) |
Stops timing for a method nested under the given parent key. |
| Stop | public static CodeTimerEntry Stop(MethodBase method, string customName, string parentTimerKey) |
Stops timing for a method using a custom name suffix and parent key. |
| Reset | public static void Reset() |
Clears all captured timer data. |
| Results | public static XDocument Results() |
Returns an XDocument summarizing all captured timers (equivalent to Results(false)), or null if no data was collected. |
| Results | public static XDocument Results(bool includeTimeSpans) |
Returns an XDocument summarizing all captured timers, optionally including per-span millisecond detail, or null if no data was collected. |
Behavioral notes¶
- All timer state is held in a
private static Dictionary<string, CodeTimerEntry> datafield — process-wide/AppDomain-wide global mutable state with no locking, so concurrent calls from multiple threads could race*. - The
Start/Stopoverloads that accept aMethodBasecheck for aCodeTimerAttributeon the method via reflection; if present and itsEnabledflag isfalse, the call is skipped and no timer is created/stopped for that call. Start/Stopoverloads without aMethodBase-based attribute check still respect the globalEnabledswitch through the private key-based overloads.- Calling
Stopfor a key that has never been started throwsApplicationException(see BR-004). Results()/Results(bool)delegate to the internalCodeTimerResulttype, which throwsApplicationExceptionif any timer is still running when results are requested (see BR-003).- A call to the WinForms results viewer (
(new CodeTimerResults(data)).ShowDialog();) is present inResults(bool)but commented out, so it has no effect on current behavior.
Constraints and usage notes¶
CodeTimeris a static class; it cannot be instantiated and all state is shared across the entire process/AppDomain.- Because there is no synchronization around the internal
datadictionary, this type should not be used for concurrent multi-threaded timing without external coordination*. - Setting
Enabled = falsedisables timing globally regardless of anyCodeTimerAttributesettings on individual methods.
Note: Statements marked with * were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts. Statements marked with ** could not be confirmed; treat as unknown until verified.
Sources¶
- SAIF.Framework.CodeTimer\SAIF.Framework.CodeTimer\CodeTimer.cs