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