Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dba7d0ae authored by junyulai's avatar junyulai Committed by Junyu Lai
Browse files

[SP26.2] add a method in NetworkStats that removes empty entries

In current design, entries with zeros are preserved after
addition/subtraction. These entries are not very useful
and lead to difficulty of verifying the result of
addition/subtraction.

However, change the behavior in the original NetworkStats
is considered risky in current stage.

Thus, this change provide a function that could remove these
empty entries in tests.

Test: atest FrameworksNetTests
Bug: 152827872
Bug: 150644692

Change-Id: I40a76935d55712b8083ee1e17e137a8a4ef5e029
Merged-In: I40a76935d55712b8083ee1e17e137a8a4ef5e029
(cherry picked from commit 6c7bef30)
parent e56cb5c0
Loading
Loading
Loading
Loading
+14 −8
Original line number Diff line number Diff line
@@ -1195,18 +1195,24 @@ public final class NetworkStats implements Parcelable {

    /**
     * Remove all rows that match one of specified UIDs.
     * This mutates the original structure in place.
     * @hide
     */
    public void removeUids(int[] uids) {
        int nextOutputEntry = 0;
        for (int i = 0; i < size; i++) {
            if (!ArrayUtils.contains(uids, uid[i])) {
                maybeCopyEntry(nextOutputEntry, i);
                nextOutputEntry++;
            }
        filter(e -> !ArrayUtils.contains(uids, e.uid));
    }

        size = nextOutputEntry;
    /**
     * Remove all rows that match one of specified UIDs.
     * @return the result object.
     * @hide
     */
    @NonNull
    public NetworkStats removeEmptyEntries() {
        final NetworkStats ret = this.clone();
        ret.filter(e -> e.rxBytes != 0 || e.rxPackets != 0 || e.txBytes != 0 || e.txPackets != 0
                || e.operations != 0);
        return ret;
    }

    /**
+47 −0
Original line number Diff line number Diff line
@@ -502,6 +502,53 @@ public class NetworkStatsTest {
                DEFAULT_NETWORK_NO, 64L, 2L, 0L, 0L, 0L);
    }

    @Test
    public void testRemoveEmptyEntries() throws Exception {
        // Test empty stats.
        final NetworkStats statsEmpty = new NetworkStats(TEST_START, 3);
        assertEquals(0, statsEmpty.removeEmptyEntries().size());

        // Test stats with non-zero entry.
        final NetworkStats statsNonZero = new NetworkStats(TEST_START, 1)
                .insertEntry(TEST_IFACE, 99, SET_DEFAULT, TAG_NONE, METERED_NO,
                        ROAMING_NO, DEFAULT_NETWORK_NO, 1L, 128L, 0L, 2L, 20L);
        assertEquals(1, statsNonZero.size());
        final NetworkStats expectedNonZero = statsNonZero.removeEmptyEntries();
        assertEquals(1, expectedNonZero.size());
        assertValues(expectedNonZero, 0, TEST_IFACE, 99, SET_DEFAULT, TAG_NONE, METERED_NO,
                ROAMING_NO, DEFAULT_NETWORK_NO, 1L, 128L, 0L, 2L, 20L);

        // Test stats with empty entry.
        final NetworkStats statsZero = new NetworkStats(TEST_START, 1)
                .insertEntry(TEST_IFACE, 99, SET_DEFAULT, TAG_NONE, METERED_NO,
                        ROAMING_NO, DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L);
        assertEquals(1, statsZero.size());
        final NetworkStats expectedZero = statsZero.removeEmptyEntries();
        assertEquals(1, statsZero.size()); // Assert immutable.
        assertEquals(0, expectedZero.size());

        // Test stats with multiple entries.
        final NetworkStats statsMultiple = new NetworkStats(TEST_START, 0)
                .insertEntry(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 2L, 64L, 0L, 2L, 20L)
                .insertEntry(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 4L, 32L, 0L, 0L, 0L)
                .insertEntry(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 0L, 0L, 0L, 0L, 0L)
                .insertEntry(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 0L, 0L, 0L, 0L, 0L)
                .insertEntry(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 8L, 0L, 0L, 0L, 0L)
                .insertEntry(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 0L, 8L, 0L, 0L, 0L)
                .insertEntry(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 4L, 0L, 0L)
                .insertEntry(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 0L, 0L, 0L, 2L, 0L)
                .insertEntry(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 0L, 0L, 0L, 0L, 1L);
        assertEquals(9, statsMultiple.size());
        final NetworkStats expectedMultiple = statsMultiple.removeEmptyEntries();
        assertEquals(9, statsMultiple.size()); // Assert immutable.
        assertEquals(7, expectedMultiple.size());
        assertValues(expectedMultiple.getTotalIncludingTags(null), 14L, 104L, 4L, 4L, 21L);

        // Test stats with multiple empty entries.
        assertEquals(statsMultiple.size(), statsMultiple.subtract(statsMultiple).size());
        assertEquals(0, statsMultiple.subtract(statsMultiple).removeEmptyEntries().size());
    }

    @Test
    public void testClone() throws Exception {
        final NetworkStats original = new NetworkStats(TEST_START, 5)