Skip to content

Commit

Permalink
Removed all instances of Environment.TickCount and replaced it with…
Browse files Browse the repository at this point in the history
… our instanced Stopwatch
  • Loading branch information
raulssorban committed Oct 22, 2023
1 parent 9bd4cbe commit c860f16
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Carbon.Core/Carbon.Components/Carbon.Bootstrap
14 changes: 6 additions & 8 deletions Carbon.Core/Carbon/src/Hooks/HookCallerInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ public override object CallHook<T>(T hookable, uint hookId, BindingFlags flags,
Profiler.StartHookCall(hookable, hookId);
#endif

var beforeTicks = Environment.TickCount;
hookable.TrackStart();
var beforeMemory = hookable.TotalMemoryUsed;

Expand All @@ -167,34 +166,33 @@ public override object CallHook<T>(T hookable, uint hookId, BindingFlags flags,
}

hookable.TrackEnd();
var afterTicks = Environment.TickCount;
var afterHookTime = hookable.CurrentHookTime;
var afterMemory = hookable.TotalMemoryUsed;
var totalTicks = afterTicks - beforeTicks;
var totalMemory = afterMemory - beforeMemory;

#if DEBUG
Profiler.EndHookCall(hookable);
#endif

AppendHookTime(hookId, totalTicks);
AppendHookTime(hookId, afterHookTime);

if (cachedHook != null)
{
cachedHook.HookTime += totalTicks;
cachedHook.HookTime += afterHookTime;
cachedHook.MemoryUsage += totalMemory;
}

if (afterTicks > beforeTicks + 100 && afterTicks > beforeTicks)
if (afterHookTime > 100)
{
if (hookable is Plugin basePlugin && !basePlugin.IsCorePlugin)
{
Carbon.Logger.Warn($" {hookable.Name} hook '{readableHook}' took longer than 100ms [{totalTicks:0}ms]{(hookable.HasGCCollected ? " [GC]" : string.Empty)}");
Carbon.Logger.Warn($" {hookable.Name} hook '{readableHook}' took longer than 100ms [{afterHookTime:0}ms]{(hookable.HasGCCollected ? " [GC]" : string.Empty)}");
Community.Runtime.Analytics.LogEvent("plugin_time_warn",
segments: Community.Runtime.Analytics.Segments,
metrics: new Dictionary<string, object>
{
{ "name", $"{readableHook} ({basePlugin.Name} v{basePlugin.Version} by {basePlugin.Author})" },
{ "time", $"{totalTicks.RoundUpToNearestCount(50)}ms" },
{ "time", $"{afterHookTime.RoundUpToNearestCount(50)}ms" },
{ "memory", $"{ByteEx.Format(totalMemory, shortName: true).ToLower()}" },
{ "hasgc", hookable.HasGCCollected }
});
Expand Down

0 comments on commit c860f16

Please sign in to comment.