Home | History | Annotate | Download | only in profiler
      1 /**
      2  * Copyright (c) 2004-2011 QOS.ch
      3  * All rights reserved.
      4  *
      5  * Permission is hereby granted, free  of charge, to any person obtaining
      6  * a  copy  of this  software  and  associated  documentation files  (the
      7  * "Software"), to  deal in  the Software without  restriction, including
      8  * without limitation  the rights to  use, copy, modify,  merge, publish,
      9  * distribute,  sublicense, and/or sell  copies of  the Software,  and to
     10  * permit persons to whom the Software  is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The  above  copyright  notice  and  this permission  notice  shall  be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
     17  * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
     18  * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21  * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
     22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  */
     25 package org.slf4j.profiler;
     26 
     27 import java.text.DecimalFormat;
     28 
     29 /**
     30  *
     31  * A collection of utility methods.
     32  *
     33  * @author Ceki Gülcü
     34  *
     35  */
     36 class Util {
     37 
     38     static final long NANOS_IN_ONE_MICROSECOND = 1000;
     39     static final long NANOS_IN_ONE_MILLISECOND = NANOS_IN_ONE_MICROSECOND * 1000;
     40     static final long NANOS_IN_ONE_SECOND = NANOS_IN_ONE_MILLISECOND * 1000;
     41     private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.000");
     42 
     43     static DurationUnit selectDurationUnitForDisplay(StopWatch sw) {
     44         return selectDurationUnitForDisplay(sw.elapsedTime());
     45     }
     46 
     47     static DurationUnit selectDurationUnitForDisplay(long durationInNanos) {
     48         if (durationInNanos < 10 * NANOS_IN_ONE_MICROSECOND) {
     49             return DurationUnit.NANOSECOND;
     50         } else if (durationInNanos < 10 * NANOS_IN_ONE_MILLISECOND) {
     51             return DurationUnit.MICROSECOND;
     52         } else if (durationInNanos < 10 * NANOS_IN_ONE_SECOND) {
     53             return DurationUnit.MILLISSECOND;
     54         } else {
     55             return DurationUnit.SECOND;
     56         }
     57     }
     58 
     59     static public double convertToMicros(long nanos) {
     60         return (double) nanos / NANOS_IN_ONE_MICROSECOND;
     61     }
     62 
     63     static public double convertToMillis(long nanos) {
     64         return (double) nanos / NANOS_IN_ONE_MILLISECOND;
     65     }
     66 
     67     static public double convertToSeconds(long nanos) {
     68         return ((double) nanos / NANOS_IN_ONE_SECOND);
     69     }
     70 
     71     static String durationInDurationUnitsAsStr(StringBuilder buf, StopWatch sw) {
     72         DurationUnit du = selectDurationUnitForDisplay(sw);
     73         return durationInDurationUnitsAsStr(sw.elapsedTime(), du);
     74     }
     75 
     76     static String durationInDurationUnitsAsStr(long nanos, DurationUnit durationUnit) {
     77         StringBuilder buf = new StringBuilder();
     78         switch (durationUnit) {
     79         case NANOSECOND:
     80             buf.append(nanos);
     81             break;
     82         case MICROSECOND:
     83             double micros = convertToMicros(nanos);
     84             buf.append(DECIMAL_FORMAT.format(micros));
     85             break;
     86         case MILLISSECOND:
     87             double millis = convertToMillis(nanos);
     88             buf.append(DECIMAL_FORMAT.format(millis));
     89             break;
     90         case SECOND:
     91             double seconds = convertToSeconds(nanos);
     92             buf.append(DECIMAL_FORMAT.format(seconds));
     93             break;
     94         }
     95         return buf.toString();
     96     }
     97 
     98     static void appendDurationUnitAsStr(StringBuilder buf, DurationUnit durationUnit) {
     99         switch (durationUnit) {
    100         case NANOSECOND:
    101             buf.append("nanoseconds.");
    102             break;
    103         case MICROSECOND:
    104             buf.append("microseconds.");
    105             break;
    106         case MILLISSECOND:
    107             buf.append("milliseconds.");
    108             break;
    109         case SECOND:
    110             buf.append(" seconds.");
    111             break;
    112         }
    113     }
    114 }
    115