All pastes #31820 Raw Edit

Anonymous

public text v1 · immutable
#31820 ·published 2005-11-30 13:53 UTC
rendered paste body
import java.util.HashMap;
import java.util.logging.Logger;

public class Profiler
{
	public static Logger _log = Logger.getLogger(Profiler.class.getName());
	
	private static Profiler _instance;
	private static String IDENT = "          "; 
	
	private HashMap<Integer,Profile> _profiles = new HashMap<Integer,Profile>();
	protected int _numProfileSamples = 10;
	
	protected class Profile
	{
		private String _name;
		private long _time;
		private int _numTest;
		private long[] _tests = new long[_numProfileSamples];
		
		public Profile(String name)
		{
			_name = name;
		}
		
		public void setTime(long time)
		{
			_time = time;
		}
		
		public long getTime()
		{
			return _time;
		}
		
		public String getName()
		{
			return _name;
		}
		
		public void addResult(long value)
		{
			_tests[_numTest] = value;
			_numTest++;
		}
		
		public int getNumTest()
		{
			return _numTest;
		}
		
		public long[] getTests()
		{
			return _tests;
		}
		
	}
	
	public static Profiler getInstance()
	{
		if (_instance == null)
		{
			_instance = new Profiler();
		}
		return _instance;
	}
	
	public void startProfile(String name)
	{
		int hashCode = name.hashCode();
		
		Profile profile = _profiles.get(hashCode);
		
		if (profile != null)
		{
			if (profile.getNumTest() >= _numProfileSamples)
				return;
		}
		else
		{
			profile = new Profile(name);
			_profiles.put(hashCode,profile);
		}
		
		profile.setTime(System.currentTimeMillis());
		
	}
	
	public void endProfile(String name)
	{	
		int hashCode = name.hashCode();
		
		Profile profile = _profiles.get(hashCode);
		
		if (profile == null)
		{
			_log.warning("The profile cannot end because not exists:" + name);
			return;
		}
		
		if (profile.getNumTest() >= _numProfileSamples)
			return;
		
		profile.addResult(System.currentTimeMillis() - profile.getTime());
	}
	
	public String getDumpProfile()
	{
		String content;
		
		content  = "    AVE    |    Min    |    Max    |   #   |  Profile name\n" +
				   "-------------------------------------------------------------\n";
		
		float ave;
		long min,max;
		String temp;
		long[] tests;
		
		for (Profile profile : _profiles.values())
		{
			ave = 0;
			min = Integer.MAX_VALUE;
			max = Integer.MIN_VALUE;
			tests = profile.getTests();
			
			//calculate the average, min and max
			for (int i = 0; i < tests.length;i++)
			{
				ave += tests[i];
				
				if (tests[i] < min)
					min = tests[i];
				
				if (tests[i] > max)
					max = tests[i];
			}
			
			ave /= tests.length;
			
			//Generate the line
			temp = String.format("%.3f",ave/1000f); 
			temp = IDENT.substring(0,IDENT.length() - temp.length()) + temp;
			content += temp + " |";
			
			temp = String.format("%.3f",min/1000f);
			temp = IDENT.substring(0,IDENT.length() - temp.length()) + temp;
			content += temp + " |";
			
			temp = String.format("%.3f",max/1000f);
			temp = IDENT.substring(0,IDENT.length() - temp.length()) + temp;
			content += temp + " |";
			
			temp = String.valueOf(profile.getNumTest());
			temp = IDENT.substring(0,IDENT.length() - temp.length() - 4) + temp;
			content += temp + " |";
			
			content += "   " + profile.getName() + "\n";
		}
		
		return content;
	}
	
	public void Initializate()
	{
		_profiles.clear();
	}
	
	public void Initializate(long miliseconds)
	{
		
	}
	
}