Home | History | Annotate | Download | only in profiler
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Test of classes in tracked_time.cc
      6 
      7 #include "base/profiler/tracked_time.h"
      8 #include "base/time/time.h"
      9 #include "base/tracked_objects.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace tracked_objects {
     13 
     14 TEST(TrackedTimeTest, TrackedTimerMilliseconds) {
     15   // First make sure we basicallly transfer simple milliseconds values as
     16   // expected.  Most critically, things should not become null.
     17   int32 kSomeMilliseconds = 243;  // Some example times.
     18   int64 kReallyBigMilliseconds = (1LL << 35) + kSomeMilliseconds;
     19 
     20   TrackedTime some = TrackedTime() +
     21       Duration::FromMilliseconds(kSomeMilliseconds);
     22   EXPECT_EQ(kSomeMilliseconds, (some - TrackedTime()).InMilliseconds());
     23   EXPECT_FALSE(some.is_null());
     24 
     25   // Now create a big time, to check that it is wrapped modulo 2^32.
     26   base::TimeTicks big = base::TimeTicks() +
     27       base::TimeDelta::FromMilliseconds(kReallyBigMilliseconds);
     28   EXPECT_EQ(kReallyBigMilliseconds, (big - base::TimeTicks()).InMilliseconds());
     29 
     30   TrackedTime wrapped_big(big);
     31   // Expect wrapping at 32 bits.
     32   EXPECT_EQ(kSomeMilliseconds, (wrapped_big - TrackedTime()).InMilliseconds());
     33 }
     34 
     35 TEST(TrackedTimeTest, TrackedTimerDuration) {
     36   int kFirstMilliseconds = 793;
     37   int kSecondMilliseconds = 14889;
     38 
     39   Duration first = Duration::FromMilliseconds(kFirstMilliseconds);
     40   Duration second = Duration::FromMilliseconds(kSecondMilliseconds);
     41 
     42   EXPECT_EQ(kFirstMilliseconds, first.InMilliseconds());
     43   EXPECT_EQ(kSecondMilliseconds, second.InMilliseconds());
     44 
     45   Duration sum = first + second;
     46   EXPECT_EQ(kFirstMilliseconds + kSecondMilliseconds, sum.InMilliseconds());
     47 }
     48 
     49 TEST(TrackedTimeTest, TrackedTimerVsTimeTicks) {
     50   // Make sure that our 32 bit timer is aligned with the TimeTicks() timer.
     51 
     52   // First get a 64 bit timer (which should not be null).
     53   base::TimeTicks ticks_before = base::TimeTicks::Now();
     54   EXPECT_FALSE(ticks_before.is_null());
     55 
     56   // Then get a 32 bit timer that can be be null when it wraps.
     57   TrackedTime now = TrackedTime::Now();
     58 
     59   // Then get a bracketing time.
     60   base::TimeTicks ticks_after = base::TimeTicks::Now();
     61   EXPECT_FALSE(ticks_after.is_null());
     62 
     63   // Now make sure that we bracketed our tracked time nicely.
     64   Duration before = now - TrackedTime(ticks_before);
     65   EXPECT_LE(0, before.InMilliseconds());
     66   Duration after = now - TrackedTime(ticks_after);
     67   EXPECT_GE(0, after.InMilliseconds());
     68 }
     69 
     70 TEST(TrackedTimeTest, TrackedTimerDisabled) {
     71   // Check to be sure disabling the collection of data induces a null time
     72   // (which we know will return much faster).
     73   if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED))
     74     return;
     75   // Since we disabled tracking, we should get a null response.
     76   TrackedTime track_now = ThreadData::Now();
     77   EXPECT_TRUE(track_now.is_null());
     78   track_now = ThreadData::NowForStartOfRun(NULL);
     79   EXPECT_TRUE(track_now.is_null());
     80   track_now = ThreadData::NowForEndOfRun();
     81   EXPECT_TRUE(track_now.is_null());
     82 }
     83 
     84 TEST(TrackedTimeTest, TrackedTimerEnabled) {
     85   if (!ThreadData::InitializeAndSetTrackingStatus(
     86       ThreadData::PROFILING_CHILDREN_ACTIVE))
     87     return;
     88   // Make sure that when we enable tracking, we get a real timer result.
     89 
     90   // First get a 64 bit timer (which should not be null).
     91   base::TimeTicks ticks_before = base::TimeTicks::Now();
     92   EXPECT_FALSE(ticks_before.is_null());
     93 
     94   // Then get a 32 bit timer that can be null when it wraps.
     95   // Crtical difference from  the TrackedTimerVsTimeTicks test, is that we use
     96   // ThreadData::Now().  It can sometimes return the null time.
     97   TrackedTime now = ThreadData::Now();
     98 
     99   // Then get a bracketing time.
    100   base::TimeTicks ticks_after = base::TimeTicks::Now();
    101   EXPECT_FALSE(ticks_after.is_null());
    102 
    103   // Now make sure that we bracketed our tracked time nicely.
    104   Duration before = now - TrackedTime(ticks_before);
    105   EXPECT_LE(0, before.InMilliseconds());
    106   Duration after = now - TrackedTime(ticks_after);
    107   EXPECT_GE(0, after.InMilliseconds());
    108 }
    109 
    110 }  // namespace tracked_objects
    111