PageRenderTime 46ms CodeModel.GetById 28ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/core/externals/update-engine/externals/google-toolbox-for-mac/UnitTesting/GTMTestTimer.h

http://macfuse.googlecode.com/
C++ Header | 125 lines | 69 code | 14 blank | 42 comment | 7 complexity | b86c9e68a63ee1c21398e5c80dfa5811 MD5 | raw file
  1//
  2//  GTMTestTimer.h
  3//
  4//  Copyright 2006-2008 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9// 
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11// 
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import <Foundation/Foundation.h>
 20#import "GTMDefines.h"
 21#import <mach/mach_time.h>
 22
 23// GTMTestTimer is done in straight inline C to avoid obj-c calling overhead.
 24// It is for doing test timings at very high precision.
 25// Test Timers have standard CoreFoundation Retain/Release rules.
 26// Test Timers are not thread safe. Test Timers do NOT check their arguments
 27// for NULL. You will crash if you pass a NULL argument in.
 28
 29typedef struct GTMTestTimer {
 30  mach_timebase_info_data_t time_base_info_;
 31  bool running_;
 32  uint64_t start_;
 33  uint64_t split_;
 34  uint64_t elapsed_;
 35  NSUInteger iterations_;
 36  NSUInteger retainCount_;
 37} GTMTestTimer;
 38
 39// Create a test timer
 40GTM_INLINE GTMTestTimer *GTMTestTimerCreate(void) {
 41  GTMTestTimer *t = (GTMTestTimer *)calloc(sizeof(GTMTestTimer), 1);
 42  if (t) {
 43    if (mach_timebase_info(&t->time_base_info_) == KERN_SUCCESS) {
 44      t->retainCount_ = 1;
 45    } else {
 46      // COV_NF_START
 47      free(t);
 48      t = NULL;
 49      // COV_NF_END
 50    }
 51  }
 52  return t;
 53}
 54
 55// Retain a timer
 56GTM_INLINE void GTMTestTimerRetain(GTMTestTimer *t) {
 57  t->retainCount_ += 1;
 58}
 59
 60// Release a timer. When release count hits zero, we free it.
 61GTM_INLINE void GTMTestTimerRelease(GTMTestTimer *t) {
 62  t->retainCount_ -= 1;
 63  if (t->retainCount_ == 0) {
 64    free(t);
 65  }
 66}
 67
 68// Starts a timer timing. Specifically starts a new split. If the timer is
 69// currently running, it resets the start time of the current split.
 70GTM_INLINE void GTMTestTimerStart(GTMTestTimer *t) {
 71  t->start_ = mach_absolute_time();
 72  t->running_ = true;
 73}
 74
 75// Stops a timer and returns split time (time from last start) in nanoseconds.
 76GTM_INLINE uint64_t GTMTestTimerStop(GTMTestTimer *t) {
 77  uint64_t now = mach_absolute_time();
 78  t->running_ = false;
 79  ++t->iterations_;
 80  t->split_ = now - t->start_;
 81  t->elapsed_ += t->split_;
 82  t->start_ = 0;
 83  return t->split_;
 84}
 85
 86// returns the current timer elapsed time (combined value of all splits, plus
 87// current split if the timer is running) in nanoseconds.
 88GTM_INLINE double GTMTestTimerGetNanoseconds(GTMTestTimer *t) {
 89  uint64_t total = t->elapsed_;
 90  if (t->running_) {
 91    total += mach_absolute_time() - t->start_;
 92  }
 93  return (double)(total * t->time_base_info_.numer 
 94                  / t->time_base_info_.denom);
 95}
 96
 97// Returns the current timer elapsed time (combined value of all splits, plus
 98// current split if the timer is running) in seconds.
 99GTM_INLINE double GTMTestTimerGetSeconds(GTMTestTimer *t) {
100  return GTMTestTimerGetNanoseconds(t) * 0.000000001;
101}
102
103// Returns the current timer elapsed time (combined value of all splits, plus
104// current split if the timer is running) in milliseconds.
105GTM_INLINE double GTMTestTimerGetMilliseconds(GTMTestTimer *t) {
106  return GTMTestTimerGetNanoseconds(t) * 0.000001;
107}
108
109// Returns the current timer elapsed time (combined value of all splits, plus
110// current split if the timer is running) in microseconds.
111GTM_INLINE double GTMTestTimerGetMicroseconds(GTMTestTimer *t) {
112  return GTMTestTimerGetNanoseconds(t) * 0.001;
113}
114
115// Returns the number of splits (start-stop) cycles recorded.
116// GTMTestTimerGetSeconds()/GTMTestTimerGetIterations() gives you an average
117// of all your splits.
118GTM_INLINE NSUInteger GTMTestTimerGetIterations(GTMTestTimer *t) {
119  return t->iterations_; 
120}
121
122// Returns true if the timer is running.
123GTM_INLINE bool GTMTestTimerIsRunning(GTMTestTimer *t) { 
124  return t->running_;
125}