local _, P = ... -- Bounded histograms shared by the two explicit measurements. No timers, API -- queries, sorting, or per-frame table allocations in the recording path. local scale, lastBin = 4000, 40000 -- quarter-millisecond bins, through ten seconds function P.NewFrameSample() return {bins={}, frames=0, seconds=0, worstMS=0, over50=0, over100=0, over250=0, hitchEpisodes=0} end function P.AddFrameSample(s, dt) if type(dt) ~= 'number' or dt <= 0 or dt ~= dt or dt == math.huge then return end local bin = math.min(lastBin, math.ceil(dt * scale)) s.bins[bin] = (s.bins[bin] or 0) + 1 s.frames = s.frames + 1; s.seconds = s.seconds + dt local ms = dt * 1000 if ms > s.worstMS then s.worstMS = ms end if ms > 50 then s.over50 = s.over50 + 1 if not s.inHitch then s.hitchEpisodes = s.hitchEpisodes + 1 end end s.inHitch = ms > 50 if ms > 100 then s.over100 = s.over100 + 1 end if ms > 250 then s.over250 = s.over250 + 1 end end function P.FrameSampleSummary(s) local r = {frames=s.frames, seconds=s.seconds, worstMS=s.worstMS, over50=s.over50, over100=s.over100, over250=s.over250, hitchEpisodes=s.hitchEpisodes} if s.frames == 0 then return r end r.meanFPS = s.frames / s.seconds local count = 0 for bin=1,lastBin do count = count + (s.bins[bin] or 0) if not r.p90MS and count >= math.ceil(s.frames * .90) then r.p90MS = bin / 4 end if count >= math.ceil(s.frames * .99) then r.p99MS = bin / 4; break end end -- Mean of the slowest 1% of frames, explicitly different from p99. local remaining, tailMS = math.max(1, math.ceil(s.frames * .01)), 0 local tailCount = remaining for bin=lastBin,1,-1 do local n = math.min(remaining, s.bins[bin] or 0) tailMS = tailMS + n * bin / 4; remaining = remaining - n if remaining == 0 then break end end r.low1FPS = 1000 / (tailMS / tailCount) r.histogramClipped = s.worstMS > 10000 return r end