1 /* 2 * Copyright (C) 2018 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 package android.util; 17 18 import com.android.internal.util.FastPrintWriter; 19 20 import com.google.caliper.AfterExperiment; 21 import com.google.caliper.BeforeExperiment; 22 23 import java.io.IOException; 24 import java.io.OutputStream; 25 import java.util.ArrayList; 26 import java.util.Arrays; 27 import java.util.Comparator; 28 import java.util.List; 29 import java.util.Random; 30 import java.util.stream.Collectors; 31 32 public class StreamsBenchmark { 33 private OutputStream dummy = new OutputStream() { 34 @Override 35 public void write(int b) throws IOException { 36 } 37 38 @Override 39 public void write(byte b[], int off, int len) throws IOException { 40 } 41 }; 42 43 private SparseIntArray calls; 44 45 @BeforeExperiment 46 protected void setUp() { 47 calls = new SparseIntArray(); 48 final Random r = new Random(1); 49 for (int i = 0; i < 100; i++) { 50 calls.put(i, r.nextInt(Integer.MAX_VALUE)); 51 } 52 } 53 54 @AfterExperiment 55 protected void tearDown() { 56 calls = null; 57 } 58 59 public void timeDirect(int reps) { 60 for (int i = 0; i < reps; i++) { 61 final int N = calls.size(); 62 final long[] values = new long[N]; 63 for (int j = 0; j < N; j++) { 64 values[j] = ((long) calls.valueAt(j) << 32) | calls.keyAt(j); 65 } 66 Arrays.sort(values); 67 68 final FastPrintWriter pw = new FastPrintWriter(dummy); 69 pw.println("Top openSession callers (uid=count):"); 70 final int end = Math.max(0, N - 20); 71 for (int j = N - 1; j >= end; j--) { 72 final int uid = (int) (values[j] & 0xffffffff); 73 final int count = (int) (values[j] >> 32); 74 pw.print(uid); 75 pw.print("="); 76 pw.println(count); 77 } 78 pw.println(); 79 pw.flush(); 80 } 81 } 82 83 public void timeStreams(int reps) { 84 for (int i = 0; i < reps; i++) { 85 List<Pair<Integer, Integer>> callsList = 86 getOpenSessionCallsList(calls).stream().sorted( 87 Comparator.comparing( 88 (Pair<Integer, Integer> pair) -> pair.second).reversed()) 89 .limit(20) 90 .collect(Collectors.toList()); 91 92 final FastPrintWriter pw = new FastPrintWriter(dummy); 93 pw.println("Top openSession callers (uid=count):"); 94 for (Pair<Integer, Integer> uidCalls : callsList) { 95 pw.print(uidCalls.first); 96 pw.print("="); 97 pw.println(uidCalls.second); 98 } 99 pw.println(); 100 pw.flush(); 101 } 102 } 103 104 private static List<Pair<Integer, Integer>> getOpenSessionCallsList( 105 SparseIntArray openSessionCalls) { 106 ArrayList<Pair<Integer, Integer>> list = new ArrayList<>(openSessionCalls.size()); 107 for (int i=0; i<openSessionCalls.size(); i++) { 108 final int uid = openSessionCalls.keyAt(i); 109 list.add(new Pair<>(uid, openSessionCalls.get(uid))); 110 } 111 112 return list; 113 } 114 } 115