WindowAnimationFrameStatsMonitorImpl.java

/*
 * Copyright (C) 2015 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 androidx.test.jank.internal;

import android.app.Instrumentation;
import android.os.Bundle;
import androidx.test.jank.WindowAnimationFrameStatsMonitor;
import android.view.FrameStats;

import java.util.Collections;

/**
 * Monitors {@link android.view.WindowAnimationFrameStats} to detect janky frames.
 *
 * Reports average and max jank, as well as average frames per second and max frame times.
 */
class WindowAnimationFrameStatsMonitorImpl extends FrameStatsMonitorBase {

    private Instrumentation mInstrumentation;

    /**
     * Constructs a WindowAnimationFrameStatsMonitorImpl instance.
     */
    public WindowAnimationFrameStatsMonitorImpl(Instrumentation instrumentation) {
        mInstrumentation = instrumentation;
    }

    public Bundle getMetrics() {
        Bundle metrics = new Bundle();

        // Store average and max jank
        metrics.putDouble(WindowAnimationFrameStatsMonitor.KEY_AVG_NUM_JANKY,
                MetricsHelper.computeAverageInt(mJankyFrames));
        metrics.putInt(WindowAnimationFrameStatsMonitor.KEY_MAX_NUM_JANKY,
                Collections.max(mJankyFrames));

        // Store average fps
        metrics.putDouble(WindowAnimationFrameStatsMonitor.KEY_AVG_FPS,
                MetricsHelper.computeAverageDouble(mFps));

        // Store average max frame duration
        metrics.putDouble(WindowAnimationFrameStatsMonitor.KEY_AVG_LONGEST_FRAME,
                MetricsHelper.computeAverageDouble(mLongestNormalizedFrames));

        return metrics;
    }

    @Override
    public void startIteration() {
        // Clear out any previous data
        mInstrumentation.getUiAutomation().clearWindowAnimationFrameStats();
    }

    @Override
    public Bundle stopIteration() {
        FrameStats stats = mInstrumentation.getUiAutomation().getWindowAnimationFrameStats();
        analyze(stats);

        // TODO(allenhair): Return full itermediate results.
        Bundle ret = new Bundle();
        ret.putInt("num-frames", stats.getFrameCount());
        return ret;
    }
}