blob: 68aa1dc1bf3b472a75368a7aa482106f82c20505 [file] [log] [blame]
Chris Wrenc8673a82016-05-17 17:11:29 -04001/*
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 */
16package com.android.server.notification;
17
Brett Chabot84151d92019-02-27 15:37:59 -080018import static org.junit.Assert.assertFalse;
19import static org.junit.Assert.assertTrue;
20
Chris Wrenc8673a82016-05-17 17:11:29 -040021import android.test.suitebuilder.annotation.SmallTest;
Brett Chabot84151d92019-02-27 15:37:59 -080022
23import androidx.test.runner.AndroidJUnit4;
24
25import com.android.server.UiServiceTestCase;
26
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040027import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
Chris Wrenc8673a82016-05-17 17:11:29 -040030
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040031@SmallTest
32@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050033public class RateEstimatorTest extends UiServiceTestCase {
Chris Wrenc8673a82016-05-17 17:11:29 -040034 private long mTestStartTime;
35 private RateEstimator mEstimator;
36
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040037 @Before
Chris Wrenc8673a82016-05-17 17:11:29 -040038 public void setUp() {
39 mTestStartTime = 1225731600000L;
Tony Makfd303322016-05-24 18:57:50 +010040 mEstimator = new RateEstimator();
Chris Wrenc8673a82016-05-17 17:11:29 -040041 }
42
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040043 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040044 public void testRunningTimeBackwardDoesntExplodeUpdate() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010045 assertUpdateTime(mTestStartTime);
46 assertUpdateTime(mTestStartTime - 1000L);
Chris Wrenc8673a82016-05-17 17:11:29 -040047 }
48
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040049 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040050 public void testRunningTimeBackwardDoesntExplodeGet() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010051 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040052 final float rate = mEstimator.getRate(mTestStartTime - 1000L);
53 assertFalse(Float.isInfinite(rate));
54 assertFalse(Float.isNaN(rate));
55 }
56
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040057 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040058 public void testInstantaneousEventsDontExplodeUpdate() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010059 assertUpdateTime(mTestStartTime);
60 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040061 }
62
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040063 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040064 public void testInstantaneousEventsDontExplodeGet() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010065 assertUpdateTime(mTestStartTime);
66 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040067 final float rate = mEstimator.getRate(mTestStartTime);
68 assertFalse(Float.isInfinite(rate));
69 assertFalse(Float.isNaN(rate));
70 }
71
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040072 @Test
Chris Wren888b7a82016-06-17 15:47:19 -040073 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 Pitsch0ffe7552016-08-29 10:07:40 -040081 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040082 public void testCompactBurstIsEstimatedUnderTwoPercent() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010083 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040084 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 Pitsch0ffe7552016-08-29 10:07:40 -040090 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040091 public void testSustained1000HzBurstIsEstimatedOverNinetyPercent() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010092 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040093 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 Pitsch0ffe7552016-08-29 10:07:40 -040099 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -0400100 public void testSustained100HzBurstIsEstimatedOverNinetyPercent() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +0100101 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -0400102 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 Pitsch0ffe7552016-08-29 10:07:40 -0400109 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -0400110 public void testRecoverQuicklyAfterSustainedBurst() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +0100111 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -0400112 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 Pitsch0ffe7552016-08-29 10:07:40 -0400118 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -0400119 public void testEstimateShouldNotOvershoot() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +0100120 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -0400121 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 Pitsch0ffe7552016-08-29 10:07:40 -0400127 @Test
Tony Makfd303322016-05-24 18:57:50 +0100128 public void testGetRateWithoutUpdate() throws Exception {
129 final float rate = mEstimator.getRate(mTestStartTime);
130 assertLessThan("Rate", rate, 0.1f);
131 }
132
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400133 @Test
Chris Wren888b7a82016-06-17 15:47:19 -0400134 public void testGetRateWithOneUpdate() throws Exception {
135 assertUpdateTime(mTestStartTime);
136 final float rate = mEstimator.getRate(mTestStartTime+1);
137 assertLessThan("Rate", rate, 1f);
138 }
139
Chris Wrenc8673a82016-05-17 17:11:29 -0400140 private void assertLessThan(String label, float a, float b) {
Chris Wren888b7a82016-06-17 15:47:19 -0400141 assertTrue(String.format("%s was %f, but should be less than %f", label, a, b), a <= b);
Chris Wrenc8673a82016-05-17 17:11:29 -0400142 }
143
144 private void assertGreaterThan(String label, float a, float b) {
Chris Wren888b7a82016-06-17 15:47:19 -0400145 assertTrue(String.format("%s was %f, but should be more than %f", label, a, b), a >= b);
Chris Wrenc8673a82016-05-17 17:11:29 -0400146 }
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 Makfd303322016-05-24 18:57:50 +0100157
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 Wrenc8673a82016-05-17 17:11:29 -0400163}