001 /*
002 * $Id: LogWatch.java,v 1.6 2014/04/21 08:44:43 oboehm Exp $
003 *
004 * Copyright (c) 2014 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 17.03.2014 by oliver (ob@oasd.de)
019 */
020
021 package patterntesting.runtime.log;
022
023 import java.util.Locale;
024
025 import org.apache.commons.lang.time.StopWatch;
026 import org.slf4j.*;
027
028 import patterntesting.runtime.util.Converter;
029
030
031 /**
032 * The Class LogWatch is a simple stop watch to be able to measure and log
033 * code segments which need a little bit longer.
034 *
035 * @author oliver
036 * @since 1.4.1 (17.03.2014)
037 * @version $Revision: 1.6 $
038 */
039 public final class LogWatch extends StopWatch {
040
041 private static final Logger log = LoggerFactory.getLogger(LogWatch.class);
042 private long nanoStartTime;
043 private long nanoEndTime;
044
045 /**
046 * Instantiates a new log watch.
047 */
048 public LogWatch() {
049 super();
050 this.start();
051 }
052
053 /**
054 * Start.
055 *
056 * @see org.apache.commons.lang.time.StopWatch#start()
057 */
058 @Override
059 public void start() {
060 this.reset();
061 super.start();
062 this.nanoStartTime = System.nanoTime();
063 }
064
065 /**
066 * Stop.
067 *
068 * @see org.apache.commons.lang.time.StopWatch#stop()
069 */
070 @Override
071 public void stop() {
072 this.nanoEndTime = System.nanoTime();
073 super.stop();
074 }
075
076 /**
077 * Reset.
078 *
079 * @see org.apache.commons.lang.time.StopWatch#reset()
080 */
081 @Override
082 public void reset() {
083 super.reset();
084 this.nanoStartTime = 0L;
085 this.nanoEndTime = 0L;
086 }
087
088 /**
089 * Gets the elapsed time from the start call. This method is a convenience
090 * method if you are coming from Perf4J or Speed4J. It also allows us to
091 * switch to one of these frameworks if it may be necessary.
092 *
093 * @return the elapsed time in milliseconds
094 */
095 public long getElapsedTime() {
096 return this.getTime();
097 }
098
099 /**
100 * Gets the elapsed time from the start call in nano seconds.
101 *
102 * @return the nano time
103 * @deprecated use {@link #getTimeInNanos()}
104 */
105 @Deprecated
106 public long getNanoTime() {
107 return this.getTimeInNanos();
108 }
109
110 /**
111 * Gets the elapsed time from the start call in nano seconds.
112 * <p>
113 * This method was called "getNanoTime" before but was now named in
114 * "getTimeInNanons" to fit better in the naming schema - there is a similar
115 * method {@link #getTimeInMillis()}.
116 * </p>
117 *
118 * @return the nano time
119 * @since 1.4.2
120 */
121 public long getTimeInNanos() {
122 long endTime = (this.nanoEndTime == 0) ? System.nanoTime() : this.nanoEndTime;
123 long nanoTime = endTime - this.nanoStartTime;
124 if (nanoTime < 0) {
125 long milliTime = this.getTime();
126 log.info("Will use {} ms as fallback because there was an overflow with nanoTime.", milliTime);
127 nanoTime = 1000000 * milliTime;
128 }
129 return nanoTime;
130 }
131
132 /**
133 * Gets the elapsed time from the start call in milli seconds.
134 *
135 * @return the time in millis
136 * @since 1.4.2
137 */
138 public double getTimeInMillis() {
139 return this.getTimeInNanos() / 1000000.0;
140 }
141
142 /**
143 * To string.
144 *
145 * @return the string
146 * @see Object#toString()
147 */
148 @Override
149 public String toString() {
150 double millis = this.getTimeInMillis();
151 if (millis > 6000000.0) {
152 return super.toString();
153 }
154 return Converter.getTimeAsString(millis, Locale.ENGLISH);
155 }
156
157 }