/** * Indicate that an elapsed time sample should be taken. The sample is the time that has passed * since the last start() call with the name timer name (the argument key). It is an error to * call stop if there has been no corresponding call to start. * * @param key The name of the timer with which to record the new elapsed time sample. * @param dataAmount The amount of data transfered during this interval. * @return The length of the measured interval, in msec. */ public static double stop(String key, long dataAmount) { if (key == null) throw new RuntimeException("ElapsedTime key can't be null"); TransferRateInterval entry = mSampleSetManager.get(key); if (entry == null || entry.mStartTime < 0) throw new RuntimeException("stop(" + key + ") called but there was no matching start"); double timeSample = (System.nanoTime() - entry.mStartTime) * MSEC_SCALE; entry.mStartTime = -1; entry.addSample(timeSample, dataAmount); return timeSample > 0.0 ? dataAmount / timeSample : Double.MAX_VALUE; }
/** * Indicates that the elapsed time since start() was called is no longer of interest, and so no * new sample should be generated by ending the measurement interval. This might happen, for * instance, if some exceptional condition occurs. Note that it is not an error to abort a timer * that hasn't been started. (This makes it easier to catch errors over a long code sequence, as * you don't have to keep track of just which timers have and which haven't yet been started.) * * @param key The name of a timer. * @return The length of the measured interval, in msec. */ public static double abort(String key) { if (key == null) throw new RuntimeException("ElapsedTime key can't be null"); ElapsedTimeInterval entry = mSampleSetManager.get(key); if (entry == null || entry.mStartTime < 0) return 0.0; double sample = (System.nanoTime() - entry.mStartTime) * MSEC_SCALE; entry.mStartTime = -1; entry.abort(); return sample; }
/** * Indicate that an elapsed time sample should be taken. The sample is the time that has passed * since the last start() call with the name timer name (the argument key). It is an error to * call stop if there has been no corresponding call to start. * * @param key The name of the timer with which to record the new elapsed time sample. * @return The length of the measured interval, in msec. */ public static double stop(String key) { if (key == null) throw new RuntimeException("ElapsedTime key can't be null"); ElapsedTimeInterval entry = mSampleSetManager.get(key); if (entry == null || entry.mStartTime < 0) throw new RuntimeException("stop(" + key + ") called but there was no matching start"); double sample = (System.nanoTime() - entry.mStartTime) * MSEC_SCALE; entry.mStartTime = -1; entry.addSample(sample); return sample; }
/** * Indicates that the elapsed time since start() was called is no longer of interest, and so no * new sample should be generated by ending the measurement interval. This might happen, for * instance, if some exceptional condition occurs. Note that it is not an error to abort a timer * that hasn't been started. (This makes it easier to catch errors over a long code sequence, as * you don't have to keep track of just which timers have and which haven't yet been started.) * * @param key The name of a timer. * @return The length of the measured interval, in msec. */ public static double abort(String key, long dataAmount) { if (key == null) throw new RuntimeException("ElapsedTime key can't be null"); TransferRateInterval entry = mSampleSetManager.get(key); if (entry == null || entry.mStartTime < 0) return 0.0; double sample = (System.nanoTime() - entry.mStartTime) * MSEC_SCALE; sample = sample > 0.0 ? sample = dataAmount / sample : Double.MAX_VALUE; entry.mStartTime = -1; entry.abort(); return sample; }
/** * Indicate the start of an elapsed time interval. The interval should be terminated by calling * stop() or abort(). * * @param key The arbitary name of a timer (e.g., ElapsedTime.start("foo")). */ public static void start(String key) { if (key == null) throw new RuntimeException("ElapsedTime key can't be null"); TransferRateInterval entry = mSampleSetManager.get(key); if (entry == null) { entry = new TransferRateInterval(); mSampleSetManager.put(key, entry); } if (entry.mStartTime > 0) { throw new RuntimeException( "start(" + key + ") called but am already had a start call with no matching stop"); } entry.mStartTime = System.nanoTime(); }
public static TransferRateInterval get(String key) { if (key == null) throw new RuntimeException("ElapsedTime key can't be null"); return mSampleSetManager.get(key); }