1 /* 2 * Copyright 2012 AndroidPlot.com 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 17 package com.androidplot.util; 18 19 import android.graphics.Canvas; 20 import android.graphics.Color; 21 import android.graphics.Paint; 22 import android.graphics.RectF; 23 import com.androidplot.Plot; 24 import com.androidplot.PlotListener; 25 26 /** 27 * !!! THIS CLASS IS STILL UNDER DEVELOPMENT AND MAY CONTAIN BUGS !!! 28 * Gathers performance statistics from a Plot. Instances of PlotStatistics 29 * should never be added to more than one Plot, otherwise the statiscs will 30 * be invalid. 31 */ 32 public class PlotStatistics implements PlotListener { 33 long minRenderTimeMs; 34 long maxRenderTimeMs; 35 long avgRenderTimeMs; 36 long fps; 37 long updateDelayMs; 38 39 40 long longestRenderMs = 0; 41 long shortestRenderMs = 0; 42 long lastStart = 0; 43 long lastLatency = 0; 44 long lastAnnotation; 45 long latencySamples = 0; 46 long latencySum = 0; 47 String annotationString = ""; 48 49 private Paint paint; 50 { 51 paint = new Paint(); 52 paint.setTextAlign(Paint.Align.CENTER); 53 paint.setColor(Color.WHITE); 54 paint.setTextSize(30); 55 resetCounters(); 56 } 57 58 59 private boolean annotatePlotEnabled; 60 61 62 63 public PlotStatistics(long updateDelayMs, boolean annotatePlotEnabled) { 64 this.updateDelayMs = updateDelayMs; 65 this.annotatePlotEnabled = annotatePlotEnabled; 66 } 67 68 public void setAnnotatePlotEnabled(boolean enabled) { 69 this.annotatePlotEnabled = enabled; 70 } 71 72 private void resetCounters() { 73 longestRenderMs = 0; 74 shortestRenderMs = 999999999; 75 latencySamples = 0; 76 latencySum = 0; 77 } 78 79 private void annotatePlot(Plot source, Canvas canvas) { 80 long nowMs = System.currentTimeMillis(); 81 // throttle the update frequency: 82 long msSinceUpdate = (nowMs - lastAnnotation); 83 if(msSinceUpdate >= updateDelayMs) { 84 85 float avgLatency = latencySamples > 0 ? latencySum/latencySamples : 0; 86 String overallFPS = String.format("%.2f", latencySamples > 0 ? (1000f/msSinceUpdate) * latencySamples : 0); 87 String potentialFPS = String.format("%.2f", latencySamples > 0 ? 1000f/avgLatency : 0); 88 annotationString = "FPS (potential): " + potentialFPS + " FPS (actual): " + overallFPS + " Latency (ms) Avg: " + lastLatency + " \nMin: " + shortestRenderMs + 89 " Max: " + longestRenderMs; 90 lastAnnotation = nowMs; 91 resetCounters(); 92 } 93 RectF r = source.getDisplayDimensions().canvasRect; 94 if(annotatePlotEnabled) { 95 canvas.drawText(annotationString, r.centerX(), r.centerY(), paint); 96 } 97 } 98 99 @Override 100 public void onBeforeDraw(Plot source, Canvas canvas) { 101 lastStart = System.currentTimeMillis(); 102 } 103 104 @Override 105 public void onAfterDraw(Plot source, Canvas canvas) { 106 lastLatency = System.currentTimeMillis() - lastStart; 107 if(lastLatency < shortestRenderMs) { 108 shortestRenderMs = lastLatency; 109 } 110 111 if(lastLatency > longestRenderMs) { 112 longestRenderMs = lastLatency; 113 } 114 latencySum += lastLatency; 115 latencySamples++; 116 annotatePlot(source, canvas); 117 } 118 } 119