| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net.metrics; |
| 18 |
|
| 19 |
import java.util.Arrays; |
| 20 |
import java.util.BitSet; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
final public class DnsEvent { |
| 27 |
|
| 28 |
private static final int SIZE_LIMIT = 20000; |
| 29 |
|
| 30 |
|
| 31 |
public final int netId; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public final long transports; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public int eventCount; |
| 40 |
|
| 41 |
public int successCount; |
| 42 |
|
| 43 |
public byte[] eventTypes; |
| 44 |
|
| 45 |
|
| 46 |
public byte[] returnCodes; |
| 47 |
|
| 48 |
public int[] latenciesMs; |
| 49 |
|
| 50 |
public DnsEvent(int netId, long transports, int initialCapacity) { |
| 51 |
this.netId = netId; |
| 52 |
this.transports = transports; |
| 53 |
eventTypes = new byte[initialCapacity]; |
| 54 |
returnCodes = new byte[initialCapacity]; |
| 55 |
latenciesMs = new int[initialCapacity]; |
| 56 |
} |
| 57 |
|
| 58 |
boolean addResult(byte eventType, byte returnCode, int latencyMs) { |
| 59 |
boolean isSuccess = (returnCode == 0); |
| 60 |
if (eventCount >= SIZE_LIMIT) { |
| 61 |
|
| 62 |
return isSuccess; |
| 63 |
} |
| 64 |
if (eventCount == eventTypes.length) { |
| 65 |
resize((int) (1.4 * eventCount)); |
| 66 |
} |
| 67 |
eventTypes[eventCount] = eventType; |
| 68 |
returnCodes[eventCount] = returnCode; |
| 69 |
latenciesMs[eventCount] = latencyMs; |
| 70 |
eventCount++; |
| 71 |
if (isSuccess) { |
| 72 |
successCount++; |
| 73 |
} |
| 74 |
return isSuccess; |
| 75 |
} |
| 76 |
|
| 77 |
public void resize(int newLength) { |
| 78 |
eventTypes = Arrays.copyOf(eventTypes, newLength); |
| 79 |
returnCodes = Arrays.copyOf(returnCodes, newLength); |
| 80 |
latenciesMs = Arrays.copyOf(latenciesMs, newLength); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public String toString() { |
| 85 |
StringBuilder builder = |
| 86 |
new StringBuilder("DnsEvent(").append("netId=").append(netId) |
| 87 |
.append(", transports=") |
| 88 |
.append(BitSet.valueOf(new long[] { transports })) |
| 89 |
.append(", "); |
| 90 |
builder.append(String.format("%d events, ", eventCount)); |
| 91 |
builder.append(String.format("%d success)", successCount)); |
| 92 |
return builder.toString(); |
| 93 |
} |
| 94 |
} |