public virtual void Play() { Stopwatch sw = null; if (BenchmarkManager.Enable) { sw = Stopwatch.StartNew(); } Environment.ResetMoveZone(); SpellSelector.AnalysePossibilities(); if (!Fighter.Fight.AIDebugMode) { ExecuteSpellCast(); ExecutePostMove(); } if (sw != null) { sw.Stop(); if (sw.ElapsedMilliseconds > 50) { BenchmarkManager.Instance.Add(BenchmarkEntry.Create("[AI] " + Fighter, sw.Elapsed, "type", "ai", "spells", SpellSelector.Possibilities.Select(x => x.Spell.ToString()).ToCSV(","))); } } }
public override void Execute() { try { Stopwatch stopwatch = Stopwatch.StartNew(); base.Execute(); base.Parameter2.LastMessage = base.Parameter3; stopwatch.Stop(); if (BenchmarkManager.Enable) { Singleton <BenchmarkManager> .Instance.RegisterEntry(BenchmarkEntry.Create(stopwatch.Elapsed, base.Parameter3)); } } catch (Exception ex) { HandledMessage <T> .logger.Error <Stump.DofusProtocol.Messages.Message, T, Exception>("[Handler : {0}] Force disconnection of client {1} : {2}", base.Parameter3, base.Parameter2, ex); T parameter = base.Parameter2; parameter.Disconnect(); Singleton <ExceptionManager> .Instance.RegisterException(ex); } }
private void UpdateCallback(object state) { if ((IsDisposed || !IsRunning) || (Interlocked.CompareExchange(ref m_currentThreadId, Thread.CurrentThread.ManagedThreadId, 0) != 0)) { logger.Info($"Area {this} exit callback since it's disposed"); return; } var updateStart = DateTime.Now; var updateDelta = (int)((updateStart - m_lastUpdateTime).TotalMilliseconds); long messageProcessTime = 0; long timerProcessingTime = 0; var timerProcessed = 0; var processedMessages = new List <BenchmarkEntry>(); try { var sw = Stopwatch.StartNew(); IMessage msg; while (m_messageQueue.TryDequeue(out msg)) { var swMsg = Stopwatch.StartNew(); try { msg.Execute(); swMsg.Stop(); if (BenchmarkManager.Enable && swMsg.Elapsed.TotalMilliseconds > 50) { processedMessages.Add(BenchmarkEntry.Create(msg.ToString(), swMsg.Elapsed, "area", Id)); } } catch (Exception ex) { swMsg.Stop(); logger.Error("Exception raised when processing Message in {0} : {1}.", this, ex); if (BenchmarkManager.Enable) { processedMessages.Add(BenchmarkEntry.Create(msg.ToString(), swMsg.Elapsed, "area", Id, "exception", ex)); } } } sw.Stop(); messageProcessTime = sw.ElapsedMilliseconds; m_isUpdating = true; foreach (var timer in m_pausedTimers.Where(timer => timer.Enabled)) { m_timers.Push(timer); } sw = Stopwatch.StartNew(); TimedTimerEntry peek; while ((peek = m_timers.Peek()) != null && peek.NextTick <= DateTime.Now) { var timer = m_timers.Pop(); if (!timer.Enabled) { if (!timer.IsDisposed) { m_pausedTimers.Add(timer); } } else { try { var swMsg = Stopwatch.StartNew(); timer.Trigger(); swMsg.Stop(); if (BenchmarkManager.Enable && swMsg.Elapsed.TotalMilliseconds > 20) { processedMessages.Add(BenchmarkEntry.Create(timer.ToString(), swMsg.Elapsed, "area", Id)); } if (timer.Enabled) { m_timers.Push(timer); } timerProcessed++; } catch (Exception ex) { logger.Error("Exception raised when processing TimerEntry in {0} : {1}.", this, ex); } } } sw.Stop(); timerProcessingTime = sw.ElapsedMilliseconds; } finally { try { // we updated the map, so set our last update time to now m_lastUpdateTime = updateStart; TickCount++; m_isUpdating = false; // get the time, now that we've finished our update callback var updateEnd = DateTime.Now; var newUpdateDelta = updateEnd - updateStart; // weigh old update-time 9 times and new update-time once AverageUpdateTime = ((AverageUpdateTime * 9) + (float)(newUpdateDelta).TotalMilliseconds) / 10; // make sure to unset the ID *before* enqueuing the task in the ThreadPool again Interlocked.Exchange(ref m_currentThreadId, 0); var callbackTimeout = (int)(m_updateDelay - newUpdateDelta.TotalMilliseconds); if (callbackTimeout < 0) { // even if we are in a hurry: For the sake of load-balance we have to give control back to the ThreadPool callbackTimeout = 0; logger.Debug("Area '{0}' update lagged ({1}ms) (msg:{2}ms, timers:{3}ms, timerProc:{4}/{5})", this, (int)newUpdateDelta.TotalMilliseconds, messageProcessTime, timerProcessingTime, timerProcessed, m_timers.Count); foreach (var msg in processedMessages.OrderByDescending(x => x.Timestamp).Take(15)) { logger.Debug(msg); } BenchmarkManager.Instance.AddRange(processedMessages.OrderByDescending(x => x.Timestamp).Take(15)); } if (!m_running) { m_stoppedAsync.Set(); } else { m_currentTask = Task.Factory.StartNewDelayed(callbackTimeout, UpdateCallback, this); } } catch (Exception ex) { logger.Error("Area {0}. Could not recall callback !! Exception {1}", this, ex); } } }