blob: 5d8d48f158fa8ee0c1ced75523349a195347a5ca [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
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040018import android.support.test.runner.AndroidJUnit4;
Chris Wrenc8673a82016-05-17 17:11:29 -040019import android.test.suitebuilder.annotation.SmallTest;
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040020import org.junit.Before;
21import org.junit.Test;
22import org.junit.runner.RunWith;
Chris Wrenc8673a82016-05-17 17:11:29 -040023
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040024import static org.junit.Assert.assertTrue;
25import static org.junit.Assert.assertFalse;
26
Jason Monk74f5e362017-12-06 08:56:33 -050027import com.android.server.UiServiceTestCase;
28
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040029@SmallTest
30@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050031public class RateEstimatorTest extends UiServiceTestCase {
Chris Wrenc8673a82016-05-17 17:11:29 -040032 private long mTestStartTime;
33 private RateEstimator mEstimator;
34
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040035 @Before
Chris Wrenc8673a82016-05-17 17:11:29 -040036 public void setUp() {
37 mTestStartTime = 1225731600000L;
Tony Makfd303322016-05-24 18:57:50 +010038 mEstimator = new RateEstimator();
Chris Wrenc8673a82016-05-17 17:11:29 -040039 }
40
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040041 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040042 public void testRunningTimeBackwardDoesntExplodeUpdate() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010043 assertUpdateTime(mTestStartTime);
44 assertUpdateTime(mTestStartTime - 1000L);
Chris Wrenc8673a82016-05-17 17:11:29 -040045 }
46
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040047 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040048 public void testRunningTimeBackwardDoesntExplodeGet() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010049 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040050 final float rate = mEstimator.getRate(mTestStartTime - 1000L);
51 assertFalse(Float.isInfinite(rate));
52 assertFalse(Float.isNaN(rate));
53 }
54
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040055 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040056 public void testInstantaneousEventsDontExplodeUpdate() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010057 assertUpdateTime(mTestStartTime);
58 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040059 }
60
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040061 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040062 public void testInstantaneousEventsDontExplodeGet() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010063 assertUpdateTime(mTestStartTime);
64 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040065 final float rate = mEstimator.getRate(mTestStartTime);
66 assertFalse(Float.isInfinite(rate));
67 assertFalse(Float.isNaN(rate));
68 }
69
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040070 @Test
Chris Wren888b7a82016-06-17 15:47:19 -040071 public void testInstantaneousBurstIsEstimatedUnderTwoPercent() throws Exception {
72 assertUpdateTime(mTestStartTime);
73 long eventStart = mTestStartTime + 1000; // start event a long time after initialization
74 long nextEventTime = postEvents(eventStart, 0, 5); // five events at \inf
75 final float rate = mEstimator.getRate(nextEventTime);
76 assertLessThan("Rate", rate, 20f);
77 }
78
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040079 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040080 public void testCompactBurstIsEstimatedUnderTwoPercent() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010081 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040082 long eventStart = mTestStartTime + 1000; // start event a long time after initialization
83 long nextEventTime = postEvents(eventStart, 1, 5); // five events at 1000Hz
84 final float rate = mEstimator.getRate(nextEventTime);
85 assertLessThan("Rate", rate, 20f);
86 }
87
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040088 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040089 public void testSustained1000HzBurstIsEstimatedOverNinetyPercent() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010090 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -040091 long eventStart = mTestStartTime + 1000; // start event a long time after initialization
92 long nextEventTime = postEvents(eventStart, 1, 100); // one hundred events at 1000Hz
93 final float rate = mEstimator.getRate(nextEventTime);
94 assertGreaterThan("Rate", rate, 900f);
95 }
96
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040097 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -040098 public void testSustained100HzBurstIsEstimatedOverNinetyPercent() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +010099 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -0400100 long eventStart = mTestStartTime + 1000; // start event a long time after initialization
101 long nextEventTime = postEvents(eventStart, 10, 100); // one hundred events at 100Hz
102 final float rate = mEstimator.getRate(nextEventTime);
103
104 assertGreaterThan("Rate", rate, 90f);
105 }
106
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400107 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -0400108 public void testRecoverQuicklyAfterSustainedBurst() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +0100109 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -0400110 long eventStart = mTestStartTime + 1000; // start event a long time after initialization
111 long nextEventTime = postEvents(eventStart, 10, 1000); // one hundred events at 100Hz
112 final float rate = mEstimator.getRate(nextEventTime + 5000L); // two seconds later
113 assertLessThan("Rate", rate, 2f);
114 }
115
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400116 @Test
Chris Wrenc8673a82016-05-17 17:11:29 -0400117 public void testEstimateShouldNotOvershoot() throws Exception {
Tony Makfd303322016-05-24 18:57:50 +0100118 assertUpdateTime(mTestStartTime);
Chris Wrenc8673a82016-05-17 17:11:29 -0400119 long eventStart = mTestStartTime + 1000; // start event a long time after initialization
120 long nextEventTime = postEvents(eventStart, 1, 1000); // one thousand events at 1000Hz
121 final float rate = mEstimator.getRate(nextEventTime);
122 assertLessThan("Rate", rate, 1000f);
123 }
124
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400125 @Test
Tony Makfd303322016-05-24 18:57:50 +0100126 public void testGetRateWithoutUpdate() throws Exception {
127 final float rate = mEstimator.getRate(mTestStartTime);
128 assertLessThan("Rate", rate, 0.1f);
129 }
130
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400131 @Test
Chris Wren888b7a82016-06-17 15:47:19 -0400132 public void testGetRateWithOneUpdate() throws Exception {
133 assertUpdateTime(mTestStartTime);
134 final float rate = mEstimator.getRate(mTestStartTime+1);
135 assertLessThan("Rate", rate, 1f);
136 }
137
Chris Wrenc8673a82016-05-17 17:11:29 -0400138 private void assertLessThan(String label, float a, float b) {
Chris Wren888b7a82016-06-17 15:47:19 -0400139 assertTrue(String.format("%s was %f, but should be less than %f", label, a, b), a <= b);
Chris Wrenc8673a82016-05-17 17:11:29 -0400140 }
141
142 private void assertGreaterThan(String label, float a, float b) {
Chris Wren888b7a82016-06-17 15:47:19 -0400143 assertTrue(String.format("%s was %f, but should be more than %f", label, a, b), a >= b);
Chris Wrenc8673a82016-05-17 17:11:29 -0400144 }
145
146 /** @returns the next event time. */
147 private long postEvents(long start, long dt, int num) {
148 long time = start;
149 for (int i = 0; i < num; i++) {
150 mEstimator.update(time);
151 time += dt;
152 }
153 return time;
154 }
Tony Makfd303322016-05-24 18:57:50 +0100155
156 private void assertUpdateTime(long time) {
157 final float rate = mEstimator.update(time);
158 assertFalse(Float.isInfinite(rate));
159 assertFalse(Float.isNaN(rate));
160 }
Chris Wrenc8673a82016-05-17 17:11:29 -0400161}