|
From: Brendan M. <mc...@us...> - 2006-08-10 22:53:39
|
Update of /cvsroot/jigs/jigs/src/edu/whitman/halfway/util In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv11662/src/edu/whitman/halfway/util Modified Files: TimedQueue.java CplexUtil.java StringUtil.java ExecutionTimer.java Added Files: Timing.java Log Message: --- NEW FILE: Timing.java --- package edu.whitman.halfway.util; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; /** * Maintains a Map from strings (usually in a dotted notation) to ExecutionTimer * objects. Provides a useful way to print and manage a collection of named * ExcutionTimers for program instrumentation. * * Also provides a single static Timing object, Timing.t, which can be used for * convience for a single common set of timers for the currently running * application. Note: the instance that Timing.t can be changed. This is * useful if, say, individual timings for each iteration of a loop are need: * the code called inside the loop refers only to Timing.t, and the at the * beginning of each iteration a new Execution Timer is assigned to Timing.t. * * @author mcmahan * */ public class Timing { protected static Logger log = Logger.getLogger(Timing.class); public static Timing t = new Timing(); protected Map map = new HashMap(); public static void setStaticInstance(Timing t) { Timing.t = t; } /** Gets a timer from the map of name, or creates it if needed. */ protected ExecutionTimer getOrCreate(String name){ Object obj = map.get(name); if(obj != null) return (ExecutionTimer) obj; ExecutionTimer timer = new ExecutionTimer(name); map.put(name, timer); return timer; } protected ExecutionTimer get(String name){ return (ExecutionTimer) map.get(name); } /** * Starts the timer if it exists (without reseting), or creates and starts a * timer if it doesn't already exist. * @param name */ public void start(String name){ ExecutionTimer timer = getOrCreate(name); timer.start(); } public long stop(String name){ //we want to be as precise as possible, so we first read off the stop time // and only then look up the timer and call stop with the given time. long stopTime = System.currentTimeMillis(); ExecutionTimer timer = get(name); if(timer == null){ log.warn("Timer " + name + " doesn't exist, so can't stop it."); return -1; } return timer.stop(); } public long getElapsedTime(String name){ ExecutionTimer timer = get(name); if(timer == null){ log.warn("Timer " + name + " doesn't exist, so can't read from it."); return -1; } return timer.getElapsedTime(); } public boolean isRunning(String name){ ExecutionTimer timer = get(name); if(timer == null){ log.warn("Timer " + name + " doesn't exist, it can't be running."); return false; } return timer.isRunning(); } public String getAsString(String name){ ExecutionTimer timer = get(name); if(timer == null){ log.warn("Timer " + name + " doesn't exist, so can't read from it."); return "[invalid timer]"; } return timer.toString(); } public void incTime(String name, long ms){ ExecutionTimer timer = get(name); if(timer == null){ log.warn("Timer " + name + " doesn't exist, so can't increment it."); } timer.incTime(ms); } public int numRunning(){ int numRunning = 0; for(Object t : map.values()){ ExecutionTimer et = (ExecutionTimer) t; if(et.isRunning()) numRunning++; } return numRunning; } /** * Resets the timer to zero, stopping it if it is running. * If the timer doesn't already exist, it is created (but not started). * @param name */ public void reset(String name){ ExecutionTimer timer = getOrCreate(name); timer.reset(); } public String toString(){ ArrayList keys = new ArrayList(map.keySet()); Collections.sort(keys); int maxLength = 0; for(Object obj : keys){ String key = (String)obj; maxLength = (key.length() > maxLength) ? key.length() : maxLength; } StringBuffer sb = new StringBuffer(); String format = "%"+maxLength +"s = %s%n"; for(Object obj : keys){ String key = (String)obj; sb.append(String.format(format, key, get(key))); } return sb.toString(); } public static long sumTimes(String name, Collection timings){ long totalTimeMS = 0; for(Object obj : timings){ Timing t = (Timing) obj; totalTimeMS += t.getElapsedTime(name); } return totalTimeMS; } public static Timing sumTimings(Collection timings){ if(timings.size()==0) return new Timing(); Set keys = ((Timing)timings.iterator().next()).map.keySet(); Timing timing = new Timing(); for(Object k : keys){ String key = (String) k; long total = sumTimes(key, timings); timing.reset(key); timing.incTime(key, total); } return timing; } public static void main(String[] args){ long[] time = { 100, 13011, 60*60*1000*59 + 60*1000*32 + 1000*49 + 333, 60*60*1000*0 + 60*1000*32 + 1000*49 + 333, 60*60*1000*0 + 60*1000*0 + 1000*49 + 3, 60*60*1000*90 + 60*1000*0 + 1000*49 + 3}; String[] name = { "foo", "cplex.total", "cplex.build_problem", "row_oracle.compute", "funk.job", "zstart"}; //Timing t = new Timing(); for(int i=0; i<time.length; i++){ System.out.printf("adding %s with %s%n", name[i], time[i]); Timing.t.reset(name[i]); Timing.t.incTime(name[i], time[i]); } System.out.println(Timing.t); } } Index: TimedQueue.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/TimedQueue.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** TimedQueue.java 6 Apr 2004 01:58:32 -0000 1.1 --- TimedQueue.java 10 Aug 2006 22:53:35 -0000 1.2 *************** *** 6,9 **** --- 6,10 ---- + /** Wrapper around a general int queue that times time spent by queue. Index: CplexUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/CplexUtil.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** CplexUtil.java 12 Jan 2004 20:06:43 -0000 1.6 --- CplexUtil.java 10 Aug 2006 22:53:35 -0000 1.7 *************** *** 12,16 **** import org.apache.log4j.Logger; ! public class CplexUtil{ --- 12,21 ---- import org.apache.log4j.Logger; ! /** ! * Old way of accessing CPLEx via file system. ! * @deprecated ! * @author mcmahan ! * ! */ public class CplexUtil{ Index: StringUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/StringUtil.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** StringUtil.java 6 Aug 2006 18:35:30 -0000 1.11 --- StringUtil.java 10 Aug 2006 22:53:35 -0000 1.12 *************** *** 9,13 **** public final class StringUtil{ public static final String newline = System.getProperty("line.separator"); - public static final Formatter f = new Formatter(); static PatternMatcher matcher = new Perl5Matcher(); --- 9,12 ---- *************** *** 17,20 **** --- 16,45 ---- static Logger log = Logger.getLogger(StringUtil.class.getName()); + + public static final String formatMillis(long ms){ + String w = " "; + long totalMillis = ms; + long sec = ms / 1000; + ms = ms % 1000; + if(sec == 0){ + return String.format("%s%s%s %03dms",w,w,w, ms); + } + long min = sec / 60; + sec = sec % 60; + if (min ==0){ + return String.format("%s%s%02ds %03dms",w,w, sec, ms); + } + long hr = min / 60; + min = min % 60; + if (hr == 0){ + return String.format("%s%02dm:%02ds %03dms", w, min, sec, ms); + } + + assert 60*60*1000*hr + 60*1000*min + 1000*sec + ms == totalMillis; + + + return String.format("%dh:%02dm:%02ds %03dms", hr, min, sec, ms); + + } public static final String stripLeadingNumsUnderscore(String s){ *************** *** 277,280 **** --- 302,316 ---- } + public static void main(String[] args){ + long[] times = { 100, 13011, + 60*60*1000*59 + 60*1000*32 + 1000*49 + 333, + 60*60*1000*0 + 60*1000*32 + 1000*49 + 333, + 60*60*1000*0 + 60*1000*0 + 1000*49 + 3, + 60*60*1000*90 + 60*1000*0 + 1000*49 + 3, + }; + for (long t : times){ + System.out.println(StringUtil.formatMillis(t)); + } + } Index: ExecutionTimer.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/ExecutionTimer.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ExecutionTimer.java 28 Feb 2005 15:50:53 -0000 1.3 --- ExecutionTimer.java 10 Aug 2006 22:53:35 -0000 1.4 *************** *** 1,5 **** --- 1,8 ---- package edu.whitman.halfway.util; + import org.apache.log4j.Logger; + public final class ExecutionTimer{ + protected static Logger log = Logger.getLogger(ExecutionTimer.class); boolean isRunning = false; *************** *** 7,10 **** --- 10,36 ---- long lastStartTime = 0; + protected String name; + + public ExecutionTimer(){ + this("Unnamed"); + } + + public ExecutionTimer(String name){ + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isRunning(){ + return isRunning; + } + + public final void reset(){ isRunning = false; *************** *** 15,19 **** public final void start(){ ! if(isRunning) throw new IllegalArgumentException("Timer is already running."); isRunning = true; lastStartTime = System.currentTimeMillis(); --- 41,50 ---- public final void start(){ ! if(isRunning){ ! log.warn(String.format( ! "Timer %s is already running, ignoring call to start.", ! getName())); ! //throw new IllegalArgumentException("Timer is already running."); ! } isRunning = true; lastStartTime = System.currentTimeMillis(); *************** *** 21,26 **** public long stop(){ ! long stopTime = System.currentTimeMillis(); ! if(!isRunning) throw new IllegalArgumentException("Can't stop a timer that isn't running"); long delta = stopTime - lastStartTime; elapsedTime += delta; --- 52,67 ---- public long stop(){ ! return stop(System.currentTimeMillis()); ! } ! ! public void incTime(long ms){ ! elapsedTime += ms; ! } ! ! public long stop(long stopTime){ ! if(!isRunning){ ! log.warn("Timer " + getName() + " isn't running, so can't stop it."); ! //throw new IllegalArgumentException("Can't stop a timer that isn't running"); ! } long delta = stopTime - lastStartTime; elapsedTime += delta; *************** *** 33,35 **** --- 74,80 ---- return elapsedTime + (System.currentTimeMillis() - lastStartTime); } + + public String toString(){ + return StringUtil.formatMillis(getElapsedTime()); + } } |