/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. #import <Foundation/Foundation.h>
  19. #import "GTMDefines.h"
  20. #import <mach/mach_time.h>
  21. // GTMTestTimer is done in straight inline C to avoid obj-c calling overhead.
  22. // It is for doing test timings at very high precision.
  23. // Test Timers have standard CoreFoundation Retain/Release rules.
  24. // Test Timers are not thread safe. Test Timers do NOT check their arguments
  25. // for NULL. You will crash if you pass a NULL argument in.
  26. typedef struct GTMTestTimer {
  27. mach_timebase_info_data_t time_base_info_;
  28. bool running_;
  29. uint64_t start_;
  30. uint64_t split_;
  31. uint64_t elapsed_;
  32. NSUInteger iterations_;
  33. NSUInteger retainCount_;
  34. } GTMTestTimer;
  35. // Create a test timer
  36. GTM_INLINE GTMTestTimer *GTMTestTimerCreate(void) {
  37. GTMTestTimer *t = (GTMTestTimer *)calloc(sizeof(GTMTestTimer), 1);
  38. if (t) {
  39. if (mach_timebase_info(&t->time_base_info_) == KERN_SUCCESS) {
  40. t->retainCount_ = 1;
  41. } else {
  42. // COV_NF_START
  43. free(t);
  44. t = NULL;
  45. // COV_NF_END
  46. }
  47. }
  48. return t;
  49. }
  50. // Retain a timer
  51. GTM_INLINE void GTMTestTimerRetain(GTMTestTimer *t) {
  52. t->retainCount_ += 1;
  53. }
  54. // Release a timer. When release count hits zero, we free it.
  55. GTM_INLINE void GTMTestTimerRelease(GTMTestTimer *t) {
  56. t->retainCount_ -= 1;
  57. if (t->retainCount_ == 0) {
  58. free(t);
  59. }
  60. }
  61. // Starts a timer timing. Specifically starts a new split. If the timer is
  62. // currently running, it resets the start time of the current split.
  63. GTM_INLINE void GTMTestTimerStart(GTMTestTimer *t) {
  64. t->start_ = mach_absolute_time();
  65. t->running_ = true;
  66. }
  67. // Stops a timer and returns split time (time from last start) in nanoseconds.
  68. GTM_INLINE uint64_t GTMTestTimerStop(GTMTestTimer *t) {
  69. uint64_t now = mach_absolute_time();
  70. t->running_ = false;
  71. ++t->iterations_;
  72. t->split_ = now - t->start_;
  73. t->elapsed_ += t->split_;
  74. t->start_ = 0;
  75. return t->split_;
  76. }
  77. // returns the current timer elapsed time (combined value of all splits, plus
  78. // current split if the timer is running) in nanoseconds.
  79. GTM_INLINE double GTMTestTimerGetNanoseconds(GTMTestTimer *t) {
  80. uint64_t total = t->elapsed_;
  81. if (t->running_) {
  82. total += mach_absolute_time() - t->start_;
  83. }
  84. return (double)(total * t->time_base_info_.numer
  85. / t->time_base_info_.denom);
  86. }
  87. // Returns the current timer elapsed time (combined value of all splits, plus
  88. // current split if the timer is running) in seconds.
  89. GTM_INLINE double GTMTestTimerGetSeconds(GTMTestTimer *t) {
  90. return GTMTestTimerGetNanoseconds(t) * 0.000000001;
  91. }
  92. // Returns the current timer elapsed time (combined value of all splits, plus
  93. // current split if the timer is running) in milliseconds.
  94. GTM_INLINE double GTMTestTimerGetMilliseconds(GTMTestTimer *t) {
  95. return GTMTestTimerGetNanoseconds(t) * 0.000001;
  96. }
  97. // Returns the current timer elapsed time (combined value of all splits, plus
  98. // current split if the timer is running) in microseconds.
  99. GTM_INLINE double GTMTestTimerGetMicroseconds(GTMTestTimer *t) {
  100. return GTMTestTimerGetNanoseconds(t) * 0.001;
  101. }
  102. // Returns the number of splits (start-stop) cycles recorded.
  103. // GTMTestTimerGetSeconds()/GTMTestTimerGetIterations() gives you an average
  104. // of all your splits.
  105. GTM_INLINE NSUInteger GTMTestTimerGetIterations(GTMTestTimer *t) {
  106. return t->iterations_;
  107. }
  108. // Returns true if the timer is running.
  109. GTM_INLINE bool GTMTestTimerIsRunning(GTMTestTimer *t) {
  110. return t->running_;
  111. }