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 1963d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.FIELD_ALL; 2063d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.FIELD_OPERATIONS; 2163d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.FIELD_RX_BYTES; 2263d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.FIELD_RX_PACKETS; 2363d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.FIELD_TX_BYTES; 2463d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.DataStreamUtils.readVarLong; 2563d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.DataStreamUtils.writeVarLong; 2663d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.Entry.UNKNOWN; 27241dde2306202e7655fdf41d5381f2874e47e108Jeff Sharkeyimport static android.net.TrafficStats.GB_IN_BYTES; 28241dde2306202e7655fdf41d5381f2874e47e108Jeff Sharkeyimport static android.net.TrafficStats.MB_IN_BYTES; 2975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.DAY_IN_MILLIS; 3075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.HOUR_IN_MILLIS; 3175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.MINUTE_IN_MILLIS; 3275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.SECOND_IN_MILLIS; 3375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.WEEK_IN_MILLIS; 3475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.YEAR_IN_MILLIS; 3575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 36a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport android.test.AndroidTestCase; 3775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.test.suitebuilder.annotation.SmallTest; 3875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.test.suitebuilder.annotation.Suppress; 3975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.util.Log; 4075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 41a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport com.android.frameworks.coretests.R; 4275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 4363d27a9233fed934340231f438493746084a681dJeff Sharkeyimport java.io.ByteArrayInputStream; 4463d27a9233fed934340231f438493746084a681dJeff Sharkeyimport java.io.ByteArrayOutputStream; 45a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport java.io.DataInputStream; 4663d27a9233fed934340231f438493746084a681dJeff Sharkeyimport java.io.DataOutputStream; 4775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.util.Random; 4875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 4975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey@SmallTest 50a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeypublic class NetworkStatsHistoryTest extends AndroidTestCase { 5175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private static final String TAG = "NetworkStatsHistoryTest"; 5275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 5375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private static final long TEST_START = 1194220800000L; 5475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 5575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private NetworkStatsHistory stats; 5675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 5775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey @Override 5875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey protected void tearDown() throws Exception { 5975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey super.tearDown(); 6075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey if (stats != null) { 6175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey assertConsistent(stats); 6275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 6375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 6475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 65a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey public void testReadOriginalVersion() throws Exception { 66a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey final DataInputStream in = new DataInputStream( 67a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey getContext().getResources().openRawResource(R.raw.history_v1)); 68a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 69a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey NetworkStatsHistory.Entry entry = null; 70a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey try { 71a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey final NetworkStatsHistory history = new NetworkStatsHistory(in); 72a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(15 * SECOND_IN_MILLIS, history.getBucketDuration()); 73a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 74a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey entry = history.getValues(0, entry); 75a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(29143L, entry.rxBytes); 76a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(6223L, entry.txBytes); 77a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 78a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey entry = history.getValues(history.size() - 1, entry); 79a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(1476L, entry.rxBytes); 80a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(838L, entry.txBytes); 81a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 82a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey entry = history.getValues(Long.MIN_VALUE, Long.MAX_VALUE, entry); 83a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(332401L, entry.rxBytes); 84a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(64314L, entry.txBytes); 85a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 86a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey } finally { 87a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey in.close(); 88a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey } 89a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey } 90a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 9175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordSingleBucket() throws Exception { 9275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 93d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 9475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 9575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record data into narrow window to get single bucket 9663d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 9763d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L)); 9875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 99d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(1, stats.size()); 100558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 0, SECOND_IN_MILLIS, 1024L, 10L, 2048L, 20L, 2L); 10175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 10275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 10375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordEqualBuckets() throws Exception { 10475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long bucketDuration = HOUR_IN_MILLIS; 105d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(bucketDuration); 10675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 10775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // split equally across two buckets 10875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long recordStart = TEST_START + (bucketDuration / 2); 10963d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(recordStart, recordStart + bucketDuration, 11063d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 128L, 2L, 2L)); 11175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 112d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(2, stats.size()); 113558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 0, HOUR_IN_MILLIS / 2, 512L, 5L, 64L, 1L, 1L); 114558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 1, HOUR_IN_MILLIS / 2, 512L, 5L, 64L, 1L, 1L); 11575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 11675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 11775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordTouchingBuckets() throws Exception { 11875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = 15 * MINUTE_IN_MILLIS; 119d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 12075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 12175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // split almost completely into middle bucket, but with a few minutes 12275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // overlap into neighboring buckets. total record is 20 minutes. 12375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long recordStart = (TEST_START + BUCKET_SIZE) - MINUTE_IN_MILLIS; 12475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long recordEnd = (TEST_START + (BUCKET_SIZE * 2)) + (MINUTE_IN_MILLIS * 4); 12563d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(recordStart, recordEnd, 12663d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1000L, 2000L, 5000L, 10000L, 100L)); 12775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 128d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(3, stats.size()); 12975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // first bucket should have (1/20 of value) 130558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 0, MINUTE_IN_MILLIS, 50L, 100L, 250L, 500L, 5L); 13175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // second bucket should have (15/20 of value) 132558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 1, 15 * MINUTE_IN_MILLIS, 750L, 1500L, 3750L, 7500L, 75L); 13375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // final bucket should have (4/20 of value) 134558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 2, 4 * MINUTE_IN_MILLIS, 200L, 400L, 1000L, 2000L, 20L); 13575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 13675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 13775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordGapBuckets() throws Exception { 13875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 139d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 14075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 14175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record some data today and next week with large gap 14275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long firstStart = TEST_START; 14375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long lastStart = TEST_START + WEEK_IN_MILLIS; 14463d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(firstStart, firstStart + SECOND_IN_MILLIS, 14563d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(128L, 2L, 256L, 4L, 1L)); 14663d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(lastStart, lastStart + SECOND_IN_MILLIS, 14763d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(64L, 1L, 512L, 8L, 2L)); 14875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 14975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // we should have two buckets, far apart from each other 150d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(2, stats.size()); 151558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 0, SECOND_IN_MILLIS, 128L, 2L, 256L, 4L, 1L); 152558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 1, SECOND_IN_MILLIS, 64L, 1L, 512L, 8L, 2L); 15375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 15475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // now record something in middle, spread across two buckets 15575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long middleStart = TEST_START + DAY_IN_MILLIS; 15675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long middleEnd = middleStart + (HOUR_IN_MILLIS * 2); 15763d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(middleStart, middleEnd, 15863d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(2048L, 4L, 2048L, 4L, 2L)); 15975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 16075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // now should have four buckets, with new record in middle two buckets 161d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(4, stats.size()); 162558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 0, SECOND_IN_MILLIS, 128L, 2L, 256L, 4L, 1L); 163558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 1, HOUR_IN_MILLIS, 1024L, 2L, 1024L, 2L, 1L); 164558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 2, HOUR_IN_MILLIS, 1024L, 2L, 1024L, 2L, 1L); 165558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 3, SECOND_IN_MILLIS, 64L, 1L, 512L, 8L, 2L); 16675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 16775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 16875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordOverlapBuckets() throws Exception { 16975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 170d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 17175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 17275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record some data in one bucket, and another overlapping buckets 17363d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 17463d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(256L, 2L, 256L, 2L, 1L)); 17575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long midStart = TEST_START + (HOUR_IN_MILLIS / 2); 17663d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(midStart, midStart + HOUR_IN_MILLIS, 17763d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 1024L, 10L, 10L)); 17875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 17975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // should have two buckets, with some data mixed together 180d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(2, stats.size()); 181558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 0, SECOND_IN_MILLIS + (HOUR_IN_MILLIS / 2), 768L, 7L, 768L, 7L, 6L); 182558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, 1, (HOUR_IN_MILLIS / 2), 512L, 5L, 512L, 5L, 5L); 18375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 18475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 18519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey public void testRecordEntireGapIdentical() throws Exception { 18619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first, create two separate histories far apart 18719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats1 = new NetworkStatsHistory(HOUR_IN_MILLIS); 18819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats1.recordData(TEST_START, TEST_START + 2 * HOUR_IN_MILLIS, 2000L, 1000L); 18919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 19019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final long TEST_START_2 = TEST_START + DAY_IN_MILLIS; 19119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats2 = new NetworkStatsHistory(HOUR_IN_MILLIS); 19219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats2.recordData(TEST_START_2, TEST_START_2 + 2 * HOUR_IN_MILLIS, 1000L, 500L); 19319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 19419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // combine together with identical bucket size 19519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(HOUR_IN_MILLIS); 19619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats1); 19719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats2); 19819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 19919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first verify that totals match up 200434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 3000L, 1500L); 20119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 20219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // now inspect internal buckets 203434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 1000L, 500L); 204434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 1000L, 500L); 205434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 500L, 250L); 206434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 500L, 250L); 20719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey } 20819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 20919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey public void testRecordEntireOverlapVaryingBuckets() throws Exception { 21019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // create history just over hour bucket boundary 21119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats1 = new NetworkStatsHistory(HOUR_IN_MILLIS); 21219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats1.recordData(TEST_START, TEST_START + MINUTE_IN_MILLIS * 60, 600L, 600L); 21319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 21419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final long TEST_START_2 = TEST_START + MINUTE_IN_MILLIS; 21519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats2 = new NetworkStatsHistory(MINUTE_IN_MILLIS); 21619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats2.recordData(TEST_START_2, TEST_START_2 + MINUTE_IN_MILLIS * 5, 50L, 50L); 21719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 21819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // combine together with minute bucket size 21919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(MINUTE_IN_MILLIS); 22019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats1); 22119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats2); 22219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 22319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first verify that totals match up 224434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 650L, 650L); 22519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 22619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // now inspect internal buckets 227434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 10L, 10L); 228434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 20L, 20L); 229434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 20L, 20L); 230434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 20L, 20L); 231434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 4, 20L, 20L); 232434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 5, 20L, 20L); 233434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 6, 10L, 10L); 23419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 23519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // now combine using 15min buckets 23619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(HOUR_IN_MILLIS / 4); 23719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats1); 23819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats2); 23919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 24019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first verify that totals match up 241434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 650L, 650L); 24219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 24319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // and inspect buckets 244434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 200L, 200L); 245434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 150L, 150L); 246434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 150L, 150L); 247434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 150L, 150L); 24819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey } 24919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 25075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRemove() throws Exception { 25119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(HOUR_IN_MILLIS); 25275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 25375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record some data across 24 buckets 25475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 24L, 24L); 255d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(24, stats.size()); 25675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 2577ee8658453fe4efe04781bea9a14368e1d5ff41fJeff Sharkey // try removing invalid data; should be no change 2587ee8658453fe4efe04781bea9a14368e1d5ff41fJeff Sharkey stats.removeBucketsBefore(0 - DAY_IN_MILLIS); 2597ee8658453fe4efe04781bea9a14368e1d5ff41fJeff Sharkey assertEquals(24, stats.size()); 2607ee8658453fe4efe04781bea9a14368e1d5ff41fJeff Sharkey 26175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing far before buckets; should be no change 26275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START - YEAR_IN_MILLIS); 263d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(24, stats.size()); 26475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 26575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing just moments into first bucket; should be no change 26675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // since that bucket contains data beyond the cutoff 26775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + SECOND_IN_MILLIS); 268d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(24, stats.size()); 26975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 27075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing single bucket 27175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + HOUR_IN_MILLIS); 272d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(23, stats.size()); 27375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 27475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing multiple buckets 27575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + (4 * HOUR_IN_MILLIS)); 276d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(20, stats.size()); 27775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 27875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing all buckets 27975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + YEAR_IN_MILLIS); 280d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(0, stats.size()); 28175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 28275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 28319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey public void testTotalData() throws Exception { 28419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 28519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 28619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 28719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // record uniform data across day 28819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 2400L, 4800L); 28919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 29019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify that total outside range is 0 291434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START - DAY_IN_MILLIS, 0L, 0L); 29219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 29319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify total in first hour 294434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START, TEST_START + HOUR_IN_MILLIS, 100L, 200L); 29519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 29619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify total across 1.5 hours 297434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START, TEST_START + (long) (1.5 * HOUR_IN_MILLIS), 150L, 300L); 29819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 29919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify total beyond end 300434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START + (23 * HOUR_IN_MILLIS), TEST_START + WEEK_IN_MILLIS, 100L, 200L); 30119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 30219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify everything total 303434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 2400L, 4800L); 30419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 30519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey } 30619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 30775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey @Suppress 30875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testFuzzing() throws Exception { 30975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey try { 31075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // fuzzing with random events, looking for crashes 31163d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStats.Entry entry = new NetworkStats.Entry(); 31275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final Random r = new Random(); 31375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey for (int i = 0; i < 500; i++) { 314d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(r.nextLong()); 31575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey for (int j = 0; j < 10000; j++) { 31675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey if (r.nextBoolean()) { 31775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // add range 31875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long start = r.nextLong(); 31975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long end = start + r.nextInt(); 32063d27a9233fed934340231f438493746084a681dJeff Sharkey entry.rxBytes = nextPositiveLong(r); 32163d27a9233fed934340231f438493746084a681dJeff Sharkey entry.rxPackets = nextPositiveLong(r); 32263d27a9233fed934340231f438493746084a681dJeff Sharkey entry.txBytes = nextPositiveLong(r); 32363d27a9233fed934340231f438493746084a681dJeff Sharkey entry.txPackets = nextPositiveLong(r); 32463d27a9233fed934340231f438493746084a681dJeff Sharkey entry.operations = nextPositiveLong(r); 32563d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(start, end, entry); 32675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } else { 32775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // trim something 32875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(r.nextLong()); 32975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 33075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 33175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey assertConsistent(stats); 33275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 33375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } catch (Throwable e) { 33475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey Log.e(TAG, String.valueOf(stats)); 33575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey throw new RuntimeException(e); 33675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 33775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 33875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 33963d27a9233fed934340231f438493746084a681dJeff Sharkey private static long nextPositiveLong(Random r) { 34063d27a9233fed934340231f438493746084a681dJeff Sharkey final long value = r.nextLong(); 34163d27a9233fed934340231f438493746084a681dJeff Sharkey return value < 0 ? -value : value; 34263d27a9233fed934340231f438493746084a681dJeff Sharkey } 34363d27a9233fed934340231f438493746084a681dJeff Sharkey 34463d27a9233fed934340231f438493746084a681dJeff Sharkey public void testIgnoreFields() throws Exception { 34563d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory history = new NetworkStatsHistory( 34663d27a9233fed934340231f438493746084a681dJeff Sharkey MINUTE_IN_MILLIS, 0, FIELD_RX_BYTES | FIELD_TX_BYTES); 34763d27a9233fed934340231f438493746084a681dJeff Sharkey 34863d27a9233fed934340231f438493746084a681dJeff Sharkey history.recordData(0, MINUTE_IN_MILLIS, 34963d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 350558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey history.recordData(0, 2 * MINUTE_IN_MILLIS, 35163d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(2L, 2L, 2L, 2L, 2L)); 35263d27a9233fed934340231f438493746084a681dJeff Sharkey 353558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertFullValues(history, UNKNOWN, 1026L, UNKNOWN, 2050L, UNKNOWN, UNKNOWN); 35463d27a9233fed934340231f438493746084a681dJeff Sharkey } 35563d27a9233fed934340231f438493746084a681dJeff Sharkey 35663d27a9233fed934340231f438493746084a681dJeff Sharkey public void testIgnoreFieldsRecordIn() throws Exception { 35763d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory full = new NetworkStatsHistory(MINUTE_IN_MILLIS, 0, FIELD_ALL); 35863d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory partial = new NetworkStatsHistory( 35963d27a9233fed934340231f438493746084a681dJeff Sharkey MINUTE_IN_MILLIS, 0, FIELD_RX_PACKETS | FIELD_OPERATIONS); 36063d27a9233fed934340231f438493746084a681dJeff Sharkey 36163d27a9233fed934340231f438493746084a681dJeff Sharkey full.recordData(0, MINUTE_IN_MILLIS, 36263d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 36363d27a9233fed934340231f438493746084a681dJeff Sharkey partial.recordEntireHistory(full); 36463d27a9233fed934340231f438493746084a681dJeff Sharkey 365558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertFullValues(partial, UNKNOWN, UNKNOWN, 10L, UNKNOWN, UNKNOWN, 4L); 36663d27a9233fed934340231f438493746084a681dJeff Sharkey } 36763d27a9233fed934340231f438493746084a681dJeff Sharkey 36863d27a9233fed934340231f438493746084a681dJeff Sharkey public void testIgnoreFieldsRecordOut() throws Exception { 36963d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory full = new NetworkStatsHistory(MINUTE_IN_MILLIS, 0, FIELD_ALL); 37063d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory partial = new NetworkStatsHistory( 37163d27a9233fed934340231f438493746084a681dJeff Sharkey MINUTE_IN_MILLIS, 0, FIELD_RX_PACKETS | FIELD_OPERATIONS); 37263d27a9233fed934340231f438493746084a681dJeff Sharkey 37363d27a9233fed934340231f438493746084a681dJeff Sharkey partial.recordData(0, MINUTE_IN_MILLIS, 37463d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 37563d27a9233fed934340231f438493746084a681dJeff Sharkey full.recordEntireHistory(partial); 37663d27a9233fed934340231f438493746084a681dJeff Sharkey 377558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertFullValues(full, MINUTE_IN_MILLIS, 0L, 10L, 0L, 0L, 4L); 37863d27a9233fed934340231f438493746084a681dJeff Sharkey } 37963d27a9233fed934340231f438493746084a681dJeff Sharkey 38063d27a9233fed934340231f438493746084a681dJeff Sharkey public void testSerialize() throws Exception { 38163d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory before = new NetworkStatsHistory(MINUTE_IN_MILLIS, 40, FIELD_ALL); 382558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey before.recordData(0, 4 * MINUTE_IN_MILLIS, 38363d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 38463d27a9233fed934340231f438493746084a681dJeff Sharkey before.recordData(DAY_IN_MILLIS, DAY_IN_MILLIS + MINUTE_IN_MILLIS, 38563d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(10L, 20L, 30L, 40L, 50L)); 38663d27a9233fed934340231f438493746084a681dJeff Sharkey 38763d27a9233fed934340231f438493746084a681dJeff Sharkey final ByteArrayOutputStream out = new ByteArrayOutputStream(); 38863d27a9233fed934340231f438493746084a681dJeff Sharkey before.writeToStream(new DataOutputStream(out)); 38963d27a9233fed934340231f438493746084a681dJeff Sharkey out.close(); 39063d27a9233fed934340231f438493746084a681dJeff Sharkey 39163d27a9233fed934340231f438493746084a681dJeff Sharkey final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 39263d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory after = new NetworkStatsHistory(new DataInputStream(in)); 39363d27a9233fed934340231f438493746084a681dJeff Sharkey 39463d27a9233fed934340231f438493746084a681dJeff Sharkey // must have identical totals before and after 395558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertFullValues(before, 5 * MINUTE_IN_MILLIS, 1034L, 30L, 2078L, 60L, 54L); 396558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertFullValues(after, 5 * MINUTE_IN_MILLIS, 1034L, 30L, 2078L, 60L, 54L); 39763d27a9233fed934340231f438493746084a681dJeff Sharkey } 39863d27a9233fed934340231f438493746084a681dJeff Sharkey 39963d27a9233fed934340231f438493746084a681dJeff Sharkey public void testVarLong() throws Exception { 40063d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(0L, performVarLong(0L)); 40163d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(-1L, performVarLong(-1L)); 40263d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(1024L, performVarLong(1024L)); 40363d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(-1024L, performVarLong(-1024L)); 40463d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(40 * MB_IN_BYTES, performVarLong(40 * MB_IN_BYTES)); 40563d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(512 * GB_IN_BYTES, performVarLong(512 * GB_IN_BYTES)); 40663d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(Long.MIN_VALUE, performVarLong(Long.MIN_VALUE)); 40763d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(Long.MAX_VALUE, performVarLong(Long.MAX_VALUE)); 40863d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(Long.MIN_VALUE + 40, performVarLong(Long.MIN_VALUE + 40)); 40963d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(Long.MAX_VALUE - 40, performVarLong(Long.MAX_VALUE - 40)); 41063d27a9233fed934340231f438493746084a681dJeff Sharkey } 41163d27a9233fed934340231f438493746084a681dJeff Sharkey 41269b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey public void testIndexBeforeAfter() throws Exception { 41369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 41469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 41569b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey 41669b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey final long FIRST_START = TEST_START; 41769b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey final long FIRST_END = FIRST_START + (2 * HOUR_IN_MILLIS); 41869b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey final long SECOND_START = TEST_START + WEEK_IN_MILLIS; 41969b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey final long SECOND_END = SECOND_START + HOUR_IN_MILLIS; 42069b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey final long THIRD_START = TEST_START + (2 * WEEK_IN_MILLIS); 42169b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey final long THIRD_END = THIRD_START + (2 * HOUR_IN_MILLIS); 42269b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey 42369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey stats.recordData(FIRST_START, FIRST_END, 42469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L)); 42569b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey stats.recordData(SECOND_START, SECOND_END, 42669b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L)); 42769b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey stats.recordData(THIRD_START, THIRD_END, 42869b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L)); 42969b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey 43069b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey // should have buckets: 2+1+2 43169b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertEquals(5, stats.size()); 43269b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey 43369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 0, 0, Long.MIN_VALUE); 43469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 0, 1, FIRST_START); 43569b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 0, 1, FIRST_START + MINUTE_IN_MILLIS); 43669b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 0, 2, FIRST_START + HOUR_IN_MILLIS); 43769b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 1, 2, FIRST_START + HOUR_IN_MILLIS + MINUTE_IN_MILLIS); 43869b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 1, 2, FIRST_END - MINUTE_IN_MILLIS); 43969b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 1, 2, FIRST_END); 44069b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 1, 2, FIRST_END + MINUTE_IN_MILLIS); 44169b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 1, 2, SECOND_START - MINUTE_IN_MILLIS); 44269b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 1, 3, SECOND_START); 44369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 2, 3, SECOND_END); 44469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 2, 3, SECOND_END + MINUTE_IN_MILLIS); 44569b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 2, 3, THIRD_START - MINUTE_IN_MILLIS); 44669b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 2, 4, THIRD_START); 44769b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 3, 4, THIRD_START + MINUTE_IN_MILLIS); 44869b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 3, 4, THIRD_START + HOUR_IN_MILLIS); 44969b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 4, 4, THIRD_END); 45069b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 4, 4, THIRD_END + MINUTE_IN_MILLIS); 45169b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertIndexBeforeAfter(stats, 4, 4, Long.MAX_VALUE); 45269b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey } 45369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey 45469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey private static void assertIndexBeforeAfter( 45569b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey NetworkStatsHistory stats, int before, int after, long time) { 45669b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertEquals("unexpected before", before, stats.getIndexBefore(time)); 45769b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey assertEquals("unexpected after", after, stats.getIndexAfter(time)); 45869b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey } 45969b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey 46063d27a9233fed934340231f438493746084a681dJeff Sharkey private static long performVarLong(long before) throws Exception { 46163d27a9233fed934340231f438493746084a681dJeff Sharkey final ByteArrayOutputStream out = new ByteArrayOutputStream(); 46263d27a9233fed934340231f438493746084a681dJeff Sharkey writeVarLong(new DataOutputStream(out), before); 46363d27a9233fed934340231f438493746084a681dJeff Sharkey 46463d27a9233fed934340231f438493746084a681dJeff Sharkey final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 46563d27a9233fed934340231f438493746084a681dJeff Sharkey return readVarLong(new DataInputStream(in)); 46663d27a9233fed934340231f438493746084a681dJeff Sharkey } 46763d27a9233fed934340231f438493746084a681dJeff Sharkey 46875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private static void assertConsistent(NetworkStatsHistory stats) { 46975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // verify timestamps are monotonic 470d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey long lastStart = Long.MIN_VALUE; 471d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey NetworkStatsHistory.Entry entry = null; 472d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey for (int i = 0; i < stats.size(); i++) { 473d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey entry = stats.getValues(i, entry); 474d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertTrue(lastStart < entry.bucketStart); 475d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey lastStart = entry.bucketStart; 47675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 47775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 47875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 479434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey private static void assertValues( 480d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey NetworkStatsHistory stats, int index, long rxBytes, long txBytes) { 481d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(index, null); 482d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 483d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 48475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 48575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 486434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey private static void assertValues( 487434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey NetworkStatsHistory stats, long start, long end, long rxBytes, long txBytes) { 488434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null); 489434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 490434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 491434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey } 492434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey 493558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey private static void assertValues(NetworkStatsHistory stats, int index, long activeTime, 494558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { 49563d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(index, null); 496558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertEquals("unexpected activeTime", activeTime, entry.activeTime); 49763d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 49863d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 49963d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 50063d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected txPackets", txPackets, entry.txPackets); 50163d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected operations", operations, entry.operations); 50263d27a9233fed934340231f438493746084a681dJeff Sharkey } 50363d27a9233fed934340231f438493746084a681dJeff Sharkey 504558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey private static void assertFullValues(NetworkStatsHistory stats, long activeTime, long rxBytes, 50563d27a9233fed934340231f438493746084a681dJeff Sharkey long rxPackets, long txBytes, long txPackets, long operations) { 506558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertValues(stats, Long.MIN_VALUE, Long.MAX_VALUE, activeTime, rxBytes, rxPackets, txBytes, 507558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey txPackets, operations); 508558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey } 509558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey 510558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey private static void assertValues(NetworkStatsHistory stats, long start, long end, 511558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey long activeTime, long rxBytes, long rxPackets, long txBytes, long txPackets, 512558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey long operations) { 51363d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null); 514558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey assertEquals("unexpected activeTime", activeTime, entry.activeTime); 51563d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 51663d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 51763d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 51863d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected txPackets", txPackets, entry.txPackets); 51963d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected operations", operations, entry.operations); 52063d27a9233fed934340231f438493746084a681dJeff Sharkey } 52175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey} 522