001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/internalplugins/PerfBehavior.java,v 1.1 2005/04/20 21:05:05 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.internalplugins;
019:
020: import javax.media.j3d.*;
021: import javax.vecmath.*;
022: import java.text.*;
023:
024: public class PerfBehavior extends Behavior {
025: WakeupOnElapsedFrames FPSwakeup = new WakeupOnElapsedFrames(0);
026: private static final long testduration = 1000; // in milliseconds
027: private static final long sampleduration = 10000; // in milliseconds
028: private boolean doCalibration = true;
029: private boolean startup = true;
030: private boolean warmup = true;
031: private int warmupTime = 20000; // Set the warmup time to 20 sec.
032: private int numframes = 0;
033: private int maxframes = 1;
034: private long startuptime = 0;
035: private long currtime = 0;
036: private long lasttime = 0;
037: private long deltatime;
038: private boolean finiteLoop = true;
039: static boolean avgFPS = true;
040: private int sumFrames = 0;
041: private long sumTimes = 0;
042: private int loop = 0;
043: private int loopCount = 5;
044: private boolean doStat = false;
045: private double fps;
046: private double avgFps;
047: private int prtMemFpCount = 0;
048:
049: public NumberFormat nf = null;
050:
051: public PerfBehavior(BoundingSphere bounds, boolean ds) {
052: setSchedulingBounds(bounds);
053: setEnable(true);
054: doStat = ds;
055: nf = NumberFormat.getNumberInstance();
056: }
057:
058: public PerfBehavior(BoundingSphere bounds, boolean ds, int memFpCnt) {
059: setSchedulingBounds(bounds);
060: setEnable(true);
061: doStat = ds;
062: nf = NumberFormat.getNumberInstance();
063: prtMemFpCount = memFpCnt;
064: }
065:
066: // Called to init the behavior
067: public void initialize() {
068: // Set the trigger for the interpolator
069: wakeupOn(FPSwakeup);
070: }
071:
072: private void memoryFootPrint() {
073: long totalMem, freeMem, usedMem;
074:
075: for (int i = 0; i < prtMemFpCount; i++) {
076: totalMem = Runtime.getRuntime().totalMemory();
077: freeMem = Runtime.getRuntime().freeMemory();
078: usedMem = totalMem - freeMem;
079: System.out
080: .print("mem used - before: " + usedMem + "bytes ");
081: //System.out.print("mem used - before: " + usedMem + " ");
082: System.runFinalization();
083: System.gc();
084: System.runFinalization();
085: totalMem = Runtime.getRuntime().totalMemory();
086: freeMem = Runtime.getRuntime().freeMemory();
087: usedMem = totalMem - freeMem;
088: System.out.println("after: " + usedMem + "bytes ");
089: //System.out.println("after: " + usedMem + " ");
090: try {
091: Thread.sleep(500);
092: } catch (InterruptedException e) {
093: }
094: }
095: }
096:
097: // Called every time the behavior is activated
098: public void processStimulus(java.util.Enumeration critera) {
099: // Apply Calibration Algorithm :
100: // To determine maxframes to run before sampling the time
101: // to determine frames per second.
102: // sampleduration = 10000, To run test pass for 10 seconds.
103:
104: if (doCalibration) { // do the calibration
105: if (startup) {
106: memoryFootPrint();
107: startuptime = System.currentTimeMillis();
108: startup = false;
109: } else if (warmup) { // Let's wait for the system to stable down.
110: currtime = System.currentTimeMillis();
111: deltatime = currtime - startuptime;
112: if (deltatime > warmupTime) {
113: // System.out.println("I'm ready!!! deltatime " + deltatime);
114: warmup = false;
115: lasttime = System.currentTimeMillis();
116: }
117: } else {
118: numframes += 1;
119: // System.out.print(maxframes+" "+numframes+" "+lasttime+" ");
120: if (numframes >= maxframes) {
121: currtime = System.currentTimeMillis();
122: deltatime = currtime - lasttime;
123: // System.out.println("deltatime = " + deltatime +
124: // ", numframes = " + numframes);
125: if (deltatime > testduration) {
126: maxframes = (int) Math
127: .ceil((double) numframes
128: * ((double) sampleduration / (double) deltatime));
129: //System.out.println("maxframes = " + maxframes);
130:
131: // reset the value for the measurement
132: doCalibration = false;
133: numframes = 0;
134: lasttime = System.currentTimeMillis();
135: } else {
136: maxframes *= 2;
137: }
138: }
139: }
140: } else { // do the measurement
141: numframes += 1;
142: // System.out.println("PerfBeh : numframes is " + numframes);
143:
144: if (numframes >= maxframes) {
145: currtime = System.currentTimeMillis();
146: deltatime = currtime - lasttime;
147: double fps = (double) numframes
148: / ((double) deltatime / 1000.0);
149:
150: System.out.println("PerfBeh : numframes : " + numframes
151: + " time : " + ((double) deltatime / 1000.0)
152: + " sec." + " fps : " + nf.format(fps));
153: if (finiteLoop) {
154: sumFrames += numframes;
155: sumTimes += deltatime;
156: avgFps = (double) sumFrames * 1000.0
157: / (double) sumTimes;
158: loop++;
159: if (loop >= loopCount) {
160: System.out.println("PerfBeh : Avg fps "
161: + avgFps);
162: if (doStat) {
163: doCalibration = true;
164: loop = 0;
165: numframes = 0;
166: maxframes = 1;
167: currtime = 0;
168: startup = true;
169: warmup = true;
170: lasttime = 0;
171: doStat(avgFps, true);
172: } else {
173: System.out
174: .println("************** The End **************\n");
175: System.exit(0);
176: }
177: }
178: }
179:
180: if (doStat) {
181: doStat(fps, false);
182: }
183:
184: numframes = 0;
185: lasttime = System.currentTimeMillis();
186: ;
187: }
188: }
189:
190: // Set the trigger for the interpolator
191: wakeupOn(FPSwakeup);
192:
193: }
194:
195: public void setFiniteLoop(boolean fl) {
196: finiteLoop = fl;
197: }
198:
199: public void setLoopCount(int lc) {
200: loopCount = lc;
201: }
202:
203: public void doStat(double fps, boolean exit) {
204:
205: }
206:
207: }
|