PerfBehavior.java :  » 6.0-JDK-Modules » java-3d » org » jdesktop » j3dfly » utils » internalplugins » Java Open Source

Java Open Source » 6.0 JDK Modules » java 3d 
java 3d » org » jdesktop » j3dfly » utils » internalplugins » PerfBehavior.java
/*
 *  $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/internalplugins/PerfBehavior.java,v 1.1 2005/04/20 21:05:05 paulby Exp $
 *
 *                         Sun Public License Notice
 *
 *  The contents of this file are subject to the Sun Public License Version
 *  1.0 (the "License"). You may not use this file except in compliance with
 *  the License. A copy of the License is available at http://www.sun.com/
 *  
 *  The Original Code is Java 3D(tm) Fly Through.
 *  The Initial Developer of the Original Code is Paul Byrne.
 *  Portions created by Paul Byrne are Copyright (C) 2002.
 *  All Rights Reserved.
 *  
 *  Contributor(s): Paul Byrne.
 *  
 **/
package org.jdesktop.j3dfly.utils.internalplugins;

import javax.media.j3d.*;
import javax.vecmath.*;
import java.text.*;

public class PerfBehavior extends Behavior {
    WakeupOnElapsedFrames FPSwakeup = new WakeupOnElapsedFrames(0);
    private static final long testduration = 1000;  // in milliseconds
    private static final long sampleduration = 10000;  // in milliseconds
    private boolean doCalibration = true;
    private boolean startup = true;
    private boolean warmup = true;
    private int warmupTime = 20000; // Set the warmup time to 20 sec.
    private int numframes = 0;
    private int maxframes = 1;
    private long startuptime = 0;
    private long currtime = 0;
    private long lasttime = 0;
    private long deltatime;
    private boolean finiteLoop = true;
    static boolean avgFPS = true;
    private int sumFrames = 0;
    private long sumTimes = 0;
    private int loop = 0;
    private int loopCount = 5;
    private boolean doStat = false;
    private double fps;
    private double avgFps;
    private int prtMemFpCount = 0;
    
    public NumberFormat nf = null;
    
    public PerfBehavior(BoundingSphere bounds, boolean ds) {
  setSchedulingBounds(bounds);
  setEnable(true);
  doStat = ds;
  nf = NumberFormat.getNumberInstance();
    }

    public PerfBehavior(BoundingSphere bounds, boolean ds, int memFpCnt) {
  setSchedulingBounds(bounds);
  setEnable(true);
  doStat = ds;
  nf = NumberFormat.getNumberInstance();
  prtMemFpCount = memFpCnt;
    }
    
    // Called to init the behavior
    public void initialize() {
  // Set the trigger for the interpolator
  wakeupOn(FPSwakeup);
    }

    private void memoryFootPrint() {
  long totalMem, freeMem, usedMem;
  
  for(int i=0; i<prtMemFpCount; i++) {
            totalMem = Runtime.getRuntime().totalMemory();
            freeMem = Runtime.getRuntime().freeMemory();
            usedMem = totalMem - freeMem;
            System.out.print("mem used - before: " + usedMem + "bytes ");
            //System.out.print("mem used - before: " + usedMem + " ");
            System.runFinalization();
            System.gc();
            System.runFinalization();
            totalMem = Runtime.getRuntime().totalMemory();
            freeMem = Runtime.getRuntime().freeMemory();
            usedMem = totalMem - freeMem;
            System.out.println("after: " + usedMem + "bytes ");
            //System.out.println("after: " + usedMem + " ");
            try {
                Thread.sleep(500);
            }
            catch (InterruptedException e) { }
        }
    }

    // Called every time the behavior is activated
    public void processStimulus(java.util.Enumeration critera) {
  // Apply Calibration Algorithm :
  // To determine maxframes to run before sampling the time
  // to determine frames per second.
  // sampleduration = 10000, To run test pass for 10 seconds.

  if (doCalibration) { // do the calibration
      if (startup) {
    memoryFootPrint();
    startuptime = System.currentTimeMillis();
    startup = false;
      }
      else if(warmup) { // Let's wait for the system to stable down.
    currtime = System.currentTimeMillis();
    deltatime = currtime - startuptime;
    if(deltatime > warmupTime) {
        // System.out.println("I'm ready!!! deltatime " + deltatime);
        warmup = false;
        lasttime = System.currentTimeMillis();
    }
      }
      else {
    numframes += 1;
    // System.out.print(maxframes+" "+numframes+" "+lasttime+" ");
    if (numframes >= maxframes) {
        currtime = System.currentTimeMillis();
        deltatime = currtime - lasttime;
        // System.out.println("deltatime = " + deltatime +
        //              ", numframes = " + numframes);
        if (deltatime > testduration) {
      maxframes =
          (int)Math.ceil((double)numframes *
             ((double)sampleduration /
              (double)deltatime));
      //System.out.println("maxframes = " + maxframes);

      // reset the value for the measurement
      doCalibration = false;
      numframes = 0;
      lasttime = System.currentTimeMillis();
        }
        else {
      maxframes *= 2;
        }
    }
      }
  }
  else { // do the measurement
      numframes += 1;
      // System.out.println("PerfBeh : numframes is " + numframes);
      
      if (numframes >= maxframes) {
    currtime = System.currentTimeMillis();
    deltatime = currtime - lasttime;
    double fps = (double)numframes / ((double)deltatime / 1000.0);
    
    System.out.println("PerfBeh : numframes : " + numframes +
           " time : " + ((double)deltatime / 1000.0) +
           " sec." + " fps : " + nf.format(fps));
    if (finiteLoop) {
        sumFrames += numframes;
        sumTimes += deltatime;
        avgFps = (double)sumFrames*1000.0/(double)sumTimes;
        loop ++;
        if (loop >= loopCount) {
      System.out.println("PerfBeh : Avg fps " + avgFps);
      if(doStat) {
          doCalibration = true;
          loop = 0;
          numframes = 0;
          maxframes = 1;
          currtime = 0;
          startup = true;
          warmup = true;
          lasttime = 0;
          doStat(avgFps, true);
      }
      else {
          System.out.println("************** The End **************\n");
          System.exit(0);
      }
        }
    }
    
    if(doStat) {
        doStat(fps, false);
    }
    
    numframes = 0;
    lasttime = System.currentTimeMillis();;
      }
  }

  // Set the trigger for the interpolator
  wakeupOn(FPSwakeup);
  
    }

    public void setFiniteLoop(boolean fl) {
  finiteLoop = fl;
    }

    public void setLoopCount(int lc) {
  loopCount = lc;
    }
    
    public void doStat(double fps, boolean exit) {
  
    }

}


java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.