View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.support;
    14
    15  import java.io.PrintStream;
    16  import java.util.ArrayList;
    17  import java.util.List;
    18
    19  /***
    20   * Utility class for measuring and dumping timestamps
    21   *
    22   * @author Ole.Matzura
    23   */
    24
    25  public class Timestamps
    26  {
    27  	private static ThreadLocal<Timestamps> timestamps = new ThreadLocal<Timestamps>();
    28
    29  	private List<String> labels = new ArrayList<String>();
    30  	private List<Long> times = new ArrayList<Long>();
    31
    32  	private long startTime;
    33
    34  	private Timestamps()
    35  	{
    36  		startTime = System.nanoTime();
    37  	}
    38
    39  	public static void init()
    40  	{
    41  		timestamps.set( new Timestamps() );
    42  	}
    43
    44  	public static void addTimestamp( String label )
    45  	{
    46  		timestamps.get().add( label );
    47  	}
    48
    49  	public synchronized static void dump()
    50  	{
    51  		timestamps.get().dumpResult( System.out );
    52  	}
    53
    54  	public static void dump( PrintStream out )
    55  	{
    56  		timestamps.get().dumpResult( out );
    57  	}
    58
    59  	private void add( String label )
    60  	{
    61  		times.add( System.nanoTime() );
    62  		labels.add( label );
    63  	}
    64
    65  	private void dumpResult( PrintStream out )
    66  	{
    67  		out.println( "Timestamps result:" );
    68  		long last = startTime;
    69  		for( int c = 0; c < labels.size(); c++ )
    70  		{
    71  			out.println( labels.get( c ) + "; " + (times.get(c)-startTime)/1000000 );
    72  			last = times.get(c);
    73  		}
    74  	}
    75
    76  	public static long getTimeTaken()
    77  	{
    78  		return timestamps.get().getInternalTimeTaken();
    79  	}
    80
    81  	private long getInternalTimeTaken()
    82  	{
    83  		if( labels.isEmpty())
    84  			return 0;
    85
    86  		return (times.get( times.size()-1 )-startTime) / 1000000;
    87  	}
    88  }