IEnumerator getCurConsumeTime(TimeConsumer tc) { float preTime = Time.unscaledTime; m_curConsumeTime = 0f; m_curFps = 0; yield return(new WaitUntil(() => getTime(preTime, tc))); }
/// <summary> /// Executes the provided code block and while the code block is running, continually consume from /// the limit result provided one token each minute. This function allows the code to run for the /// first full minute without requesting additional time from the provider. Following that, every /// minute an additional one minute will be requested from the provider. /// </summary> /// <remarks> /// This method exists to support scheduled events, and as such, intercepts any errors raised via the /// provided code and wraps them in a <see cref="ScheduledEventException"/>. If in the future this is /// usable elsewhere, consider refactoring to handle the errors in a different fashion. /// </remarks> public static void Consume( this IIsolatorLimitResultProvider isolatorLimitProvider, ITimeProvider timeProvider, Action code, TimeMonitor timeMonitor ) { var consumer = new TimeConsumer { IsolatorLimitProvider = isolatorLimitProvider, TimeProvider = timeProvider }; timeMonitor.Add(consumer); code(); consumer.Finished = true; }
//StringBuilder sb = new StringBuilder(1024); bool getTime(float preTime, TimeConsumer tc) { float deltaTime = Time.unscaledTime - preTime; //if (TimeConsumer.GPU == tc) { if (deltaTime >= FpsProfilerSetting.startTime && m_curFps < EngineProfiler.FPSDisplay.fps_) { m_curFps = EngineProfiler.FPSDisplay.fps_; } if (deltaTime >= FpsProfilerSetting.endTime) { m_curConsumeTime = 1000 / m_curFps; return(true); } } //else //{ // if (deltaTime >= startTime) // { // fps += EngineProfiler.FPSDisplay.fps_; // count++; // } // if (deltaTime >= endTime) // { // m_curConsumeTime = 1000/(fps / count); // fps = 0f; // count = 0; // return true; // } //} return(false); }
/* * 先隐藏所有,再通过显示个别来测试耗时不准。 * step1. 显示所有东西 * step2. deactive cpu&gpu neck * step3. if 计算gpu耗时 * step3.1 计算gpu片的耗时 * step3.2 active gpu neck,保证当前是gpu瓶颈,计算当前一帧耗时total * step3.3 隐藏某物体,计算当前一帧耗时a,则total-a为该物体的gpu耗时 * step3.4 重复step3.3,计算出其他所有物体的gpu耗时 * step3.5 总耗时total-所有物体的gpu总耗时-gpu片的耗时 = unity自身的耗时 * step4. else 计算cpu耗时 * step4.1 计算cpu脚本的耗时,后面的步骤同上 */ protected IEnumerator profileTimeConsumer(TimeConsumer tc) { bWork = true; m_fpsCaseManager.showAll(true); if (null != m_cpuNeck) { m_cpuNeck.SetActive(false); } if (null != m_gpuNeck) { m_gpuNeck.SetActive(false); } float neckTime = 0.0f; string tcFlag = ""; if (TimeConsumer.GPU == tc) { tcFlag = "gpu"; yield return(calcGpuNeckTime()); neckTime = m_gpuNeckTime; } else { tcFlag = "cpu"; yield return(calcCpuNeckTime()); neckTime = m_cpuNeckTime; } //LogWrapper.LogProf (tcFlag+" consumer (ms):"); string logStr = "显示" + tcFlag + "瓶颈:"; if (TimeConsumer.GPU == tc) { showGpuNeck(true); } else { showCpuNeck(true); } yield return(getCurConsumeTime(tc)); float neckRefTime = curConsumeTime; logStr += "\t" + neckRefTime; //LogWrapper.LogProf (logStr); float totalTime = 0f; if (!FpsProfilerSetting.bOnlyGetGpuCpuNeck) { int iCount = m_fpsCaseManager.fpsCaseDic.Count; //LinkedListNode<H3DPair<FpsCaseManager.FPS_CASE, FpsCase>> fpsCaseNode = m_fpsCaseManager.fpsCaseDic.Begin(); var fpsCaseNode = m_fpsCaseManager.fpsCaseDic.GetEnumerator(); //for (; null != fpsCaseNode; fpsCaseNode = fpsCaseNode.Next) while (fpsCaseNode.MoveNext()) { logStr = "隐藏" + fpsCaseNode.Current.Key + ":"; if (FpsCaseManager.FPS_CASE.FC_AVATAR_OTHER == fpsCaseNode.Current.Key || FpsCaseManager.FPS_CASE.FC_SHEQU_OTHER == fpsCaseNode.Current.Key) { showOther(fpsCaseNode.Current.Key, false); } else { m_fpsCaseManager.show(fpsCaseNode.Current.Key, false); } yield return(getCurConsumeTime(tc)); float curNeckTime = curConsumeTime; float tmpTime = neckRefTime - curNeckTime; if (tmpTime > 0) { totalTime += tmpTime; } logStr += "\t" + curNeckTime + "\t" + tmpTime; //LogWrapper.LogProf(logStr); m_fpsCaseManager.show(fpsCaseNode.Current.Key, true); } } float unityTime = neckRefTime - totalTime - neckTime; if (unityTime < 0) { unityTime = 0f; } //LogWrapper.LogProf("unity:\t" + unityTime); //LogWrapper.LogProf("total:\t" + (unityTime + totalTime) + "ms\t预计帧率: " + (1000 / (unityTime + totalTime))); if (TimeConsumer.GPU == tc) { m_curGpuTime = unityTime; } else if (TimeConsumer.CPU == tc) { m_curCpuTime = unityTime; } m_fpsCaseManager.showAll(true); if (TimeConsumer.GPU == tc) { showGpuNeck(false); } else { showCpuNeck(false); } //搞完gpu耗时,需要继续测完cpu耗时 if (TimeConsumer.GPU == tc) { m_profileCoroutine = profileTimeConsumer(TimeConsumer.CPU); StartCoroutine(m_profileCoroutine); } else { //都搞完了就结束 bWork = false; if (null != endProfilerCallback) { endProfilerCallback(); } } }
protected override void ProcessConsumer(TimeConsumer consumer) { base.ProcessConsumer(consumer); TimeMonitorEvent.Set(); }