asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | import java.util.HashMap; |
| 14 | import java.util.Map; |
| 15 | |
Magnus Jedvert | 9060eb1 | 2017-12-12 12:52:54 +0100 | [diff] [blame] | 16 | // Java-side of androidmetrics.cc |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 17 | // |
| 18 | // Rtc histograms can be queried through the API, getAndReset(). |
| 19 | // The returned map holds the name of a histogram and its samples. |
| 20 | // |
| 21 | // Example of |map| with one histogram: |
| 22 | // |name|: "WebRTC.Video.InputFramesPerSecond" |
| 23 | // |min|: 1 |
| 24 | // |max|: 100 |
| 25 | // |bucketCount|: 50 |
| 26 | // |samples|: [30]:1 |
| 27 | // |
| 28 | // Most histograms are not updated frequently (e.g. most video metrics are an |
| 29 | // average over the call and recorded when a stream is removed). |
| 30 | // The metrics can for example be retrieved when a peer connection is closed. |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 31 | public class Metrics { |
sakal | 2a5f371 | 2016-09-09 00:11:48 -0700 | [diff] [blame] | 32 | private static final String TAG = "Metrics"; |
| 33 | |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 34 | public final Map<String, HistogramInfo> map = |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 35 | new HashMap<String, HistogramInfo>(); // <name, HistogramInfo> |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 36 | |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 37 | @CalledByNative |
| 38 | Metrics() {} |
| 39 | |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Class holding histogram information. |
| 42 | */ |
| 43 | public static class HistogramInfo { |
| 44 | public final int min; |
| 45 | public final int max; |
| 46 | public final int bucketCount; |
| 47 | public final Map<Integer, Integer> samples = |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 48 | new HashMap<Integer, Integer>(); // <value, # of events> |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 49 | |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 50 | @CalledByNative("HistogramInfo") |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 51 | public HistogramInfo(int min, int max, int bucketCount) { |
| 52 | this.min = min; |
| 53 | this.max = max; |
| 54 | this.bucketCount = bucketCount; |
| 55 | } |
| 56 | |
Magnus Jedvert | 986a79c | 2017-11-18 16:45:18 +0100 | [diff] [blame] | 57 | @CalledByNative("HistogramInfo") |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 58 | public void addSample(int value, int numEvents) { |
| 59 | samples.put(value, numEvents); |
| 60 | } |
| 61 | } |
| 62 | |
Magnus Jedvert | 986a79c | 2017-11-18 16:45:18 +0100 | [diff] [blame] | 63 | @CalledByNative |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 64 | private void add(String name, HistogramInfo info) { |
| 65 | map.put(name, info); |
| 66 | } |
| 67 | |
| 68 | // Enables gathering of metrics (which can be fetched with getAndReset()). |
| 69 | // Must be called before PeerConnectionFactory is created. |
| 70 | public static void enable() { |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 71 | nativeEnable(); |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Gets and clears native histograms. |
| 75 | public static Metrics getAndReset() { |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 76 | return nativeGetAndReset(); |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 79 | private static native void nativeEnable(); |
| 80 | private static native Metrics nativeGetAndReset(); |
asapersson | b432b26 | 2016-05-23 06:49:36 -0700 | [diff] [blame] | 81 | } |