博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android源码浏览器的性能分析工具类
阅读量:4146 次
发布时间:2019-05-25

本文共 5769 字,大约阅读时间需要 19 分钟。

一、Performance.java

本来想查看 proc/stat文件的,居然发现系统源码还有如此好东西,即查看浏览网页的各种事件计算,进行辅助性能分析。

有需要的童鞋拿去吧,我只是搬运工

String performanceString =    "It took total " + (SystemClock.uptimeMillis() - mStart)            + " ms clock time to load the page." + "\nbrowser process used "            + (Process.getElapsedCpuTime() - mProcessStart)            + " ms, user processes used " + (sysCpu[0] + sysCpu[1] - mUserStart)            * 10 + " ms, kernel used " + (sysCpu[2] - mSystemStart) * 10            + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10            + " ms and irq took " + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)            * 10 + " ms, " + uiInfo;

二、具体源码

/packages/apps/Browser/src/com/android/browser/Performance.java

/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.browser;import android.net.WebAddress;import android.os.Debug;import android.os.Process;import android.os.SystemClock;import android.util.Log;/** * Performance analysis */public class Performance {
private static final String LOGTAG = "browser"; private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED; private static boolean mInTrace; // Performance probe private static final int[] SYSTEM_CPU_FORMAT = new int[] { Process.PROC_SPACE_TERM | Process.PROC_COMBINE, Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time }; private static long mStart; private static long mProcessStart; private static long mUserStart; private static long mSystemStart; private static long mIdleStart; private static long mIrqStart; private static long mUiStart; static void tracePageStart(String url) { if (BrowserSettings.getInstance().isTracing()) { String host; try { WebAddress uri = new WebAddress(url); host = uri.getHost(); } catch (android.net.ParseException ex) { host = "browser"; } host = host.replace('.', '_'); host += ".trace"; mInTrace = true; Debug.startMethodTracing(host, 20 * 1024 * 1024); } } static void tracePageFinished() { if (mInTrace) { mInTrace = false; Debug.stopMethodTracing(); } } static void onPageStarted() { mStart = SystemClock.uptimeMillis(); mProcessStart = Process.getElapsedCpuTime(); long[] sysCpu = new long[7]; if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, sysCpu, null)) { mUserStart = sysCpu[0] + sysCpu[1]; mSystemStart = sysCpu[2]; mIdleStart = sysCpu[3]; mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6]; } mUiStart = SystemClock.currentThreadTimeMillis(); } static void onPageFinished(String url) { long[] sysCpu = new long[7]; if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, sysCpu, null)) { String uiInfo = "UI thread used " + (SystemClock.currentThreadTimeMillis() - mUiStart) + " ms"; if (LOGD_ENABLED) { Log.d(LOGTAG, uiInfo); } // The string that gets written to the log String performanceString = "It took total " + (SystemClock.uptimeMillis() - mStart) + " ms clock time to load the page." + "\nbrowser process used " + (Process.getElapsedCpuTime() - mProcessStart) + " ms, user processes used " + (sysCpu[0] + sysCpu[1] - mUserStart) * 10 + " ms, kernel used " + (sysCpu[2] - mSystemStart) * 10 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10 + " ms and irq took " + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart) * 10 + " ms, " + uiInfo; if (LOGD_ENABLED) { Log.d(LOGTAG, performanceString + "\nWebpage: " + url); } if (url != null) { // strip the url to maintain consistency String newUrl = new String(url); if (newUrl.startsWith("http://www.")) { newUrl = newUrl.substring(11); } else if (newUrl.startsWith("http://")) { newUrl = newUrl.substring(7); } else if (newUrl.startsWith("https://www.")) { newUrl = newUrl.substring(12); } else if (newUrl.startsWith("https://")) { newUrl = newUrl.substring(8); } if (LOGD_ENABLED) { Log.d(LOGTAG, newUrl + " loaded"); } } } }}

转载地址:http://xocti.baihongyu.com/

你可能感兴趣的文章
RabbitMQ消息队列处理库存解锁及关闭订单问题
查看>>
netty学习笔记
查看>>
mvcc多版本并发控制
查看>>
HashMap实现原理
查看>>
ThreadLocal技术详解
查看>>
ConcurrentHashMap解析
查看>>
TCP协议三次握手、四次挥手以及TCP窗口滑动机制
查看>>
【排序算法】- 希尔排序
查看>>
Int和Integer的区别
查看>>
java如何避免死锁
查看>>
【排序算法】- 快速排序
查看>>
【排序算法】- 归并排序
查看>>
【查找算法】- 二分查找算法
查看>>
java中的静态变量、静态方法与静态代码块区别
查看>>
JAVA中静态块、静态变量加载顺序详解
查看>>
Spring Cloud 学习(六)分布式配置中心
查看>>
Intellij Idea的常用快捷键
查看>>
分布式架构的演进过程
查看>>
分布式系统架构设计
查看>>
幂等性问题及解决方案
查看>>