2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Param;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
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;
31 @Param({"100", "1000"})
34 * Should not be more than the length of {@link #UNDERLYING_IFACES}.
37 private int mNumUnderlyingIfaces;
38 private NetworkStats mNetworkStats;
41 protected void setUp() throws Exception {
42 mNetworkStats = new NetworkStats(0, mSize + 2);
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);
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) {
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);
77 private String[] getVpnUnderlyingIfaces() {
78 return Arrays.copyOf(UNDERLYING_IFACES, mNumUnderlyingIfaces);
82 * Same as {@link #getVpnUnderlyingIfaces}, but also contains {@link #TUN_IFACE}.
84 private List<String> getAllIfacesForBenchmark() {
85 List<String> ifaces = new ArrayList<>();
86 ifaces.add(TUN_IFACE);
87 ifaces.addAll(Arrays.asList(getVpnUnderlyingIfaces()));
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());
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.
103 public void timeClone(int reps) {
104 for (int i = 0; i < reps; i++) {
105 NetworkStats stats = mNetworkStats.clone();