Coverage Report - org.webslinger.lang.StopWatch
 
Classes in this File Line Coverage Branch Coverage Complexity
StopWatch
0%
0/12
0%
0/1
0
StopWatch$1
N/A
N/A
0
StopWatch$ThreadStopWatch
0%
0/23
0%
0/1
0
 
 1  
 package org.webslinger.lang;
 2  
 
 3  
 import java.util.Formatter;
 4  
 
 5  0
 public final class StopWatch {
 6  0
     private static final Formatter formatter = new Formatter(System.err);
 7  0
     private static final ThreadLocal<ThreadStopWatch> tl = new ThreadLocal<ThreadStopWatch>();
 8  
 
 9  0
     private StopWatch() {
 10  0
     }
 11  
 
 12  
     public static final long mark(String format, Object... args) {
 13  0
         return mark(0, format, args);
 14  
     }
 15  
 
 16  
     public static final long mark(long lastMarkTime, String format, Object... args) {
 17  0
         ThreadStopWatch tsw = tl.get();
 18  0
         if (tsw == null) {
 19  0
             tsw = new ThreadStopWatch();
 20  0
             ExecutionPool.addAtExit(tsw);
 21  0
             tl.set(tsw);
 22  
         }
 23  0
         return tsw.mark(lastMarkTime, format, args);
 24  
     }
 25  
 
 26  0
     private static final class ThreadStopWatch implements ThreadSingletonState {
 27  0
         private final long startTime = System.nanoTime();
 28  0
         private long lastTime = startTime;
 29  0
         private final String threadName = Thread.currentThread().getName();
 30  
 
 31  
         public final void start() {
 32  0
             mark(0, "thread start");
 33  0
         }
 34  
 
 35  
         public final void finish() {
 36  0
             long diff = System.nanoTime() - startTime;
 37  0
             synchronized (formatter) {
 38  0
                 formatter.format("%09.6f[%s]: thread done\n", ((double)diff / (double)1000000000), threadName);
 39  0
             }
 40  0
             tl.set(null);
 41  0
         }
 42  
 
 43  
         protected final long mark(long lastMarkTime, String format, Object... args) {
 44  0
             long curTime = System.nanoTime();
 45  0
             long diff = curTime - lastTime;
 46  0
             lastTime = curTime;
 47  0
             synchronized (formatter) {
 48  0
                 if (lastMarkTime != 0) {
 49  0
                     formatter.format("%09.6f(%09.6f)[%s]: ", ((double)diff / (double)1000000000), ((double)(curTime - lastMarkTime) / (double)1000000000), threadName);
 50  
                 } else {
 51  0
                     formatter.format("%09.6f[%s]: ", ((double)diff / (double)1000000000), threadName);
 52  
                 }
 53  0
                 formatter.format(format, args);
 54  0
                 System.err.println();
 55  0
             }
 56  0
             return curTime;
 57  
         }
 58  
     }
 59  
 }