blob: 6182ccbc9aa91b1ff1c41406a4bbbbad3a70cbba (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package org.drrickorang.loopback;
/**
* Created by ninatai on 5/12/15.
*/
import android.util.Log;
import org.drrickorang.loopback.LoopbackAudioThread.RecorderRunnable;
import java.util.Arrays;
import java.util.HashMap;
// TODO remember that after one record, set mPreviousTime back to zero -> done in onButtonTest
public class LatencyRecord {
private static long mPreviousTime = 0;
private static long mCurrentTime = 0;
private static final int range = 102; //TODO adjust this value
private static int mMaxLatency = 0;
private static boolean exceedRange = false;
private static int[] mJavaLatency = new int[range];
public static void collectLatency() {
mCurrentTime = System.nanoTime();
// if = 0 it's the first time the thread runs, so don't record the interval
// FIXME discard the first few records
if (mPreviousTime != 0 && mCurrentTime != 0) {
long diffInNano = mCurrentTime - mPreviousTime;
int diffInMilli = (int) Math.ceil(( ((double)diffInNano / 1000000))); // round up
if (diffInMilli > mMaxLatency) {
mMaxLatency = diffInMilli;
}
// from 0 ms to 1000 ms, plus a sum of all instances > 1000ms
if (diffInMilli >= 0 && diffInMilli < (range - 1)) {
mJavaLatency[diffInMilli] += 1;
} else if (diffInMilli >= (range - 1)) {
mJavaLatency[range-1] += 1;
} else if (diffInMilli < 0) {
// throw new IllegalLatencyException("Latency must be >= 0");
errorLog("Having negative Latency.");
}
}
mPreviousTime = mCurrentTime;
}
// Check if max latency exceeds the range of latencies that are going to be displayed on histogram
public static void setExceedRange() {
if (mMaxLatency > (range - 2)) {
exceedRange = true;
} else {
exceedRange = false;
}
}
public static void resetRecord() {
mPreviousTime = 0;
mCurrentTime = 0;
Arrays.fill(mJavaLatency, 0);
mMaxLatency = 0;
}
public static int[] getLatencyArray() {
return mJavaLatency;
}
public static int getMaxLatency() {
return mMaxLatency;
}
private static void errorLog(String msg) {
Log.e("LatencyTracker", msg);
}
private static void log(String msg) {
Log.v("LatencyTracker", msg);
}
public static class IllegalLatencyException extends Exception {
public IllegalLatencyException(String message)
{
super(message);
}
}
}
|