/**
 * 调试工具类
 * 统计运行时间的毫秒数，统计对象包括：
 *  执行次数，总耗时，耗时最短5个时间，耗时最长5个时间
 *  @author robinz,wt
 */
function MillsCounter(size) {
	if (size) {
		this.size = size;
	} else {
		this.size = 5;//var DEFAULT_SIZE = 5;
	}
	this.millisCount = 0;
	this.runCount = 0;
	this.shortest = new Array;
	this.longest = new Array;
	for (var i = 0; i < this.size; i++) {
		this.shortest.push(10000) ;//var MAX_VALUE = 10000;
		this.longest.push(0);
	}
}
MillsCounter.prototype.count = function(mills){
	this.millisCount += mills;
	this.runCount++;
	this.doShort(mills);
	this.doLong(mills);
};
MillsCounter.prototype.doLong = function(mills){
	var longValue=Number(this.shortest[0]);
	if (longValue < mills) {
		this.longest.shift();
		this.longest.unshift(mills);
		this.longest.sort();
	}
};
MillsCounter.prototype.doShort = function(mills){
	var shortValue=Number(this.shortest[this.size - 1]);
	if (shortValue > mills) {
		this.shortest.pop();
		this.shortest.push(mills);
		this.shortest.sort();
	}
};
MillsCounter.prototype.getRunCount = function(){
	return Number(this.runCount).toFixed(0);
};
MillsCounter.prototype.getMillsCount = function(){
	return Number(this.millisCount).toFixed(0);
};
MillsCounter.prototype.getAvgMills = function(){
	return Number(this.runCount == 0? 0 : this.millisCount/this.runCount).toFixed(2);
};
MillsCounter.prototype.getShortestMills = function(){
	return Number(this.shortest[0]).toFixed(0);
};
MillsCounter.prototype.getLongestMills = function(){
	return Number(this.longest[this.size - 1]).toFixed(0);
};
MillsCounter.prototype.toString = function(){
	var sb = new StringBuffer();
	sb.append("Avg/Total/Num: ");
	sb.append(this.getAvgMills());
	sb.append("/");
	sb.append(this.getMillsCount());
	sb.append("/");
	sb.append(this.getRunCount());
	sb.append(";");
	sb.append("Longest(");
	sb.append(this.size);
	sb.append("): ");
	
	for (var i = 0;i<this.size - 1;i++) {
		sb.append(this.longest[i]);
		sb.append("/");
	}
	sb.append(this.longest[this.size - 1]);
	sb.append(";");
	sb.append("Shortest(");
	sb.append(this.size);
	sb.append("): ");
	
	for (var i = 0; i < this.size - 1; i++) {
		sb.append(this.shortest[i]);
		sb.append("/");
	}
	
	sb.append(this.shortest[this.size - 1]);
	sb.append(";");
	return sb.toString();
};


function StringBuffer() {
    this._strings_ = new Array;
}
StringBuffer.prototype.append = function(str) {
    this._strings_.push(str);
};
StringBuffer.prototype.toString = function() {
    return this._strings_.join("");
};


