NetworkStatsHistoryTest.java revision 434962e44ea93b1c4d216c55f636a435bf54aa54
175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey/* 275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Copyright (C) 2011 The Android Open Source Project 375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * 475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License"); 575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * you may not use this file except in compliance with the License. 675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * You may obtain a copy of the License at 775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * 875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * http://www.apache.org/licenses/LICENSE-2.0 975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * 1075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Unless required by applicable law or agreed to in writing, software 1175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS, 1275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * See the License for the specific language governing permissions and 1475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * limitations under the License. 1575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey */ 1675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 1775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeypackage android.net; 1875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 1975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.DAY_IN_MILLIS; 2075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.HOUR_IN_MILLIS; 2175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.MINUTE_IN_MILLIS; 2275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.SECOND_IN_MILLIS; 2375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.WEEK_IN_MILLIS; 2475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.YEAR_IN_MILLIS; 2575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 2675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.test.suitebuilder.annotation.SmallTest; 2775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.test.suitebuilder.annotation.Suppress; 2875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.util.Log; 2975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 3075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport junit.framework.TestCase; 3175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 3275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.util.Random; 3375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 3475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey@SmallTest 3575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeypublic class NetworkStatsHistoryTest extends TestCase { 3675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private static final String TAG = "NetworkStatsHistoryTest"; 3775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 3875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private static final long TEST_START = 1194220800000L; 3975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 4075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private NetworkStatsHistory stats; 4175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 4275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey @Override 4375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey protected void tearDown() throws Exception { 4475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey super.tearDown(); 4575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey if (stats != null) { 4675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey assertConsistent(stats); 4775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 4875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 4975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 5075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordSingleBucket() throws Exception { 5175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 52d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 5375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 5475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record data into narrow window to get single bucket 5575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 1024L, 2048L); 5675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 57d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(1, stats.size()); 58434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 1024L, 2048L); 5975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 6075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 6175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordEqualBuckets() throws Exception { 6275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long bucketDuration = HOUR_IN_MILLIS; 63d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(bucketDuration); 6475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 6575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // split equally across two buckets 6675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long recordStart = TEST_START + (bucketDuration / 2); 6775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(recordStart, recordStart + bucketDuration, 1024L, 128L); 6875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 69d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(2, stats.size()); 70434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 512L, 64L); 71434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 512L, 64L); 7275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 7375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 7475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordTouchingBuckets() throws Exception { 7575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = 15 * MINUTE_IN_MILLIS; 76d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 7775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 7875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // split almost completely into middle bucket, but with a few minutes 7975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // overlap into neighboring buckets. total record is 20 minutes. 8075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long recordStart = (TEST_START + BUCKET_SIZE) - MINUTE_IN_MILLIS; 8175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long recordEnd = (TEST_START + (BUCKET_SIZE * 2)) + (MINUTE_IN_MILLIS * 4); 8275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(recordStart, recordEnd, 1000L, 5000L); 8375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 84d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(3, stats.size()); 8575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // first bucket should have (1/20 of value) 86434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 50L, 250L); 8775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // second bucket should have (15/20 of value) 88434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 750L, 3750L); 8975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // final bucket should have (4/20 of value) 90434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 200L, 1000L); 9175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 9275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 9375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordGapBuckets() throws Exception { 9475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 95d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 9675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 9775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record some data today and next week with large gap 9875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long firstStart = TEST_START; 9975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long lastStart = TEST_START + WEEK_IN_MILLIS; 10075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(firstStart, firstStart + SECOND_IN_MILLIS, 128L, 256L); 10175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(lastStart, lastStart + SECOND_IN_MILLIS, 64L, 512L); 10275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 10375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // we should have two buckets, far apart from each other 104d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(2, stats.size()); 105434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 128L, 256L); 106434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 64L, 512L); 10775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 10875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // now record something in middle, spread across two buckets 10975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long middleStart = TEST_START + DAY_IN_MILLIS; 11075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long middleEnd = middleStart + (HOUR_IN_MILLIS * 2); 11175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(middleStart, middleEnd, 2048L, 2048L); 11275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 11375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // now should have four buckets, with new record in middle two buckets 114d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(4, stats.size()); 115434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 128L, 256L); 116434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 1024L, 1024L); 117434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 1024L, 1024L); 118434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 64L, 512L); 11975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 12075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 12175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordOverlapBuckets() throws Exception { 12275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 123d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 12475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 12575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record some data in one bucket, and another overlapping buckets 12675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 256L, 256L); 12775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long midStart = TEST_START + (HOUR_IN_MILLIS / 2); 12875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(midStart, midStart + HOUR_IN_MILLIS, 1024L, 1024L); 12975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 13075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // should have two buckets, with some data mixed together 131d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(2, stats.size()); 132434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 768L, 768L); 133434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 512L, 512L); 13475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 13575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 13619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey public void testRecordEntireGapIdentical() throws Exception { 13719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first, create two separate histories far apart 13819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats1 = new NetworkStatsHistory(HOUR_IN_MILLIS); 13919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats1.recordData(TEST_START, TEST_START + 2 * HOUR_IN_MILLIS, 2000L, 1000L); 14019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 14119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final long TEST_START_2 = TEST_START + DAY_IN_MILLIS; 14219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats2 = new NetworkStatsHistory(HOUR_IN_MILLIS); 14319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats2.recordData(TEST_START_2, TEST_START_2 + 2 * HOUR_IN_MILLIS, 1000L, 500L); 14419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 14519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // combine together with identical bucket size 14619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(HOUR_IN_MILLIS); 14719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats1); 14819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats2); 14919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 15019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first verify that totals match up 151434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 3000L, 1500L); 15219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 15319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // now inspect internal buckets 154434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 1000L, 500L); 155434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 1000L, 500L); 156434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 500L, 250L); 157434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 500L, 250L); 15819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey } 15919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 16019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey public void testRecordEntireOverlapVaryingBuckets() throws Exception { 16119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // create history just over hour bucket boundary 16219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats1 = new NetworkStatsHistory(HOUR_IN_MILLIS); 16319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats1.recordData(TEST_START, TEST_START + MINUTE_IN_MILLIS * 60, 600L, 600L); 16419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 16519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final long TEST_START_2 = TEST_START + MINUTE_IN_MILLIS; 16619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats2 = new NetworkStatsHistory(MINUTE_IN_MILLIS); 16719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats2.recordData(TEST_START_2, TEST_START_2 + MINUTE_IN_MILLIS * 5, 50L, 50L); 16819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 16919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // combine together with minute bucket size 17019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(MINUTE_IN_MILLIS); 17119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats1); 17219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats2); 17319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 17419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first verify that totals match up 175434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 650L, 650L); 17619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 17719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // now inspect internal buckets 178434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 10L, 10L); 179434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 20L, 20L); 180434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 20L, 20L); 181434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 20L, 20L); 182434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 4, 20L, 20L); 183434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 5, 20L, 20L); 184434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 6, 10L, 10L); 18519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 18619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // now combine using 15min buckets 18719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(HOUR_IN_MILLIS / 4); 18819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats1); 18919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats2); 19019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 19119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first verify that totals match up 192434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 650L, 650L); 19319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 19419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // and inspect buckets 195434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 200L, 200L); 196434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 150L, 150L); 197434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 150L, 150L); 198434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 150L, 150L); 19919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey } 20019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 20175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRemove() throws Exception { 20219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(HOUR_IN_MILLIS); 20375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 20475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record some data across 24 buckets 20575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 24L, 24L); 206d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(24, stats.size()); 20775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 20875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing far before buckets; should be no change 20975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START - YEAR_IN_MILLIS); 210d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(24, stats.size()); 21175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 21275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing just moments into first bucket; should be no change 21375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // since that bucket contains data beyond the cutoff 21475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + SECOND_IN_MILLIS); 215d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(24, stats.size()); 21675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 21775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing single bucket 21875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + HOUR_IN_MILLIS); 219d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(23, stats.size()); 22075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 22175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing multiple buckets 22275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + (4 * HOUR_IN_MILLIS)); 223d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(20, stats.size()); 22475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 22575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing all buckets 22675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + YEAR_IN_MILLIS); 227d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(0, stats.size()); 22875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 22975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 23019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey public void testTotalData() throws Exception { 23119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 23219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 23319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 23419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // record uniform data across day 23519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 2400L, 4800L); 23619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 23719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify that total outside range is 0 238434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START - DAY_IN_MILLIS, 0L, 0L); 23919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 24019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify total in first hour 241434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START, TEST_START + HOUR_IN_MILLIS, 100L, 200L); 24219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 24319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify total across 1.5 hours 244434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START, TEST_START + (long) (1.5 * HOUR_IN_MILLIS), 150L, 300L); 24519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 24619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify total beyond end 247434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START + (23 * HOUR_IN_MILLIS), TEST_START + WEEK_IN_MILLIS, 100L, 200L); 24819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 24919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify everything total 250434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 2400L, 4800L); 25119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 25219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey } 25319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 25475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey @Suppress 25575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testFuzzing() throws Exception { 25675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey try { 25775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // fuzzing with random events, looking for crashes 25875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final Random r = new Random(); 25975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey for (int i = 0; i < 500; i++) { 260d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(r.nextLong()); 26175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey for (int j = 0; j < 10000; j++) { 26275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey if (r.nextBoolean()) { 26375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // add range 26475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long start = r.nextLong(); 26575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long end = start + r.nextInt(); 26675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(start, end, r.nextLong(), r.nextLong()); 26775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } else { 26875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // trim something 26975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(r.nextLong()); 27075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 27175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 27275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey assertConsistent(stats); 27375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 27475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } catch (Throwable e) { 27575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey Log.e(TAG, String.valueOf(stats)); 27675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey throw new RuntimeException(e); 27775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 27875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 27975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 28075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private static void assertConsistent(NetworkStatsHistory stats) { 28175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // verify timestamps are monotonic 282d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey long lastStart = Long.MIN_VALUE; 283d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey NetworkStatsHistory.Entry entry = null; 284d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey for (int i = 0; i < stats.size(); i++) { 285d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey entry = stats.getValues(i, entry); 286d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertTrue(lastStart < entry.bucketStart); 287d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey lastStart = entry.bucketStart; 28875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 28975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 29075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 291434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey private static void assertValues( 292d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey NetworkStatsHistory stats, int index, long rxBytes, long txBytes) { 293d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(index, null); 294d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 295d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 29675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 29775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 298434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey private static void assertValues( 299434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey NetworkStatsHistory stats, long start, long end, long rxBytes, long txBytes) { 300434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null); 301434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 302434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 303434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey } 304434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey 30575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey} 306