NetworkStatsHistoryTest.java revision 63d27a9233fed934340231f438493746084a681d
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; 2775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.DAY_IN_MILLIS; 2875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.HOUR_IN_MILLIS; 2975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.MINUTE_IN_MILLIS; 3075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.SECOND_IN_MILLIS; 3175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.WEEK_IN_MILLIS; 3275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.YEAR_IN_MILLIS; 3375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 34a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport android.test.AndroidTestCase; 3575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.test.suitebuilder.annotation.SmallTest; 3675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.test.suitebuilder.annotation.Suppress; 3775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.util.Log; 3875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 39a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport com.android.frameworks.coretests.R; 4075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 4163d27a9233fed934340231f438493746084a681dJeff Sharkeyimport java.io.ByteArrayInputStream; 4263d27a9233fed934340231f438493746084a681dJeff Sharkeyimport java.io.ByteArrayOutputStream; 43a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport java.io.DataInputStream; 4463d27a9233fed934340231f438493746084a681dJeff Sharkeyimport java.io.DataOutputStream; 4575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.util.Random; 4675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 4775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey@SmallTest 48a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeypublic class NetworkStatsHistoryTest extends AndroidTestCase { 4975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private static final String TAG = "NetworkStatsHistoryTest"; 5075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 5175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private static final long TEST_START = 1194220800000L; 5275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 5363d27a9233fed934340231f438493746084a681dJeff Sharkey private static final long KB_IN_BYTES = 1024; 5463d27a9233fed934340231f438493746084a681dJeff Sharkey private static final long MB_IN_BYTES = KB_IN_BYTES * 1024; 5563d27a9233fed934340231f438493746084a681dJeff Sharkey private static final long GB_IN_BYTES = MB_IN_BYTES * 1024; 5663d27a9233fed934340231f438493746084a681dJeff Sharkey 5775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private NetworkStatsHistory stats; 5875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 5975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey @Override 6075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey protected void tearDown() throws Exception { 6175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey super.tearDown(); 6275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey if (stats != null) { 6375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey assertConsistent(stats); 6475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 6575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 6675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 67a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey public void testReadOriginalVersion() throws Exception { 68a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey final DataInputStream in = new DataInputStream( 69a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey getContext().getResources().openRawResource(R.raw.history_v1)); 70a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 71a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey NetworkStatsHistory.Entry entry = null; 72a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey try { 73a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey final NetworkStatsHistory history = new NetworkStatsHistory(in); 74a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(15 * SECOND_IN_MILLIS, history.getBucketDuration()); 75a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 76a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey entry = history.getValues(0, entry); 77a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(29143L, entry.rxBytes); 78a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(6223L, entry.txBytes); 79a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 80a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey entry = history.getValues(history.size() - 1, entry); 81a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(1476L, entry.rxBytes); 82a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(838L, entry.txBytes); 83a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 84a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey entry = history.getValues(Long.MIN_VALUE, Long.MAX_VALUE, entry); 85a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(332401L, entry.rxBytes); 86a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey assertEquals(64314L, entry.txBytes); 87a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 88a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey } finally { 89a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey in.close(); 90a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey } 91a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey } 92a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey 9375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordSingleBucket() throws Exception { 9475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 95d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 9675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 9775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record data into narrow window to get single bucket 9863d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 9963d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L)); 10075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 101d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(1, stats.size()); 10263d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 0, 1024L, 10L, 2048L, 20L, 2L); 10375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 10475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 10575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordEqualBuckets() throws Exception { 10675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long bucketDuration = HOUR_IN_MILLIS; 107d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(bucketDuration); 10875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 10975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // split equally across two buckets 11075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long recordStart = TEST_START + (bucketDuration / 2); 11163d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(recordStart, recordStart + bucketDuration, 11263d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 128L, 2L, 2L)); 11375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 114d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(2, stats.size()); 11563d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 0, 512L, 5L, 64L, 1L, 1L); 11663d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 1, 512L, 5L, 64L, 1L, 1L); 11775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 11875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 11975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordTouchingBuckets() throws Exception { 12075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = 15 * MINUTE_IN_MILLIS; 121d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 12275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 12375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // split almost completely into middle bucket, but with a few minutes 12475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // overlap into neighboring buckets. total record is 20 minutes. 12575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long recordStart = (TEST_START + BUCKET_SIZE) - MINUTE_IN_MILLIS; 12675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long recordEnd = (TEST_START + (BUCKET_SIZE * 2)) + (MINUTE_IN_MILLIS * 4); 12763d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(recordStart, recordEnd, 12863d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1000L, 2000L, 5000L, 10000L, 100L)); 12975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 130d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(3, stats.size()); 13175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // first bucket should have (1/20 of value) 13263d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 0, 50L, 100L, 250L, 500L, 5L); 13375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // second bucket should have (15/20 of value) 13463d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 1, 750L, 1500L, 3750L, 7500L, 75L); 13575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // final bucket should have (4/20 of value) 13663d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 2, 200L, 400L, 1000L, 2000L, 20L); 13775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 13875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 13975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordGapBuckets() throws Exception { 14075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 141d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 14275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 14375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record some data today and next week with large gap 14475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long firstStart = TEST_START; 14575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long lastStart = TEST_START + WEEK_IN_MILLIS; 14663d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(firstStart, firstStart + SECOND_IN_MILLIS, 14763d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(128L, 2L, 256L, 4L, 1L)); 14863d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(lastStart, lastStart + SECOND_IN_MILLIS, 14963d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(64L, 1L, 512L, 8L, 2L)); 15075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 15175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // we should have two buckets, far apart from each other 152d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(2, stats.size()); 15363d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 0, 128L, 2L, 256L, 4L, 1L); 15463d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 1, 64L, 1L, 512L, 8L, 2L); 15575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 15675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // now record something in middle, spread across two buckets 15775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long middleStart = TEST_START + DAY_IN_MILLIS; 15875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long middleEnd = middleStart + (HOUR_IN_MILLIS * 2); 15963d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(middleStart, middleEnd, 16063d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(2048L, 4L, 2048L, 4L, 2L)); 16175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 16275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // now should have four buckets, with new record in middle two buckets 163d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(4, stats.size()); 16463d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 0, 128L, 2L, 256L, 4L, 1L); 16563d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 1, 1024L, 2L, 1024L, 2L, 1L); 16663d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 2, 1024L, 2L, 1024L, 2L, 1L); 16763d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 3, 64L, 1L, 512L, 8L, 2L); 16875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 16975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 17075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRecordOverlapBuckets() throws Exception { 17175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 172d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 17375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 17475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record some data in one bucket, and another overlapping buckets 17563d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 17663d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(256L, 2L, 256L, 2L, 1L)); 17775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long midStart = TEST_START + (HOUR_IN_MILLIS / 2); 17863d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(midStart, midStart + HOUR_IN_MILLIS, 17963d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 1024L, 10L, 10L)); 18075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 18175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // should have two buckets, with some data mixed together 182d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(2, stats.size()); 18363d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 0, 768L, 7L, 768L, 7L, 6L); 18463d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(stats, 1, 512L, 5L, 512L, 5L, 5L); 18575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 18675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 18719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey public void testRecordEntireGapIdentical() throws Exception { 18819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first, create two separate histories far apart 18919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats1 = new NetworkStatsHistory(HOUR_IN_MILLIS); 19019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats1.recordData(TEST_START, TEST_START + 2 * HOUR_IN_MILLIS, 2000L, 1000L); 19119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 19219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final long TEST_START_2 = TEST_START + DAY_IN_MILLIS; 19319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats2 = new NetworkStatsHistory(HOUR_IN_MILLIS); 19419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats2.recordData(TEST_START_2, TEST_START_2 + 2 * HOUR_IN_MILLIS, 1000L, 500L); 19519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 19619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // combine together with identical bucket size 19719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(HOUR_IN_MILLIS); 19819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats1); 19919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats2); 20019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 20119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first verify that totals match up 202434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 3000L, 1500L); 20319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 20419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // now inspect internal buckets 205434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 1000L, 500L); 206434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 1000L, 500L); 207434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 500L, 250L); 208434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 500L, 250L); 20919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey } 21019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 21119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey public void testRecordEntireOverlapVaryingBuckets() throws Exception { 21219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // create history just over hour bucket boundary 21319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats1 = new NetworkStatsHistory(HOUR_IN_MILLIS); 21419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats1.recordData(TEST_START, TEST_START + MINUTE_IN_MILLIS * 60, 600L, 600L); 21519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 21619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final long TEST_START_2 = TEST_START + MINUTE_IN_MILLIS; 21719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final NetworkStatsHistory stats2 = new NetworkStatsHistory(MINUTE_IN_MILLIS); 21819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats2.recordData(TEST_START_2, TEST_START_2 + MINUTE_IN_MILLIS * 5, 50L, 50L); 21919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 22019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // combine together with minute bucket size 22119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(MINUTE_IN_MILLIS); 22219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats1); 22319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats2); 22419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 22519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first verify that totals match up 226434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 650L, 650L); 22719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 22819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // now inspect internal buckets 229434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 10L, 10L); 230434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 20L, 20L); 231434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 20L, 20L); 232434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 20L, 20L); 233434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 4, 20L, 20L); 234434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 5, 20L, 20L); 235434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 6, 10L, 10L); 23619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 23719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // now combine using 15min buckets 23819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(HOUR_IN_MILLIS / 4); 23919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats1); 24019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordEntireHistory(stats2); 24119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 24219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // first verify that totals match up 243434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 650L, 650L); 24419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 24519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // and inspect buckets 246434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 0, 200L, 200L); 247434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 1, 150L, 150L); 248434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 2, 150L, 150L); 249434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, 3, 150L, 150L); 25019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey } 25119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 25275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testRemove() throws Exception { 25319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(HOUR_IN_MILLIS); 25475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 25575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // record some data across 24 buckets 25675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 24L, 24L); 257d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(24, stats.size()); 25875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 25975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing far before buckets; should be no change 26075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START - YEAR_IN_MILLIS); 261d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(24, stats.size()); 26275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 26375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing just moments into first bucket; should be no change 26475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // since that bucket contains data beyond the cutoff 26575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + SECOND_IN_MILLIS); 266d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(24, stats.size()); 26775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 26875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing single bucket 26975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + HOUR_IN_MILLIS); 270d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(23, stats.size()); 27175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 27275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing multiple buckets 27375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + (4 * HOUR_IN_MILLIS)); 274d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(20, stats.size()); 27575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 27675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // try removing all buckets 27775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(TEST_START + YEAR_IN_MILLIS); 278d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals(0, stats.size()); 27975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 28075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 28119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey public void testTotalData() throws Exception { 28219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey final long BUCKET_SIZE = HOUR_IN_MILLIS; 28319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats = new NetworkStatsHistory(BUCKET_SIZE); 28419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 28519862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // record uniform data across day 28619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 2400L, 4800L); 28719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 28819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify that total outside range is 0 289434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START - DAY_IN_MILLIS, 0L, 0L); 29019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 29119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify total in first hour 292434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START, TEST_START + HOUR_IN_MILLIS, 100L, 200L); 29319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 29419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify total across 1.5 hours 295434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START, TEST_START + (long) (1.5 * HOUR_IN_MILLIS), 150L, 300L); 29619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 29719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify total beyond end 298434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START + (23 * HOUR_IN_MILLIS), TEST_START + WEEK_IN_MILLIS, 100L, 200L); 29919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 30019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey // verify everything total 301434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 2400L, 4800L); 30219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 30319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey } 30419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey 30575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey @Suppress 30675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey public void testFuzzing() throws Exception { 30775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey try { 30875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // fuzzing with random events, looking for crashes 30963d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStats.Entry entry = new NetworkStats.Entry(); 31075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final Random r = new Random(); 31175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey for (int i = 0; i < 500; i++) { 312d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey stats = new NetworkStatsHistory(r.nextLong()); 31375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey for (int j = 0; j < 10000; j++) { 31475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey if (r.nextBoolean()) { 31575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // add range 31675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long start = r.nextLong(); 31775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey final long end = start + r.nextInt(); 31863d27a9233fed934340231f438493746084a681dJeff Sharkey entry.rxBytes = nextPositiveLong(r); 31963d27a9233fed934340231f438493746084a681dJeff Sharkey entry.rxPackets = nextPositiveLong(r); 32063d27a9233fed934340231f438493746084a681dJeff Sharkey entry.txBytes = nextPositiveLong(r); 32163d27a9233fed934340231f438493746084a681dJeff Sharkey entry.txPackets = nextPositiveLong(r); 32263d27a9233fed934340231f438493746084a681dJeff Sharkey entry.operations = nextPositiveLong(r); 32363d27a9233fed934340231f438493746084a681dJeff Sharkey stats.recordData(start, end, entry); 32475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } else { 32575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // trim something 32675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey stats.removeBucketsBefore(r.nextLong()); 32775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 32875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 32975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey assertConsistent(stats); 33075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 33175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } catch (Throwable e) { 33275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey Log.e(TAG, String.valueOf(stats)); 33375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey throw new RuntimeException(e); 33475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 33575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 33675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 33763d27a9233fed934340231f438493746084a681dJeff Sharkey private static long nextPositiveLong(Random r) { 33863d27a9233fed934340231f438493746084a681dJeff Sharkey final long value = r.nextLong(); 33963d27a9233fed934340231f438493746084a681dJeff Sharkey return value < 0 ? -value : value; 34063d27a9233fed934340231f438493746084a681dJeff Sharkey } 34163d27a9233fed934340231f438493746084a681dJeff Sharkey 34263d27a9233fed934340231f438493746084a681dJeff Sharkey public void testIgnoreFields() throws Exception { 34363d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory history = new NetworkStatsHistory( 34463d27a9233fed934340231f438493746084a681dJeff Sharkey MINUTE_IN_MILLIS, 0, FIELD_RX_BYTES | FIELD_TX_BYTES); 34563d27a9233fed934340231f438493746084a681dJeff Sharkey 34663d27a9233fed934340231f438493746084a681dJeff Sharkey history.recordData(0, MINUTE_IN_MILLIS, 34763d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 34863d27a9233fed934340231f438493746084a681dJeff Sharkey history.recordData(0, MINUTE_IN_MILLIS * 2, 34963d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(2L, 2L, 2L, 2L, 2L)); 35063d27a9233fed934340231f438493746084a681dJeff Sharkey 35163d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues( 35263d27a9233fed934340231f438493746084a681dJeff Sharkey history, Long.MIN_VALUE, Long.MAX_VALUE, 1026L, UNKNOWN, 2050L, UNKNOWN, UNKNOWN); 35363d27a9233fed934340231f438493746084a681dJeff Sharkey } 35463d27a9233fed934340231f438493746084a681dJeff Sharkey 35563d27a9233fed934340231f438493746084a681dJeff Sharkey public void testIgnoreFieldsRecordIn() throws Exception { 35663d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory full = new NetworkStatsHistory(MINUTE_IN_MILLIS, 0, FIELD_ALL); 35763d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory partial = new NetworkStatsHistory( 35863d27a9233fed934340231f438493746084a681dJeff Sharkey MINUTE_IN_MILLIS, 0, FIELD_RX_PACKETS | FIELD_OPERATIONS); 35963d27a9233fed934340231f438493746084a681dJeff Sharkey 36063d27a9233fed934340231f438493746084a681dJeff Sharkey full.recordData(0, MINUTE_IN_MILLIS, 36163d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 36263d27a9233fed934340231f438493746084a681dJeff Sharkey partial.recordEntireHistory(full); 36363d27a9233fed934340231f438493746084a681dJeff Sharkey 36463d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(partial, Long.MIN_VALUE, Long.MAX_VALUE, UNKNOWN, 10L, UNKNOWN, UNKNOWN, 4L); 36563d27a9233fed934340231f438493746084a681dJeff Sharkey } 36663d27a9233fed934340231f438493746084a681dJeff Sharkey 36763d27a9233fed934340231f438493746084a681dJeff Sharkey public void testIgnoreFieldsRecordOut() throws Exception { 36863d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory full = new NetworkStatsHistory(MINUTE_IN_MILLIS, 0, FIELD_ALL); 36963d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory partial = new NetworkStatsHistory( 37063d27a9233fed934340231f438493746084a681dJeff Sharkey MINUTE_IN_MILLIS, 0, FIELD_RX_PACKETS | FIELD_OPERATIONS); 37163d27a9233fed934340231f438493746084a681dJeff Sharkey 37263d27a9233fed934340231f438493746084a681dJeff Sharkey partial.recordData(0, MINUTE_IN_MILLIS, 37363d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 37463d27a9233fed934340231f438493746084a681dJeff Sharkey full.recordEntireHistory(partial); 37563d27a9233fed934340231f438493746084a681dJeff Sharkey 37663d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(full, Long.MIN_VALUE, Long.MAX_VALUE, 0L, 10L, 0L, 0L, 4L); 37763d27a9233fed934340231f438493746084a681dJeff Sharkey } 37863d27a9233fed934340231f438493746084a681dJeff Sharkey 37963d27a9233fed934340231f438493746084a681dJeff Sharkey public void testSerialize() throws Exception { 38063d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory before = new NetworkStatsHistory(MINUTE_IN_MILLIS, 40, FIELD_ALL); 38163d27a9233fed934340231f438493746084a681dJeff Sharkey before.recordData(0, MINUTE_IN_MILLIS * 4, 38263d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 38363d27a9233fed934340231f438493746084a681dJeff Sharkey before.recordData(DAY_IN_MILLIS, DAY_IN_MILLIS + MINUTE_IN_MILLIS, 38463d27a9233fed934340231f438493746084a681dJeff Sharkey new NetworkStats.Entry(10L, 20L, 30L, 40L, 50L)); 38563d27a9233fed934340231f438493746084a681dJeff Sharkey 38663d27a9233fed934340231f438493746084a681dJeff Sharkey final ByteArrayOutputStream out = new ByteArrayOutputStream(); 38763d27a9233fed934340231f438493746084a681dJeff Sharkey before.writeToStream(new DataOutputStream(out)); 38863d27a9233fed934340231f438493746084a681dJeff Sharkey out.close(); 38963d27a9233fed934340231f438493746084a681dJeff Sharkey 39063d27a9233fed934340231f438493746084a681dJeff Sharkey final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 39163d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory after = new NetworkStatsHistory(new DataInputStream(in)); 39263d27a9233fed934340231f438493746084a681dJeff Sharkey 39363d27a9233fed934340231f438493746084a681dJeff Sharkey // must have identical totals before and after 39463d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(before, Long.MIN_VALUE, Long.MAX_VALUE, 1034L, 30L, 2078L, 60L, 54L); 39563d27a9233fed934340231f438493746084a681dJeff Sharkey assertValues(after, Long.MIN_VALUE, Long.MAX_VALUE, 1034L, 30L, 2078L, 60L, 54L); 39663d27a9233fed934340231f438493746084a681dJeff Sharkey } 39763d27a9233fed934340231f438493746084a681dJeff Sharkey 39863d27a9233fed934340231f438493746084a681dJeff Sharkey public void testVarLong() throws Exception { 39963d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(0L, performVarLong(0L)); 40063d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(-1L, performVarLong(-1L)); 40163d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(1024L, performVarLong(1024L)); 40263d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(-1024L, performVarLong(-1024L)); 40363d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(40 * MB_IN_BYTES, performVarLong(40 * MB_IN_BYTES)); 40463d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(512 * GB_IN_BYTES, performVarLong(512 * GB_IN_BYTES)); 40563d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(Long.MIN_VALUE, performVarLong(Long.MIN_VALUE)); 40663d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(Long.MAX_VALUE, performVarLong(Long.MAX_VALUE)); 40763d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(Long.MIN_VALUE + 40, performVarLong(Long.MIN_VALUE + 40)); 40863d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals(Long.MAX_VALUE - 40, performVarLong(Long.MAX_VALUE - 40)); 40963d27a9233fed934340231f438493746084a681dJeff Sharkey } 41063d27a9233fed934340231f438493746084a681dJeff Sharkey 41163d27a9233fed934340231f438493746084a681dJeff Sharkey private static long performVarLong(long before) throws Exception { 41263d27a9233fed934340231f438493746084a681dJeff Sharkey final ByteArrayOutputStream out = new ByteArrayOutputStream(); 41363d27a9233fed934340231f438493746084a681dJeff Sharkey writeVarLong(new DataOutputStream(out), before); 41463d27a9233fed934340231f438493746084a681dJeff Sharkey 41563d27a9233fed934340231f438493746084a681dJeff Sharkey final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 41663d27a9233fed934340231f438493746084a681dJeff Sharkey return readVarLong(new DataInputStream(in)); 41763d27a9233fed934340231f438493746084a681dJeff Sharkey } 41863d27a9233fed934340231f438493746084a681dJeff Sharkey 41975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private static void assertConsistent(NetworkStatsHistory stats) { 42075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey // verify timestamps are monotonic 421d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey long lastStart = Long.MIN_VALUE; 422d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey NetworkStatsHistory.Entry entry = null; 423d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey for (int i = 0; i < stats.size(); i++) { 424d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey entry = stats.getValues(i, entry); 425d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertTrue(lastStart < entry.bucketStart); 426d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey lastStart = entry.bucketStart; 42775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 42875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 42975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 430434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey private static void assertValues( 431d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey NetworkStatsHistory stats, int index, long rxBytes, long txBytes) { 432d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(index, null); 433d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 434d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 43575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey } 43675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey 437434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey private static void assertValues( 438434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey NetworkStatsHistory stats, long start, long end, long rxBytes, long txBytes) { 439434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null); 440434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 441434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 442434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey } 443434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey 44463d27a9233fed934340231f438493746084a681dJeff Sharkey private static void assertValues(NetworkStatsHistory stats, int index, long rxBytes, 44563d27a9233fed934340231f438493746084a681dJeff Sharkey long rxPackets, long txBytes, long txPackets, long operations) { 44663d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(index, null); 44763d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 44863d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 44963d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 45063d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected txPackets", txPackets, entry.txPackets); 45163d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected operations", operations, entry.operations); 45263d27a9233fed934340231f438493746084a681dJeff Sharkey } 45363d27a9233fed934340231f438493746084a681dJeff Sharkey 45463d27a9233fed934340231f438493746084a681dJeff Sharkey private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes, 45563d27a9233fed934340231f438493746084a681dJeff Sharkey long rxPackets, long txBytes, long txPackets, long operations) { 45663d27a9233fed934340231f438493746084a681dJeff Sharkey final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null); 45763d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 45863d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 45963d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 46063d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected txPackets", txPackets, entry.txPackets); 46163d27a9233fed934340231f438493746084a681dJeff Sharkey assertEquals("unexpected operations", operations, entry.operations); 46263d27a9233fed934340231f438493746084a681dJeff Sharkey } 46375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey} 464