Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.server.notification; |
| 17 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 18 | import static org.junit.Assert.assertFalse; |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 21 | import android.test.suitebuilder.annotation.SmallTest; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 22 | |
| 23 | import androidx.test.runner.AndroidJUnit4; |
| 24 | |
| 25 | import com.android.server.UiServiceTestCase; |
| 26 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 30 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 31 | @SmallTest |
| 32 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 33 | public class RateEstimatorTest extends UiServiceTestCase { |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 34 | private long mTestStartTime; |
| 35 | private RateEstimator mEstimator; |
| 36 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 37 | @Before |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 38 | public void setUp() { |
| 39 | mTestStartTime = 1225731600000L; |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 40 | mEstimator = new RateEstimator(); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 41 | } |
| 42 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 43 | @Test |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 44 | public void testRunningTimeBackwardDoesntExplodeUpdate() throws Exception { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 45 | assertUpdateTime(mTestStartTime); |
| 46 | assertUpdateTime(mTestStartTime - 1000L); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 49 | @Test |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 50 | public void testRunningTimeBackwardDoesntExplodeGet() throws Exception { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 51 | assertUpdateTime(mTestStartTime); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 52 | final float rate = mEstimator.getRate(mTestStartTime - 1000L); |
| 53 | assertFalse(Float.isInfinite(rate)); |
| 54 | assertFalse(Float.isNaN(rate)); |
| 55 | } |
| 56 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 57 | @Test |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 58 | public void testInstantaneousEventsDontExplodeUpdate() throws Exception { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 59 | assertUpdateTime(mTestStartTime); |
| 60 | assertUpdateTime(mTestStartTime); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 63 | @Test |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 64 | public void testInstantaneousEventsDontExplodeGet() throws Exception { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 65 | assertUpdateTime(mTestStartTime); |
| 66 | assertUpdateTime(mTestStartTime); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 67 | final float rate = mEstimator.getRate(mTestStartTime); |
| 68 | assertFalse(Float.isInfinite(rate)); |
| 69 | assertFalse(Float.isNaN(rate)); |
| 70 | } |
| 71 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 72 | @Test |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 73 | public void testInstantaneousBurstIsEstimatedUnderTwoPercent() throws Exception { |
| 74 | assertUpdateTime(mTestStartTime); |
| 75 | long eventStart = mTestStartTime + 1000; // start event a long time after initialization |
| 76 | long nextEventTime = postEvents(eventStart, 0, 5); // five events at \inf |
| 77 | final float rate = mEstimator.getRate(nextEventTime); |
| 78 | assertLessThan("Rate", rate, 20f); |
| 79 | } |
| 80 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 81 | @Test |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 82 | public void testCompactBurstIsEstimatedUnderTwoPercent() throws Exception { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 83 | assertUpdateTime(mTestStartTime); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 84 | long eventStart = mTestStartTime + 1000; // start event a long time after initialization |
| 85 | long nextEventTime = postEvents(eventStart, 1, 5); // five events at 1000Hz |
| 86 | final float rate = mEstimator.getRate(nextEventTime); |
| 87 | assertLessThan("Rate", rate, 20f); |
| 88 | } |
| 89 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 90 | @Test |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 91 | public void testSustained1000HzBurstIsEstimatedOverNinetyPercent() throws Exception { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 92 | assertUpdateTime(mTestStartTime); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 93 | long eventStart = mTestStartTime + 1000; // start event a long time after initialization |
| 94 | long nextEventTime = postEvents(eventStart, 1, 100); // one hundred events at 1000Hz |
| 95 | final float rate = mEstimator.getRate(nextEventTime); |
| 96 | assertGreaterThan("Rate", rate, 900f); |
| 97 | } |
| 98 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 99 | @Test |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 100 | public void testSustained100HzBurstIsEstimatedOverNinetyPercent() throws Exception { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 101 | assertUpdateTime(mTestStartTime); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 102 | long eventStart = mTestStartTime + 1000; // start event a long time after initialization |
| 103 | long nextEventTime = postEvents(eventStart, 10, 100); // one hundred events at 100Hz |
| 104 | final float rate = mEstimator.getRate(nextEventTime); |
| 105 | |
| 106 | assertGreaterThan("Rate", rate, 90f); |
| 107 | } |
| 108 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 109 | @Test |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 110 | public void testRecoverQuicklyAfterSustainedBurst() throws Exception { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 111 | assertUpdateTime(mTestStartTime); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 112 | long eventStart = mTestStartTime + 1000; // start event a long time after initialization |
| 113 | long nextEventTime = postEvents(eventStart, 10, 1000); // one hundred events at 100Hz |
| 114 | final float rate = mEstimator.getRate(nextEventTime + 5000L); // two seconds later |
| 115 | assertLessThan("Rate", rate, 2f); |
| 116 | } |
| 117 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 118 | @Test |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 119 | public void testEstimateShouldNotOvershoot() throws Exception { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 120 | assertUpdateTime(mTestStartTime); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 121 | long eventStart = mTestStartTime + 1000; // start event a long time after initialization |
| 122 | long nextEventTime = postEvents(eventStart, 1, 1000); // one thousand events at 1000Hz |
| 123 | final float rate = mEstimator.getRate(nextEventTime); |
| 124 | assertLessThan("Rate", rate, 1000f); |
| 125 | } |
| 126 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 127 | @Test |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 128 | public void testGetRateWithoutUpdate() throws Exception { |
| 129 | final float rate = mEstimator.getRate(mTestStartTime); |
| 130 | assertLessThan("Rate", rate, 0.1f); |
| 131 | } |
| 132 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 133 | @Test |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 134 | public void testGetRateWithOneUpdate() throws Exception { |
| 135 | assertUpdateTime(mTestStartTime); |
| 136 | final float rate = mEstimator.getRate(mTestStartTime+1); |
| 137 | assertLessThan("Rate", rate, 1f); |
| 138 | } |
| 139 | |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 140 | private void assertLessThan(String label, float a, float b) { |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 141 | assertTrue(String.format("%s was %f, but should be less than %f", label, a, b), a <= b); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private void assertGreaterThan(String label, float a, float b) { |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 145 | assertTrue(String.format("%s was %f, but should be more than %f", label, a, b), a >= b); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** @returns the next event time. */ |
| 149 | private long postEvents(long start, long dt, int num) { |
| 150 | long time = start; |
| 151 | for (int i = 0; i < num; i++) { |
| 152 | mEstimator.update(time); |
| 153 | time += dt; |
| 154 | } |
| 155 | return time; |
| 156 | } |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 157 | |
| 158 | private void assertUpdateTime(long time) { |
| 159 | final float rate = mEstimator.update(time); |
| 160 | assertFalse(Float.isInfinite(rate)); |
| 161 | assertFalse(Float.isNaN(rate)); |
| 162 | } |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 163 | } |