alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (ProfileRecorder.java)

This example Android source code file (ProfileRecorder.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

android, os, profile_count, profile_draw, profile_frame, profile_sim, profilerecord, profilerecorder

The ProfileRecorder.java Android example source code

/*
 * 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.heightmapprofiler;

import android.os.SystemClock;

/** 
 * Implements a simple runtime profiler.  The profiler records start and stop
 * times for several different types of profiles and can then return min, max
 * and average execution times per type.  Profile types are independent and may
 * be nested in calling code. This object is a singleton for convenience.
 */
public class ProfileRecorder {
    // A type for recording actual draw command time.
    public static final int PROFILE_DRAW = 0;
    // A type for recording the time it takes to run a single simulation step.
    public static final int PROFILE_SIM = 2;
    // A type for recording the total amount of time spent rendering a frame.
    public static final int PROFILE_FRAME = 3;
    private static final int PROFILE_COUNT = PROFILE_FRAME + 1;
    
    private ProfileRecord[] mProfiles;
    private int mFrameCount;
    private long mVertexCount;
    public static ProfileRecorder sSingleton = new ProfileRecorder();
    
    public ProfileRecorder() {
        mProfiles = new ProfileRecord[PROFILE_COUNT];
        for (int x = 0; x < PROFILE_COUNT; x++) {
            mProfiles[x] = new ProfileRecord();
        }
    }
    
    /** Starts recording execution time for a specific profile type.*/
    public final void start(int profileType) {
        if (profileType < PROFILE_COUNT) {
            mProfiles[profileType].start(SystemClock.uptimeMillis());
        }
    }
    
    /** Stops recording time for this profile type. */
    public final void stop(int profileType) {
        if (profileType < PROFILE_COUNT) {
            mProfiles[profileType].stop(SystemClock.uptimeMillis());
        }
    }
    
    /** Indicates the end of the frame.*/
    public final void endFrame() {
        mFrameCount++;
    }
    
    /* Flushes all recorded timings from the profiler. */
    public final void resetAll() {
        for (int x = 0; x < PROFILE_COUNT; x++) {
            mProfiles[x].reset();
        }
        mFrameCount = 0;
        mVertexCount = 0L;
    }
    
    /* Returns the average execution time, in milliseconds, for a given type. */
    public long getAverageTime(int profileType) {
        long time = 0;
        if (profileType < PROFILE_COUNT) {
            time = mProfiles[profileType].getAverageTime(mFrameCount);
        }
        return time;
    }
    
    /* Returns the minimum execution time in milliseconds for a given type. */
    public long getMinTime(int profileType) {
        long time = 0;
        if (profileType < PROFILE_COUNT) {
            time = mProfiles[profileType].getMinTime();
        }
        return time;
    }
    
    /* Returns the maximum execution time in milliseconds for a given type. */
    public long getMaxTime(int profileType) {
        long time = 0;
        if (profileType < PROFILE_COUNT) {
            time = mProfiles[profileType].getMaxTime();
        }
        return time;
    }
    
    public long getTotalVerts() {
		return mVertexCount;
	}
    
    public long getAverageVerts() {
    	return mVertexCount / mFrameCount;
    }
    
    public void addVerts(long vertCount) {
    	mVertexCount += vertCount;
    }
    
    /** 
     * A simple class for storing timing information about a single profile
     * type.
     */
    protected class ProfileRecord {
        private long mStartTime;
        private long mTotalTime;
        private long mMinTime;
        private long mMaxTime;
        
        public void start(long time) {
            mStartTime = time;
        }
        
        public void stop(long time) {
        	if (mStartTime > 0) {
	            final long timeDelta = time - mStartTime;
	            mTotalTime += timeDelta;
	            if (mMinTime == 0 || timeDelta < mMinTime) {
	                mMinTime = timeDelta;
	            }
	            if (mMaxTime == 0 || timeDelta > mMaxTime) {
	                mMaxTime = timeDelta;
	            }
        	}
        }
        
        public long getAverageTime(int frameCount) {
            long time = frameCount > 0 ? mTotalTime / frameCount : 0;
            return time;
        }
        
        public long getMinTime() {
            return mMinTime;
        }
        
        public long getMaxTime() {
            return mMaxTime;
        }
        
        public void startNewProfilePeriod() {
            mTotalTime = 0;
        }
        
        public void reset() {
            mTotalTime = 0;
            mStartTime = 0;
            mMinTime = 0;
            mMaxTime = 0;
        }
    }

	
}

Other Android examples (source code examples)

Here is a short list of links related to this Android ProfileRecorder.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.