NetworkStatsHistoryTest.java revision d2a458750e5a3d490af09cecb5c28370baf0a913
175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey/*
275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Copyright (C) 2011 The Android Open Source Project
375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *
475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * you may not use this file except in compliance with the License.
675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * You may obtain a copy of the License at
775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *
875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *
1075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
1175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
1275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * See the License for the specific language governing permissions and
1475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * limitations under the License.
1575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey */
1675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
1775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeypackage android.net;
1875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
1975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.DAY_IN_MILLIS;
2075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.HOUR_IN_MILLIS;
2175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.MINUTE_IN_MILLIS;
2275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.SECOND_IN_MILLIS;
2375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.WEEK_IN_MILLIS;
2475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport static android.text.format.DateUtils.YEAR_IN_MILLIS;
2575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
2675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.test.suitebuilder.annotation.SmallTest;
2775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.test.suitebuilder.annotation.Suppress;
2875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.util.Log;
2975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
3075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport junit.framework.TestCase;
3175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
3275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.util.Random;
3375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
3475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey@SmallTest
3575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeypublic class NetworkStatsHistoryTest extends TestCase {
3675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    private static final String TAG = "NetworkStatsHistoryTest";
3775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
3875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    private static final long TEST_START = 1194220800000L;
3975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
4075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    private NetworkStatsHistory stats;
4175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
4275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    @Override
4375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    protected void tearDown() throws Exception {
4475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        super.tearDown();
4575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        if (stats != null) {
4675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            assertConsistent(stats);
4775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
4875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
4975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
5075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void testRecordSingleBucket() throws Exception {
5175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long BUCKET_SIZE = HOUR_IN_MILLIS;
52d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        stats = new NetworkStatsHistory(BUCKET_SIZE);
5375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
5475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // record data into narrow window to get single bucket
5575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 1024L, 2048L);
5675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
5775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(1, stats.bucketCount);
5875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 0, 1024L, 2048L);
5975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
6075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
6175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void testRecordEqualBuckets() throws Exception {
6275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long bucketDuration = HOUR_IN_MILLIS;
63d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        stats = new NetworkStatsHistory(bucketDuration);
6475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
6575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // split equally across two buckets
6675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long recordStart = TEST_START + (bucketDuration / 2);
6775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.recordData(recordStart, recordStart + bucketDuration, 1024L, 128L);
6875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
6975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(2, stats.bucketCount);
7075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 0, 512L, 64L);
7175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 1, 512L, 64L);
7275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
7375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
7475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void testRecordTouchingBuckets() throws Exception {
7575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long BUCKET_SIZE = 15 * MINUTE_IN_MILLIS;
76d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        stats = new NetworkStatsHistory(BUCKET_SIZE);
7775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
7875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // split almost completely into middle bucket, but with a few minutes
7975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // overlap into neighboring buckets. total record is 20 minutes.
8075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long recordStart = (TEST_START + BUCKET_SIZE) - MINUTE_IN_MILLIS;
8175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long recordEnd = (TEST_START + (BUCKET_SIZE * 2)) + (MINUTE_IN_MILLIS * 4);
8275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.recordData(recordStart, recordEnd, 1000L, 5000L);
8375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
8475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(3, stats.bucketCount);
8575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // first bucket should have (1/20 of value)
8675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 0, 50L, 250L);
8775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // second bucket should have (15/20 of value)
8875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 1, 750L, 3750L);
8975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // final bucket should have (4/20 of value)
9075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 2, 200L, 1000L);
9175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
9275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
9375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void testRecordGapBuckets() throws Exception {
9475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long BUCKET_SIZE = HOUR_IN_MILLIS;
95d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        stats = new NetworkStatsHistory(BUCKET_SIZE);
9675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
9775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // record some data today and next week with large gap
9875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long firstStart = TEST_START;
9975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long lastStart = TEST_START + WEEK_IN_MILLIS;
10075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.recordData(firstStart, firstStart + SECOND_IN_MILLIS, 128L, 256L);
10175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.recordData(lastStart, lastStart + SECOND_IN_MILLIS, 64L, 512L);
10275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
10375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // we should have two buckets, far apart from each other
10475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(2, stats.bucketCount);
10575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 0, 128L, 256L);
10675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 1, 64L, 512L);
10775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
10875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // now record something in middle, spread across two buckets
10975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long middleStart = TEST_START + DAY_IN_MILLIS;
11075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long middleEnd = middleStart + (HOUR_IN_MILLIS * 2);
11175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.recordData(middleStart, middleEnd, 2048L, 2048L);
11275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
11375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // now should have four buckets, with new record in middle two buckets
11475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(4, stats.bucketCount);
11575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 0, 128L, 256L);
11675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 1, 1024L, 1024L);
11775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 2, 1024L, 1024L);
11875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 3, 64L, 512L);
11975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
12075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
12175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void testRecordOverlapBuckets() throws Exception {
12275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long BUCKET_SIZE = HOUR_IN_MILLIS;
123d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        stats = new NetworkStatsHistory(BUCKET_SIZE);
12475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
12575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // record some data in one bucket, and another overlapping buckets
12675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 256L, 256L);
12775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long midStart = TEST_START + (HOUR_IN_MILLIS / 2);
12875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.recordData(midStart, midStart + HOUR_IN_MILLIS, 1024L, 1024L);
12975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
13075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // should have two buckets, with some data mixed together
13175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(2, stats.bucketCount);
13275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 0, 768L, 768L);
13375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertBucket(stats, 1, 512L, 512L);
13475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
13575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
13675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void testRemove() throws Exception {
13775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final long BUCKET_SIZE = HOUR_IN_MILLIS;
138d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey        stats = new NetworkStatsHistory(BUCKET_SIZE);
13975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
14075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // record some data across 24 buckets
14175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 24L, 24L);
14275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(24, stats.bucketCount);
14375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
14475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // try removing far before buckets; should be no change
14575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.removeBucketsBefore(TEST_START - YEAR_IN_MILLIS);
14675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(24, stats.bucketCount);
14775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
14875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // try removing just moments into first bucket; should be no change
14975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // since that bucket contains data beyond the cutoff
15075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.removeBucketsBefore(TEST_START + SECOND_IN_MILLIS);
15175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(24, stats.bucketCount);
15275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
15375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // try removing single bucket
15475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.removeBucketsBefore(TEST_START + HOUR_IN_MILLIS);
15575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(23, stats.bucketCount);
15675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
15775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // try removing multiple buckets
15875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.removeBucketsBefore(TEST_START + (4 * HOUR_IN_MILLIS));
15975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(20, stats.bucketCount);
16075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
16175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // try removing all buckets
16275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        stats.removeBucketsBefore(TEST_START + YEAR_IN_MILLIS);
16375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals(0, stats.bucketCount);
16475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
16575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
16675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    @Suppress
16775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void testFuzzing() throws Exception {
16875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        try {
16975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            // fuzzing with random events, looking for crashes
17075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final Random r = new Random();
17175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            for (int i = 0; i < 500; i++) {
172d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey                stats = new NetworkStatsHistory(r.nextLong());
17375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                for (int j = 0; j < 10000; j++) {
17475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                    if (r.nextBoolean()) {
17575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                        // add range
17675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                        final long start = r.nextLong();
17775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                        final long end = start + r.nextInt();
17875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                        stats.recordData(start, end, r.nextLong(), r.nextLong());
17975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                    } else {
18075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                        // trim something
18175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                        stats.removeBucketsBefore(r.nextLong());
18275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                    }
18375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                }
18475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                assertConsistent(stats);
18575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            }
18675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        } catch (Throwable e) {
18775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            Log.e(TAG, String.valueOf(stats));
18875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            throw new RuntimeException(e);
18975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
19075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
19175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
19275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    private static void assertConsistent(NetworkStatsHistory stats) {
19375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // verify timestamps are monotonic
19475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        for (int i = 1; i < stats.bucketCount; i++) {
19575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            assertTrue(stats.bucketStart[i - 1] < stats.bucketStart[i]);
19675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
19775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
19875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
19975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    private static void assertBucket(NetworkStatsHistory stats, int index, long rx, long tx) {
20075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals("unexpected rx", rx, stats.rx[index]);
20175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        assertEquals("unexpected tx", tx, stats.tx[index]);
20275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
20375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
20475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey}
205