/**
 * Benchmark
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 4/7/2008
 *
 * @projectDescription Utility object to benchmark functions
 *
 * @author Ariel Flesler
 * @version 1.0.0
 */
var benchmark = {
	list:null,
	clocks:{},
	log:function(){
		if( !benchmark.list )
			benchmark.list = document.body.appendChild(document.createElement('ol'));
		
		var text = [].join.call(arguments,' : ');		
		var li = document.createElement('li');
		li.appendChild(document.createTextNode(text));
		benchmark.list.appendChild(li);
	},
	time:function( name ){
		benchmark.clocks[name] = (new Date()).getTime();
	},
	timeEnd:function( name, log ){
		var time = (new Date()).getTime();
		time -= benchmark.clocks[name];
		if( log )
			benchmark.log( name, time + 'ms' );
		return time;
	},
	run:function( times, fn, name, log ){
		benchmark.time(name);
		do fn(); while( --times );
		return benchmark.timeEnd(name,log);
	},
	sequence:function( arr, fn ){
		if( arr.length )
			setTimeout(function(){
				fn( arr.shift() );
				benchmark.sequence( arr, fn );
			}, 500 );
	},
	compare:function( attempts, times, fns, net, wait ){
		if( net ){
			var base = benchmark.run( times, function(){}, 'base' );
			benchmark.log( 'base', base + 'ms' );
		}
		var names = [ ];
		for( var name in fns ) names.push(name);
		
		function test( name ){
			for( var i = 0, sum = 0, min = -1; i < attempts; i++ ){
				var time = benchmark.run( times, fns[name], name );
				if( net )
					time -= base;
				benchmark.log( name, time + 'ms' );
				sum += time;
				if( min == -1 || time < min )
					min = time;
			}
			if( attempts > 1 ){
				benchmark.log( name, 'average', sum / attempts + 'ms' );
				benchmark.log( name, 'minimum', min + 'ms' );
			}
		};
		
		if( wait )
			setTimeout( function(){
				benchmark.sequence( names, test );
			}, wait );
		else
			benchmark.sequence( names, test );
	}	
};

function assert( condition ){
	benchmark.log( 'the #'+ ++assert.count + ' assert ' + ( condition ? 'passed' : 'failed' ));
};
assert.count = 0;
