/// <summary> // 统计最近10秒内的 平均FPS和CPU睡眠时间,反映当前进程状态 /// </summary> /// <param name="frameCount">1秒内的帧数</param> /// <param name="sleepTime">1秒内睡眠时间</param> private FpsAndCpuInfo RecordFPSAndCpuInfo(int frameCount, int sleepTime) { //10秒一个周期 记录平均值 if (_statTime < 10) { _statFrames += frameCount; _statSleepTime += sleepTime; _statTime++; } else { _averageFramesPerSecond = (int)_statFrames / _statTime; _averageSleepTimePerSecond = (int)_statSleepTime / _statTime; FpsAndCpuInfo msg = new FpsAndCpuInfo(); msg.fps = _averageFramesPerSecond; msg.sleepTime = _averageSleepTimePerSecond; Process proc = Process.GetCurrentProcess(); msg.memorySize = (long)(proc.PrivateMemorySize64 / (1024 * 1024)); _statTime = 0; _statFrames = 0; _statSleepTime = 0; return(msg); } return(_msg); }
/// <summary> /// 设置帧结束点 /// </summary> public void SetFrameEnd() { DateTime curTime = DateTime.Now; _curFrameEndTime = curTime; _lastUpdateTime = _curFrameEndTime; _nextUpdateTime = _lastUpdateTime.AddMilliseconds(Math.Max((TimePerFrame - (curTime - _curFrameBeginTime).TotalMilliseconds), 0)); if ((curTime - _flagStartTime).TotalSeconds > 1) { //以1秒为周期进行记录 _msg = RecordFPSAndCpuInfo(_frames, (int)_sleepTimes); _flagStartTime = curTime; _sleepTimes = 0; _frames = 0; } }
/// <summary> /// 初始化 /// </summary> public void Init() { _now = DateTime.Now; _flagStartTime = Now; _lastUpdateTime = Now; _nextUpdateTime = Now; _curFrameBeginTime = Now; _curFrameEndTime = Now; _frames = 0; _sleepTimes = 0; _statTime = 0; _statFrames = 0; _statSleepTime = 0; _averageFramesPerSecond = 0; _averageSleepTimePerSecond = 0; _msg = null; _timePerFrame = ONESECOND / FPS; }
public void ClearFPSAndCpuInfo() { _msg = null; }