NetworkStatsHistoryTest.java revision 69b0f63af2e3babc2e9f048c4682032a0c17d9d0
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.net; 18 19import static android.net.NetworkStatsHistory.FIELD_ALL; 20import static android.net.NetworkStatsHistory.FIELD_OPERATIONS; 21import static android.net.NetworkStatsHistory.FIELD_RX_BYTES; 22import static android.net.NetworkStatsHistory.FIELD_RX_PACKETS; 23import static android.net.NetworkStatsHistory.FIELD_TX_BYTES; 24import static android.net.NetworkStatsHistory.DataStreamUtils.readVarLong; 25import static android.net.NetworkStatsHistory.DataStreamUtils.writeVarLong; 26import static android.net.NetworkStatsHistory.Entry.UNKNOWN; 27import static android.text.format.DateUtils.DAY_IN_MILLIS; 28import static android.text.format.DateUtils.HOUR_IN_MILLIS; 29import static android.text.format.DateUtils.MINUTE_IN_MILLIS; 30import static android.text.format.DateUtils.SECOND_IN_MILLIS; 31import static android.text.format.DateUtils.WEEK_IN_MILLIS; 32import static android.text.format.DateUtils.YEAR_IN_MILLIS; 33 34import android.test.AndroidTestCase; 35import android.test.suitebuilder.annotation.SmallTest; 36import android.test.suitebuilder.annotation.Suppress; 37import android.util.Log; 38 39import com.android.frameworks.coretests.R; 40 41import java.io.ByteArrayInputStream; 42import java.io.ByteArrayOutputStream; 43import java.io.DataInputStream; 44import java.io.DataOutputStream; 45import java.util.Random; 46 47@SmallTest 48public class NetworkStatsHistoryTest extends AndroidTestCase { 49 private static final String TAG = "NetworkStatsHistoryTest"; 50 51 private static final long TEST_START = 1194220800000L; 52 53 private static final long KB_IN_BYTES = 1024; 54 private static final long MB_IN_BYTES = KB_IN_BYTES * 1024; 55 private static final long GB_IN_BYTES = MB_IN_BYTES * 1024; 56 57 private NetworkStatsHistory stats; 58 59 @Override 60 protected void tearDown() throws Exception { 61 super.tearDown(); 62 if (stats != null) { 63 assertConsistent(stats); 64 } 65 } 66 67 public void testReadOriginalVersion() throws Exception { 68 final DataInputStream in = new DataInputStream( 69 getContext().getResources().openRawResource(R.raw.history_v1)); 70 71 NetworkStatsHistory.Entry entry = null; 72 try { 73 final NetworkStatsHistory history = new NetworkStatsHistory(in); 74 assertEquals(15 * SECOND_IN_MILLIS, history.getBucketDuration()); 75 76 entry = history.getValues(0, entry); 77 assertEquals(29143L, entry.rxBytes); 78 assertEquals(6223L, entry.txBytes); 79 80 entry = history.getValues(history.size() - 1, entry); 81 assertEquals(1476L, entry.rxBytes); 82 assertEquals(838L, entry.txBytes); 83 84 entry = history.getValues(Long.MIN_VALUE, Long.MAX_VALUE, entry); 85 assertEquals(332401L, entry.rxBytes); 86 assertEquals(64314L, entry.txBytes); 87 88 } finally { 89 in.close(); 90 } 91 } 92 93 public void testRecordSingleBucket() throws Exception { 94 final long BUCKET_SIZE = HOUR_IN_MILLIS; 95 stats = new NetworkStatsHistory(BUCKET_SIZE); 96 97 // record data into narrow window to get single bucket 98 stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 99 new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L)); 100 101 assertEquals(1, stats.size()); 102 assertValues(stats, 0, SECOND_IN_MILLIS, 1024L, 10L, 2048L, 20L, 2L); 103 } 104 105 public void testRecordEqualBuckets() throws Exception { 106 final long bucketDuration = HOUR_IN_MILLIS; 107 stats = new NetworkStatsHistory(bucketDuration); 108 109 // split equally across two buckets 110 final long recordStart = TEST_START + (bucketDuration / 2); 111 stats.recordData(recordStart, recordStart + bucketDuration, 112 new NetworkStats.Entry(1024L, 10L, 128L, 2L, 2L)); 113 114 assertEquals(2, stats.size()); 115 assertValues(stats, 0, HOUR_IN_MILLIS / 2, 512L, 5L, 64L, 1L, 1L); 116 assertValues(stats, 1, HOUR_IN_MILLIS / 2, 512L, 5L, 64L, 1L, 1L); 117 } 118 119 public void testRecordTouchingBuckets() throws Exception { 120 final long BUCKET_SIZE = 15 * MINUTE_IN_MILLIS; 121 stats = new NetworkStatsHistory(BUCKET_SIZE); 122 123 // split almost completely into middle bucket, but with a few minutes 124 // overlap into neighboring buckets. total record is 20 minutes. 125 final long recordStart = (TEST_START + BUCKET_SIZE) - MINUTE_IN_MILLIS; 126 final long recordEnd = (TEST_START + (BUCKET_SIZE * 2)) + (MINUTE_IN_MILLIS * 4); 127 stats.recordData(recordStart, recordEnd, 128 new NetworkStats.Entry(1000L, 2000L, 5000L, 10000L, 100L)); 129 130 assertEquals(3, stats.size()); 131 // first bucket should have (1/20 of value) 132 assertValues(stats, 0, MINUTE_IN_MILLIS, 50L, 100L, 250L, 500L, 5L); 133 // second bucket should have (15/20 of value) 134 assertValues(stats, 1, 15 * MINUTE_IN_MILLIS, 750L, 1500L, 3750L, 7500L, 75L); 135 // final bucket should have (4/20 of value) 136 assertValues(stats, 2, 4 * MINUTE_IN_MILLIS, 200L, 400L, 1000L, 2000L, 20L); 137 } 138 139 public void testRecordGapBuckets() throws Exception { 140 final long BUCKET_SIZE = HOUR_IN_MILLIS; 141 stats = new NetworkStatsHistory(BUCKET_SIZE); 142 143 // record some data today and next week with large gap 144 final long firstStart = TEST_START; 145 final long lastStart = TEST_START + WEEK_IN_MILLIS; 146 stats.recordData(firstStart, firstStart + SECOND_IN_MILLIS, 147 new NetworkStats.Entry(128L, 2L, 256L, 4L, 1L)); 148 stats.recordData(lastStart, lastStart + SECOND_IN_MILLIS, 149 new NetworkStats.Entry(64L, 1L, 512L, 8L, 2L)); 150 151 // we should have two buckets, far apart from each other 152 assertEquals(2, stats.size()); 153 assertValues(stats, 0, SECOND_IN_MILLIS, 128L, 2L, 256L, 4L, 1L); 154 assertValues(stats, 1, SECOND_IN_MILLIS, 64L, 1L, 512L, 8L, 2L); 155 156 // now record something in middle, spread across two buckets 157 final long middleStart = TEST_START + DAY_IN_MILLIS; 158 final long middleEnd = middleStart + (HOUR_IN_MILLIS * 2); 159 stats.recordData(middleStart, middleEnd, 160 new NetworkStats.Entry(2048L, 4L, 2048L, 4L, 2L)); 161 162 // now should have four buckets, with new record in middle two buckets 163 assertEquals(4, stats.size()); 164 assertValues(stats, 0, SECOND_IN_MILLIS, 128L, 2L, 256L, 4L, 1L); 165 assertValues(stats, 1, HOUR_IN_MILLIS, 1024L, 2L, 1024L, 2L, 1L); 166 assertValues(stats, 2, HOUR_IN_MILLIS, 1024L, 2L, 1024L, 2L, 1L); 167 assertValues(stats, 3, SECOND_IN_MILLIS, 64L, 1L, 512L, 8L, 2L); 168 } 169 170 public void testRecordOverlapBuckets() throws Exception { 171 final long BUCKET_SIZE = HOUR_IN_MILLIS; 172 stats = new NetworkStatsHistory(BUCKET_SIZE); 173 174 // record some data in one bucket, and another overlapping buckets 175 stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 176 new NetworkStats.Entry(256L, 2L, 256L, 2L, 1L)); 177 final long midStart = TEST_START + (HOUR_IN_MILLIS / 2); 178 stats.recordData(midStart, midStart + HOUR_IN_MILLIS, 179 new NetworkStats.Entry(1024L, 10L, 1024L, 10L, 10L)); 180 181 // should have two buckets, with some data mixed together 182 assertEquals(2, stats.size()); 183 assertValues(stats, 0, SECOND_IN_MILLIS + (HOUR_IN_MILLIS / 2), 768L, 7L, 768L, 7L, 6L); 184 assertValues(stats, 1, (HOUR_IN_MILLIS / 2), 512L, 5L, 512L, 5L, 5L); 185 } 186 187 public void testRecordEntireGapIdentical() throws Exception { 188 // first, create two separate histories far apart 189 final NetworkStatsHistory stats1 = new NetworkStatsHistory(HOUR_IN_MILLIS); 190 stats1.recordData(TEST_START, TEST_START + 2 * HOUR_IN_MILLIS, 2000L, 1000L); 191 192 final long TEST_START_2 = TEST_START + DAY_IN_MILLIS; 193 final NetworkStatsHistory stats2 = new NetworkStatsHistory(HOUR_IN_MILLIS); 194 stats2.recordData(TEST_START_2, TEST_START_2 + 2 * HOUR_IN_MILLIS, 1000L, 500L); 195 196 // combine together with identical bucket size 197 stats = new NetworkStatsHistory(HOUR_IN_MILLIS); 198 stats.recordEntireHistory(stats1); 199 stats.recordEntireHistory(stats2); 200 201 // first verify that totals match up 202 assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 3000L, 1500L); 203 204 // now inspect internal buckets 205 assertValues(stats, 0, 1000L, 500L); 206 assertValues(stats, 1, 1000L, 500L); 207 assertValues(stats, 2, 500L, 250L); 208 assertValues(stats, 3, 500L, 250L); 209 } 210 211 public void testRecordEntireOverlapVaryingBuckets() throws Exception { 212 // create history just over hour bucket boundary 213 final NetworkStatsHistory stats1 = new NetworkStatsHistory(HOUR_IN_MILLIS); 214 stats1.recordData(TEST_START, TEST_START + MINUTE_IN_MILLIS * 60, 600L, 600L); 215 216 final long TEST_START_2 = TEST_START + MINUTE_IN_MILLIS; 217 final NetworkStatsHistory stats2 = new NetworkStatsHistory(MINUTE_IN_MILLIS); 218 stats2.recordData(TEST_START_2, TEST_START_2 + MINUTE_IN_MILLIS * 5, 50L, 50L); 219 220 // combine together with minute bucket size 221 stats = new NetworkStatsHistory(MINUTE_IN_MILLIS); 222 stats.recordEntireHistory(stats1); 223 stats.recordEntireHistory(stats2); 224 225 // first verify that totals match up 226 assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 650L, 650L); 227 228 // now inspect internal buckets 229 assertValues(stats, 0, 10L, 10L); 230 assertValues(stats, 1, 20L, 20L); 231 assertValues(stats, 2, 20L, 20L); 232 assertValues(stats, 3, 20L, 20L); 233 assertValues(stats, 4, 20L, 20L); 234 assertValues(stats, 5, 20L, 20L); 235 assertValues(stats, 6, 10L, 10L); 236 237 // now combine using 15min buckets 238 stats = new NetworkStatsHistory(HOUR_IN_MILLIS / 4); 239 stats.recordEntireHistory(stats1); 240 stats.recordEntireHistory(stats2); 241 242 // first verify that totals match up 243 assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 650L, 650L); 244 245 // and inspect buckets 246 assertValues(stats, 0, 200L, 200L); 247 assertValues(stats, 1, 150L, 150L); 248 assertValues(stats, 2, 150L, 150L); 249 assertValues(stats, 3, 150L, 150L); 250 } 251 252 public void testRemove() throws Exception { 253 stats = new NetworkStatsHistory(HOUR_IN_MILLIS); 254 255 // record some data across 24 buckets 256 stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 24L, 24L); 257 assertEquals(24, stats.size()); 258 259 // try removing far before buckets; should be no change 260 stats.removeBucketsBefore(TEST_START - YEAR_IN_MILLIS); 261 assertEquals(24, stats.size()); 262 263 // try removing just moments into first bucket; should be no change 264 // since that bucket contains data beyond the cutoff 265 stats.removeBucketsBefore(TEST_START + SECOND_IN_MILLIS); 266 assertEquals(24, stats.size()); 267 268 // try removing single bucket 269 stats.removeBucketsBefore(TEST_START + HOUR_IN_MILLIS); 270 assertEquals(23, stats.size()); 271 272 // try removing multiple buckets 273 stats.removeBucketsBefore(TEST_START + (4 * HOUR_IN_MILLIS)); 274 assertEquals(20, stats.size()); 275 276 // try removing all buckets 277 stats.removeBucketsBefore(TEST_START + YEAR_IN_MILLIS); 278 assertEquals(0, stats.size()); 279 } 280 281 public void testTotalData() throws Exception { 282 final long BUCKET_SIZE = HOUR_IN_MILLIS; 283 stats = new NetworkStatsHistory(BUCKET_SIZE); 284 285 // record uniform data across day 286 stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 2400L, 4800L); 287 288 // verify that total outside range is 0 289 assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START - DAY_IN_MILLIS, 0L, 0L); 290 291 // verify total in first hour 292 assertValues(stats, TEST_START, TEST_START + HOUR_IN_MILLIS, 100L, 200L); 293 294 // verify total across 1.5 hours 295 assertValues(stats, TEST_START, TEST_START + (long) (1.5 * HOUR_IN_MILLIS), 150L, 300L); 296 297 // verify total beyond end 298 assertValues(stats, TEST_START + (23 * HOUR_IN_MILLIS), TEST_START + WEEK_IN_MILLIS, 100L, 200L); 299 300 // verify everything total 301 assertValues(stats, TEST_START - WEEK_IN_MILLIS, TEST_START + WEEK_IN_MILLIS, 2400L, 4800L); 302 303 } 304 305 @Suppress 306 public void testFuzzing() throws Exception { 307 try { 308 // fuzzing with random events, looking for crashes 309 final NetworkStats.Entry entry = new NetworkStats.Entry(); 310 final Random r = new Random(); 311 for (int i = 0; i < 500; i++) { 312 stats = new NetworkStatsHistory(r.nextLong()); 313 for (int j = 0; j < 10000; j++) { 314 if (r.nextBoolean()) { 315 // add range 316 final long start = r.nextLong(); 317 final long end = start + r.nextInt(); 318 entry.rxBytes = nextPositiveLong(r); 319 entry.rxPackets = nextPositiveLong(r); 320 entry.txBytes = nextPositiveLong(r); 321 entry.txPackets = nextPositiveLong(r); 322 entry.operations = nextPositiveLong(r); 323 stats.recordData(start, end, entry); 324 } else { 325 // trim something 326 stats.removeBucketsBefore(r.nextLong()); 327 } 328 } 329 assertConsistent(stats); 330 } 331 } catch (Throwable e) { 332 Log.e(TAG, String.valueOf(stats)); 333 throw new RuntimeException(e); 334 } 335 } 336 337 private static long nextPositiveLong(Random r) { 338 final long value = r.nextLong(); 339 return value < 0 ? -value : value; 340 } 341 342 public void testIgnoreFields() throws Exception { 343 final NetworkStatsHistory history = new NetworkStatsHistory( 344 MINUTE_IN_MILLIS, 0, FIELD_RX_BYTES | FIELD_TX_BYTES); 345 346 history.recordData(0, MINUTE_IN_MILLIS, 347 new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 348 history.recordData(0, 2 * MINUTE_IN_MILLIS, 349 new NetworkStats.Entry(2L, 2L, 2L, 2L, 2L)); 350 351 assertFullValues(history, UNKNOWN, 1026L, UNKNOWN, 2050L, UNKNOWN, UNKNOWN); 352 } 353 354 public void testIgnoreFieldsRecordIn() throws Exception { 355 final NetworkStatsHistory full = new NetworkStatsHistory(MINUTE_IN_MILLIS, 0, FIELD_ALL); 356 final NetworkStatsHistory partial = new NetworkStatsHistory( 357 MINUTE_IN_MILLIS, 0, FIELD_RX_PACKETS | FIELD_OPERATIONS); 358 359 full.recordData(0, MINUTE_IN_MILLIS, 360 new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 361 partial.recordEntireHistory(full); 362 363 assertFullValues(partial, UNKNOWN, UNKNOWN, 10L, UNKNOWN, UNKNOWN, 4L); 364 } 365 366 public void testIgnoreFieldsRecordOut() throws Exception { 367 final NetworkStatsHistory full = new NetworkStatsHistory(MINUTE_IN_MILLIS, 0, FIELD_ALL); 368 final NetworkStatsHistory partial = new NetworkStatsHistory( 369 MINUTE_IN_MILLIS, 0, FIELD_RX_PACKETS | FIELD_OPERATIONS); 370 371 partial.recordData(0, MINUTE_IN_MILLIS, 372 new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 373 full.recordEntireHistory(partial); 374 375 assertFullValues(full, MINUTE_IN_MILLIS, 0L, 10L, 0L, 0L, 4L); 376 } 377 378 public void testSerialize() throws Exception { 379 final NetworkStatsHistory before = new NetworkStatsHistory(MINUTE_IN_MILLIS, 40, FIELD_ALL); 380 before.recordData(0, 4 * MINUTE_IN_MILLIS, 381 new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 4L)); 382 before.recordData(DAY_IN_MILLIS, DAY_IN_MILLIS + MINUTE_IN_MILLIS, 383 new NetworkStats.Entry(10L, 20L, 30L, 40L, 50L)); 384 385 final ByteArrayOutputStream out = new ByteArrayOutputStream(); 386 before.writeToStream(new DataOutputStream(out)); 387 out.close(); 388 389 final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 390 final NetworkStatsHistory after = new NetworkStatsHistory(new DataInputStream(in)); 391 392 // must have identical totals before and after 393 assertFullValues(before, 5 * MINUTE_IN_MILLIS, 1034L, 30L, 2078L, 60L, 54L); 394 assertFullValues(after, 5 * MINUTE_IN_MILLIS, 1034L, 30L, 2078L, 60L, 54L); 395 } 396 397 public void testVarLong() throws Exception { 398 assertEquals(0L, performVarLong(0L)); 399 assertEquals(-1L, performVarLong(-1L)); 400 assertEquals(1024L, performVarLong(1024L)); 401 assertEquals(-1024L, performVarLong(-1024L)); 402 assertEquals(40 * MB_IN_BYTES, performVarLong(40 * MB_IN_BYTES)); 403 assertEquals(512 * GB_IN_BYTES, performVarLong(512 * GB_IN_BYTES)); 404 assertEquals(Long.MIN_VALUE, performVarLong(Long.MIN_VALUE)); 405 assertEquals(Long.MAX_VALUE, performVarLong(Long.MAX_VALUE)); 406 assertEquals(Long.MIN_VALUE + 40, performVarLong(Long.MIN_VALUE + 40)); 407 assertEquals(Long.MAX_VALUE - 40, performVarLong(Long.MAX_VALUE - 40)); 408 } 409 410 public void testIndexBeforeAfter() throws Exception { 411 final long BUCKET_SIZE = HOUR_IN_MILLIS; 412 stats = new NetworkStatsHistory(BUCKET_SIZE); 413 414 final long FIRST_START = TEST_START; 415 final long FIRST_END = FIRST_START + (2 * HOUR_IN_MILLIS); 416 final long SECOND_START = TEST_START + WEEK_IN_MILLIS; 417 final long SECOND_END = SECOND_START + HOUR_IN_MILLIS; 418 final long THIRD_START = TEST_START + (2 * WEEK_IN_MILLIS); 419 final long THIRD_END = THIRD_START + (2 * HOUR_IN_MILLIS); 420 421 stats.recordData(FIRST_START, FIRST_END, 422 new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L)); 423 stats.recordData(SECOND_START, SECOND_END, 424 new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L)); 425 stats.recordData(THIRD_START, THIRD_END, 426 new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L)); 427 428 // should have buckets: 2+1+2 429 assertEquals(5, stats.size()); 430 431 assertIndexBeforeAfter(stats, 0, 0, Long.MIN_VALUE); 432 assertIndexBeforeAfter(stats, 0, 1, FIRST_START); 433 assertIndexBeforeAfter(stats, 0, 1, FIRST_START + MINUTE_IN_MILLIS); 434 assertIndexBeforeAfter(stats, 0, 2, FIRST_START + HOUR_IN_MILLIS); 435 assertIndexBeforeAfter(stats, 1, 2, FIRST_START + HOUR_IN_MILLIS + MINUTE_IN_MILLIS); 436 assertIndexBeforeAfter(stats, 1, 2, FIRST_END - MINUTE_IN_MILLIS); 437 assertIndexBeforeAfter(stats, 1, 2, FIRST_END); 438 assertIndexBeforeAfter(stats, 1, 2, FIRST_END + MINUTE_IN_MILLIS); 439 assertIndexBeforeAfter(stats, 1, 2, SECOND_START - MINUTE_IN_MILLIS); 440 assertIndexBeforeAfter(stats, 1, 3, SECOND_START); 441 assertIndexBeforeAfter(stats, 2, 3, SECOND_END); 442 assertIndexBeforeAfter(stats, 2, 3, SECOND_END + MINUTE_IN_MILLIS); 443 assertIndexBeforeAfter(stats, 2, 3, THIRD_START - MINUTE_IN_MILLIS); 444 assertIndexBeforeAfter(stats, 2, 4, THIRD_START); 445 assertIndexBeforeAfter(stats, 3, 4, THIRD_START + MINUTE_IN_MILLIS); 446 assertIndexBeforeAfter(stats, 3, 4, THIRD_START + HOUR_IN_MILLIS); 447 assertIndexBeforeAfter(stats, 4, 4, THIRD_END); 448 assertIndexBeforeAfter(stats, 4, 4, THIRD_END + MINUTE_IN_MILLIS); 449 assertIndexBeforeAfter(stats, 4, 4, Long.MAX_VALUE); 450 } 451 452 private static void assertIndexBeforeAfter( 453 NetworkStatsHistory stats, int before, int after, long time) { 454 assertEquals("unexpected before", before, stats.getIndexBefore(time)); 455 assertEquals("unexpected after", after, stats.getIndexAfter(time)); 456 } 457 458 private static long performVarLong(long before) throws Exception { 459 final ByteArrayOutputStream out = new ByteArrayOutputStream(); 460 writeVarLong(new DataOutputStream(out), before); 461 462 final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 463 return readVarLong(new DataInputStream(in)); 464 } 465 466 private static void assertConsistent(NetworkStatsHistory stats) { 467 // verify timestamps are monotonic 468 long lastStart = Long.MIN_VALUE; 469 NetworkStatsHistory.Entry entry = null; 470 for (int i = 0; i < stats.size(); i++) { 471 entry = stats.getValues(i, entry); 472 assertTrue(lastStart < entry.bucketStart); 473 lastStart = entry.bucketStart; 474 } 475 } 476 477 private static void assertValues( 478 NetworkStatsHistory stats, int index, long rxBytes, long txBytes) { 479 final NetworkStatsHistory.Entry entry = stats.getValues(index, null); 480 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 481 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 482 } 483 484 private static void assertValues( 485 NetworkStatsHistory stats, long start, long end, long rxBytes, long txBytes) { 486 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null); 487 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 488 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 489 } 490 491 private static void assertValues(NetworkStatsHistory stats, int index, long activeTime, 492 long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { 493 final NetworkStatsHistory.Entry entry = stats.getValues(index, null); 494 assertEquals("unexpected activeTime", activeTime, entry.activeTime); 495 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 496 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 497 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 498 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 499 assertEquals("unexpected operations", operations, entry.operations); 500 } 501 502 private static void assertFullValues(NetworkStatsHistory stats, long activeTime, long rxBytes, 503 long rxPackets, long txBytes, long txPackets, long operations) { 504 assertValues(stats, Long.MIN_VALUE, Long.MAX_VALUE, activeTime, rxBytes, rxPackets, txBytes, 505 txPackets, operations); 506 } 507 508 private static void assertValues(NetworkStatsHistory stats, long start, long end, 509 long activeTime, long rxBytes, long rxPackets, long txBytes, long txPackets, 510 long operations) { 511 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null); 512 assertEquals("unexpected activeTime", activeTime, entry.activeTime); 513 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 514 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 515 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 516 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 517 assertEquals("unexpected operations", operations, entry.operations); 518 } 519} 520