OSDN Git Service

Merge "docs: Add documentation for equals() method" into qt-dev am: 732a127636
[android-x86/frameworks-base.git] / core / tests / benchmarks / src / android / net / NetworkStatsBenchmark.java
1 /*
2  * Copyright (C) 2015 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
17 package android.net;
18
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Param;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 public class NetworkStatsBenchmark {
27     private static final String[] UNDERLYING_IFACES = {"wlan0", "rmnet0"};
28     private static final String TUN_IFACE = "tun0";
29     private static final int TUN_UID = 999999999;
30
31     @Param({"100", "1000"})
32     private int mSize;
33     /**
34      * Should not be more than the length of {@link #UNDERLYING_IFACES}.
35      */
36     @Param({"1", "2"})
37     private int mNumUnderlyingIfaces;
38     private NetworkStats mNetworkStats;
39
40     @BeforeExperiment
41     protected void setUp() throws Exception {
42         mNetworkStats = new NetworkStats(0, mSize + 2);
43         int uid = 0;
44         NetworkStats.Entry recycle = new NetworkStats.Entry();
45         final List<String> allIfaces = getAllIfacesForBenchmark(); // also contains TUN_IFACE.
46         final int totalIfaces = allIfaces.size();
47         for (int i = 0; i < mSize; i++) {
48             recycle.iface = allIfaces.get(i % totalIfaces);
49             recycle.uid = uid;
50             recycle.set = i % 2;
51             recycle.tag = NetworkStats.TAG_NONE;
52             recycle.rxBytes = 60000;
53             recycle.rxPackets = 60;
54             recycle.txBytes = 150000;
55             recycle.txPackets = 1500;
56             recycle.operations = 0;
57             mNetworkStats.addValues(recycle);
58             if (recycle.set == 1) {
59                 uid++;
60             }
61         }
62
63         for (int i = 0; i < mNumUnderlyingIfaces; i++) {
64             recycle.iface = UNDERLYING_IFACES[i];
65             recycle.uid = TUN_UID;
66             recycle.set = NetworkStats.SET_FOREGROUND;
67             recycle.tag = NetworkStats.TAG_NONE;
68             recycle.rxBytes = 90000 * mSize;
69             recycle.rxPackets = 40 * mSize;
70             recycle.txBytes = 180000 * mSize;
71             recycle.txPackets = 1200 * mSize;
72             recycle.operations = 0;
73             mNetworkStats.addValues(recycle);
74         }
75     }
76
77     private String[] getVpnUnderlyingIfaces() {
78         return Arrays.copyOf(UNDERLYING_IFACES, mNumUnderlyingIfaces);
79     }
80
81     /**
82      * Same as {@link #getVpnUnderlyingIfaces}, but also contains {@link #TUN_IFACE}.
83      */
84     private List<String> getAllIfacesForBenchmark() {
85         List<String> ifaces = new ArrayList<>();
86         ifaces.add(TUN_IFACE);
87         ifaces.addAll(Arrays.asList(getVpnUnderlyingIfaces()));
88         return ifaces;
89     }
90
91     public void timeMigrateTun(int reps) {
92         for (int i = 0; i < reps; i++) {
93             NetworkStats stats = mNetworkStats.clone();
94             stats.migrateTun(TUN_UID, TUN_IFACE, getVpnUnderlyingIfaces());
95         }
96     }
97
98     /**
99      * Since timeMigrateTun() includes a clone() call on the NetworkStats object,
100      * we need to measure the cost of the clone() call itself in order to get more
101      * accurate measurement on the migrateTun() method.
102      */
103     public void timeClone(int reps) {
104         for (int i = 0; i < reps; i++) {
105             NetworkStats stats = mNetworkStats.clone();
106         }
107     }
108 }