| 1 | package com.rox.video; | |
| 2 | ||
| 3 | import java.util.Arrays; | |
| 4 | import java.util.Locale; | |
| 5 | ||
| 6 | /** | |
| 7 | * Frame-to-frame intervals of one stream of events (frames produced, or frames shown), gathered between | |
| 8 | * {@link #takeStats()} calls. The average says whether a stream keeps up, but lag is felt in the spread: | |
| 9 | * one 50ms gap among 16.7ms ones is a visible hitch that barely moves the mean - hence p99, max and a count | |
| 10 | * of {@link #LATE_MS late} gaps. {@link #record} and {@link #takeStats} may be called from different threads. | |
| 11 | */ | |
| 12 | public final class FrameJitter { | |
| 13 | /** A gap this long means at least one display refresh showed the previous frame again: 1.5 frames at 60Hz. */ | |
| 14 | public static final double LATE_MS = 1.5 * 1000 / 60; | |
| 15 | private static final int INITIAL_CAPACITY = 128; | |
| 16 | ||
| 17 | /** What one window of intervals looked like; all zero if there were none. */ | |
| 18 | public record Stats(int count, double meanMs, double p99Ms, double maxMs, int late) { | |
| 19 | /** e.g. {@code 16.7 ms p99 18.2 max 24.1 late 0}. */ | |
| 20 | public String describe(){ | |
| 21 |
1
1. describe : replaced return value with "" for com/rox/video/FrameJitter$Stats::describe → KILLED |
return String.format(Locale.ROOT, "%4.1f ms p99 %4.1f max %4.1f late %d", meanMs, p99Ms, maxMs, late); |
| 22 | } | |
| 23 | } | |
| 24 | ||
| 25 | private long previousNanos; | |
| 26 | private boolean started; | |
| 27 | private double[] intervalsMs = new double[INITIAL_CAPACITY]; | |
| 28 | private int count; | |
| 29 | ||
| 30 | /** Notes an event at {@code nanos} (a {@link System#nanoTime()} reading); the first only sets a baseline. */ | |
| 31 | public synchronized void record(final long nanos){ | |
| 32 |
1
1. record : negated conditional → KILLED |
if (started){ |
| 33 |
1
1. record : negated conditional → KILLED |
if (count == intervalsMs.length){ |
| 34 |
1
1. record : Replaced integer multiplication with division → KILLED |
intervalsMs = Arrays.copyOf(intervalsMs, count * 2); |
| 35 | } | |
| 36 |
3
1. record : Replaced double division with multiplication → KILLED 2. record : Replaced integer addition with subtraction → KILLED 3. record : Replaced long subtraction with addition → KILLED |
intervalsMs[count++] = (nanos - previousNanos) / 1e6; |
| 37 | } | |
| 38 | previousNanos = nanos; | |
| 39 | started = true; | |
| 40 | } | |
| 41 | ||
| 42 | /** The intervals recorded since the last call, which are then forgotten (the baseline is kept). */ | |
| 43 | public synchronized Stats takeStats(){ | |
| 44 |
1
1. takeStats : negated conditional → KILLED |
if (count == 0){ |
| 45 |
1
1. takeStats : replaced return value with null for com/rox/video/FrameJitter::takeStats → KILLED |
return new Stats(0, 0, 0, 0, 0); |
| 46 | } | |
| 47 | final double[] sorted = Arrays.copyOf(intervalsMs, count); | |
| 48 |
1
1. takeStats : removed call to java/util/Arrays::sort → KILLED |
Arrays.sort(sorted); |
| 49 | double sum = 0; | |
| 50 | int late = 0; | |
| 51 | for (final double ms : sorted){ | |
| 52 |
1
1. takeStats : Replaced double addition with subtraction → KILLED |
sum += ms; |
| 53 |
2
1. takeStats : changed conditional boundary → KILLED 2. takeStats : negated conditional → KILLED |
if (ms > LATE_MS){ |
| 54 |
1
1. takeStats : Changed increment from 1 to -1 → KILLED |
late++; |
| 55 | } | |
| 56 | } | |
| 57 |
3
1. takeStats : Replaced double division with multiplication → KILLED 2. takeStats : Replaced integer subtraction with addition → KILLED 3. takeStats : Replaced double multiplication with division → KILLED |
final Stats stats = new Stats(count, sum / count, sorted[(int) (0.99 * count)], |
| 58 | sorted[count - 1], late); | |
| 59 | count = 0; | |
| 60 |
1
1. takeStats : replaced return value with null for com/rox/video/FrameJitter::takeStats → KILLED |
return stats; |
| 61 | } | |
| 62 | } | |
Mutations | ||
| 21 |
1.1 |
|
| 32 |
1.1 |
|
| 33 |
1.1 |
|
| 34 |
1.1 |
|
| 36 |
1.1 2.2 3.3 |
|
| 44 |
1.1 |
|
| 45 |
1.1 |
|
| 48 |
1.1 |
|
| 52 |
1.1 |
|
| 53 |
1.1 2.2 |
|
| 54 |
1.1 |
|
| 57 |
1.1 2.2 3.3 |
|
| 60 |
1.1 |