Skip to content

Commit

Permalink
If DEBUG_SCHEDULER is defined, /taskdebug also outputs how long each …
Browse files Browse the repository at this point in the history
…task takes to complete
  • Loading branch information
UnknownShadow200 committed Nov 20, 2017
1 parent 7970334 commit f5004f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
23 changes: 17 additions & 6 deletions fCraft/System/Scheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public static class Scheduler {
static Thread schedulerThread,
backgroundThread;


#if DEBUG_SCHEDULER
public static event EventHandler<SchedulerTaskEventArgs> TaskAdded;
public static event EventHandler<SchedulerTaskEventArgs> TaskRemoved;
public static event EventHandler<SchedulerTaskEventArgs> TaskExecuted;
public static event EventHandler<SchedulerTaskEventArgs> TaskExecuting;
#endif
public static int CriticalTaskCount {
get {
lock( BackgroundTaskQueueLock ) {
Expand Down Expand Up @@ -65,6 +70,7 @@ public static class Scheduler {
} else {
task.IsExecuting = true;
#if DEBUG_SCHEDULER
task.ExecuteStart = DateTime.UtcNow;
FireEvent( TaskExecuting, task );
#endif

Expand All @@ -82,6 +88,7 @@ public static class Scheduler {
#endif

#if DEBUG_SCHEDULER
task.ExecuteEnd = DateTime.UtcNow;
FireEvent( TaskExecuted, task );
#endif
}
Expand Down Expand Up @@ -130,6 +137,7 @@ public static class Scheduler {
static void ExecuteBackgroundTask( SchedulerTask task ) {
task.IsExecuting = true;
#if DEBUG_SCHEDULER
task.ExecuteStart = DateTime.UtcNow;
FireEvent( TaskExecuting, task );
#endif

Expand All @@ -146,6 +154,7 @@ public static class Scheduler {
#endif

#if DEBUG_SCHEDULER
task.ExecuteEnd = DateTime.UtcNow;
FireEvent( TaskExecuted, task );
#endif
}
Expand All @@ -162,11 +171,9 @@ public static class Scheduler {
FireEvent( TaskAdded, task );
if( Tasks.Add( task ) ) {
UpdateCache();
Logger.Log( LogType.Debug,
"Scheduler.AddTask: Added {0}", task );
}else{
Logger.Log( LogType.Debug,
"Scheduler.AddTask: Added duplicate {0}", task );
Logger.Log( LogType.Debug, "Scheduler.AddTask: Added {0}", task );
} else {
Logger.Log( LogType.Debug, "Scheduler.AddTask: Added duplicate {0}", task );
}
#else
if( Tasks.Add( task ) ) {
Expand Down Expand Up @@ -282,6 +289,10 @@ public static class Scheduler {
lock( TaskListLock ) {
foreach( SchedulerTask task in Tasks ) {
player.Message( task.ToString() );
#if DEBUG_SCHEDULER
TimeSpan delta = task.ExecuteEnd - task.ExecuteStart;
player.Message( " Exceution time: {0} milliseconds", delta.TotalMilliseconds);
#endif
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion fCraft/System/SchedulerTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ internal SchedulerTask( [NotNull] SchedulerCallback callback, bool isBackground,
/// can be used for anything you want. </summary>
public object UserState { get; set; }


#if DEBUG_SCHEDULER
public DateTime ExecuteStart, ExecuteEnd;
#endif
#region Run Once

/// <summary> Runs the task once, as quickly as possible.
Expand Down

0 comments on commit f5004f2

Please sign in to comment.