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.
5 // Test of classes in tracked_time.cc
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"
12 namespace tracked_objects
{
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
;
20 TrackedTime some
= TrackedTime() +
21 Duration::FromMilliseconds(kSomeMilliseconds
);
22 EXPECT_EQ(kSomeMilliseconds
, (some
- TrackedTime()).InMilliseconds());
23 EXPECT_FALSE(some
.is_null());
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());
30 TrackedTime
wrapped_big(big
);
31 // Expect wrapping at 32 bits.
32 EXPECT_EQ(kSomeMilliseconds
, (wrapped_big
- TrackedTime()).InMilliseconds());
35 TEST(TrackedTimeTest
, TrackedTimerDuration
) {
36 int kFirstMilliseconds
= 793;
37 int kSecondMilliseconds
= 14889;
39 Duration first
= Duration::FromMilliseconds(kFirstMilliseconds
);
40 Duration second
= Duration::FromMilliseconds(kSecondMilliseconds
);
42 EXPECT_EQ(kFirstMilliseconds
, first
.InMilliseconds());
43 EXPECT_EQ(kSecondMilliseconds
, second
.InMilliseconds());
45 Duration sum
= first
+ second
;
46 EXPECT_EQ(kFirstMilliseconds
+ kSecondMilliseconds
, sum
.InMilliseconds());
49 TEST(TrackedTimeTest
, TrackedTimerVsTimeTicks
) {
50 // Make sure that our 32 bit timer is aligned with the TimeTicks() timer.
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());
56 // Then get a 32 bit timer that can be be null when it wraps.
57 TrackedTime now
= TrackedTime::Now();
59 // Then get a bracketing time.
60 base::TimeTicks ticks_after
= base::TimeTicks::Now();
61 EXPECT_FALSE(ticks_after
.is_null());
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());
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
))
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());
84 TEST(TrackedTimeTest
, TrackedTimerEnabled
) {
85 if (!ThreadData::InitializeAndSetTrackingStatus(
86 ThreadData::PROFILING_CHILDREN_ACTIVE
))
88 // Make sure that when we enable tracking, we get a real timer result.
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());
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();
99 // Then get a bracketing time.
100 base::TimeTicks ticks_after
= base::TimeTicks::Now();
101 EXPECT_FALSE(ticks_after
.is_null());
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());
110 } // namespace tracked_objects