天天看點

Android源碼浏覽器的性能分析工具類一、Performance.java二、具體源碼

一、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[] + sysCpu[] - mUserStart)
            *  + " ms, kernel used " + (sysCpu[] - mSystemStart) * 
            + " ms, idle took " + (sysCpu[] - mIdleStart) * 
            + " ms and irq took " + (sysCpu[] + sysCpu[] + sysCpu[] - mIrqStart)
            *  + " 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,  *  * );
        }
    }

    static void tracePageFinished() {
        if (mInTrace) {
            mInTrace = false;
            Debug.stopMethodTracing();
        }
    }

    static void onPageStarted() {
        mStart = SystemClock.uptimeMillis();
        mProcessStart = Process.getElapsedCpuTime();
        long[] sysCpu = new long[];
        if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, sysCpu, null)) {
            mUserStart = sysCpu[] + sysCpu[];
            mSystemStart = sysCpu[];
            mIdleStart = sysCpu[];
            mIrqStart = sysCpu[] + sysCpu[] + sysCpu[];
        }
        mUiStart = SystemClock.currentThreadTimeMillis();
    }

    static void onPageFinished(String url) {
        long[] sysCpu = new long[];
        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[] + sysCpu[] - mUserStart)
                            *  + " ms, kernel used " + (sysCpu[] - mSystemStart) * 
                            + " ms, idle took " + (sysCpu[] - mIdleStart) * 
                            + " ms and irq took " + (sysCpu[] + sysCpu[] + sysCpu[] - mIrqStart)
                            *  + " 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();
                } else if (newUrl.startsWith("http://")) {
                    newUrl = newUrl.substring();
                } else if (newUrl.startsWith("https://www.")) {
                    newUrl = newUrl.substring();
                } else if (newUrl.startsWith("https://")) {
                    newUrl = newUrl.substring();
                }
                if (LOGD_ENABLED) {
                    Log.d(LOGTAG, newUrl + " loaded");
                }
            }
        }
    }
}
           

繼續閱讀