1 /***************************************
  2  * Copyright 2011, 2012 GlobWeb contributors.
  3  *
  4  * This file is part of GlobWeb.
  5  *
  6  * GlobWeb is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published by
  8  * the Free Software Foundation, version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * GlobWeb is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with GlobWeb. If not, see <http://www.gnu.org/licenses/>.
 18  ***************************************/
 19 
 20  define( function() {
 21  
 22 /**************************************************************************************************************/
 23 
 24 /** 
 25 	@name Stats
 26 	@class
 27 	Display some rendering statistics in a HTML element
 28 	@param options Configuration properties for Stats.
 29 		<ul>
 30 			<li>element : the HTML element to receivce statistcs, can be a string (the ID) or the DOM element itself</li>
 31 			<li>verbose : the verbosity of the stats, default is false</li>
 32 		</ul>
 33  */
 34 var Stats = function(globe,options)
 35 {
 36 	globe.renderContext.stats = this;
 37 	this.globe = globe;
 38 	
 39 	var elt = options ? options['element'] : undefined;
 40 	if ( elt )
 41 	{	
 42 		if (typeof elt == "string") 
 43 		{
 44 			this.element = document.getElementById(elt);
 45 		}
 46 		else
 47 		{
 48 			this.element = elt;
 49 		}
 50 	}
 51 	
 52 	this.showFPS = globe.renderContext.continuousRendering;
 53 	this.verbose = options && options['verbose'] ? options['verbose'] : false;
 54 	this.numFrames = 0;
 55 	
 56 	var self = this;
 57 	window.setInterval( function() { self.print(); }, 1000 );
 58 }
 59 
 60 /**************************************************************************************************************/
 61 
 62 /** 
 63 	Start measuring time
 64  */
 65 Stats.prototype.start = function(name)
 66 {
 67 	this[name] = Date.now();
 68 }
 69 
 70 /**************************************************************************************************************/
 71 
 72 /** 
 73 	End measuring time
 74  */
 75 Stats.prototype.end = function(name)
 76 {
 77 	var time = Date.now() - this[name];
 78 	
 79 	var max = this["max_"+name] || -1; 
 80 	if (max < time) max = time;
 81 	
 82 	var sum = this["sum_"+name] || 0; 
 83 	sum += time;
 84 	
 85 	this[name] = time;
 86 	this["max_"+name] = max;
 87 	this["sum_"+name] = sum;
 88 	
 89 	if ( name == "globalRenderTime" )
 90 	{
 91 		this.numFrames++;
 92 	}
 93 }
 94 
 95 /**************************************************************************************************************/
 96 
 97 /** 
 98 	Print stats in an HTML element
 99  */
100 Stats.prototype.print = function()
101 {
102 	if ( this.numFrames > 0 )
103 	{
104 		var content = "";
105 		
106 		if ( this.showFPS )
107 		{
108 			content += "FPS : " + this.numFrames + "<br>";
109 		}
110 		
111 		content += "Average render time : " + (this["sum_globalRenderTime"] / this.numFrames).toFixed(2) + " ms";
112 		content += "<br># rendered tiles : " + this.globe.tileManager.tilesToRender.length;
113 		//content += "<br># level zero tiles : " + this.globe.tileManager.level0Tiles.length;
114 		
115 		if ( this.verbose )
116 		{
117 			content += "<br>Average traverse tiles time : " + (this["sum_traverseTime"] / this.numFrames).toFixed(2) + " ms";
118 			content += "<br>Average render tiles time : " + (this["sum_renderTime"] / this.numFrames).toFixed(2) + " ms";
119 			content += "<br>Average generate tiles time : " + (this["sum_generateTime"] / this.numFrames).toFixed(2) + " ms";
120 			content += "<br>Average request tiles time : " + (this["sum_requestTime"] / this.numFrames).toFixed(2) + " ms";
121 			content += "<br>Max render time : " + this["max_globalRenderTime"] + " ms";
122 			content += "<br>Max traverse tiles time : " + this["max_traverseTime"] + " ms";
123 			content += "<br>Max render tiles time : " + this["max_renderTime"] + " ms";
124 			content += "<br>Max generate tiles time : " + this["max_generateTime"]  + " ms";
125 			content += "<br>Max request tiles time : " + this["max_requestTime"] + " ms";
126 		}
127 		
128 		this.element.innerHTML = content;
129 		
130 		this["sum_globalRenderTime"] = 0;
131 		this["sum_traverseTime"] = 0;
132 		this["sum_renderTime"] = 0;
133 		this["sum_generateTime"] = 0;
134 		this["sum_requestTime"] = 0;
135 		this["max_globalRenderTime"] = 0;
136 		this["max_traverseTime"] = 0;
137 		this["max_renderTime"] = 0;
138 		this["max_generateTime"] = 0;
139 		this["max_requestTime"] = 0;
140 		this.numFrames = 0;
141 	}
142 }
143 
144 /**************************************************************************************************************/
145 
146 return Stats;
147 
148 });
149