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
12namespace tracked_objects {
13
14TEST(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
35TEST(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
49TEST(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
70TEST(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}
79
80TEST(TrackedTimeTest, TrackedTimerEnabled) {
81  if (!ThreadData::InitializeAndSetTrackingStatus(
82      ThreadData::PROFILING_CHILDREN_ACTIVE))
83    return;
84  // Make sure that when we enable tracking, we get a real timer result.
85
86  // First get a 64 bit timer (which should not be null).
87  base::TimeTicks ticks_before = base::TimeTicks::Now();
88  EXPECT_FALSE(ticks_before.is_null());
89
90  // Then get a 32 bit timer that can be null when it wraps.
91  // Crtical difference from  the TrackedTimerVsTimeTicks test, is that we use
92  // ThreadData::Now().  It can sometimes return the null time.
93  TrackedTime now = ThreadData::Now();
94
95  // Then get a bracketing time.
96  base::TimeTicks ticks_after = base::TimeTicks::Now();
97  EXPECT_FALSE(ticks_after.is_null());
98
99  // Now make sure that we bracketed our tracked time nicely.
100  Duration before = now - TrackedTime(ticks_before);
101  EXPECT_LE(0, before.InMilliseconds());
102  Duration after = now - TrackedTime(ticks_after);
103  EXPECT_GE(0, after.InMilliseconds());
104}
105
106}  // namespace tracked_objects
107