blob: fee4ba6875b4235ff01f0bd6b6371adf95093b4e [file] [log] [blame]
jar@chromium.org3d6a82f2011-11-05 09:39:27 +09001// Copyright (c) 2011 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.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#if defined(USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS)
32 // Expect wrapping at 32 bits.
33 EXPECT_EQ(kSomeMilliseconds, (wrapped_big - TrackedTime()).InMilliseconds());
34#else // !USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS)
35 // Expect no wrapping at 32 bits.
36 EXPECT_EQ(kReallyBigMilliseconds,
37 (wrapped_big - TrackedTime()).InMilliseconds());
38#endif // USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS)
39}
40
41TEST(TrackedTimeTest, TrackedTimerDuration) {
42 int kFirstMilliseconds = 793;
43 int kSecondMilliseconds = 14889;
44
45 Duration first = Duration::FromMilliseconds(kFirstMilliseconds);
46 Duration second = Duration::FromMilliseconds(kSecondMilliseconds);
47
48 EXPECT_EQ(kFirstMilliseconds, first.InMilliseconds());
49 EXPECT_EQ(kSecondMilliseconds, second.InMilliseconds());
50
51 Duration sum = first + second;
52 EXPECT_EQ(kFirstMilliseconds + kSecondMilliseconds, sum.InMilliseconds());
53}
54
55TEST(TrackedTimeTest, TrackedTimerVsTimeTicks) {
56 // Make sure that our 32 bit timer is aligned with the TimeTicks() timer.
57
58 // First get a 64 bit timer (which should not be null).
59 base::TimeTicks ticks_before = base::TimeTicks::Now();
60 EXPECT_FALSE(ticks_before.is_null());
61
62 // Then get a 32 bit timer that can be be null when it wraps.
63 TrackedTime now = TrackedTime::Now();
64
65 // Then get a bracketing time.
66 base::TimeTicks ticks_after = base::TimeTicks::Now();
67 EXPECT_FALSE(ticks_after.is_null());
68
69 // Now make sure that we bracketed our tracked time nicely.
70 Duration before = now - TrackedTime(ticks_before);
71 EXPECT_LE(0, before.InMilliseconds());
72 Duration after = now - TrackedTime(ticks_after);
73 EXPECT_GE(0, after.InMilliseconds());
74}
75
76TEST(TrackedTimeTest, TrackedTimerDisabled) {
77 // Check to be sure disabling the collection of data induces a null time
78 // (which we know will return much faster).
79 if (!ThreadData::InitializeAndSetTrackingStatus(false))
80 return;
81 // Since we disabled tracking, we should get a null response.
82 TrackedTime track_now = ThreadData::Now();
83 EXPECT_TRUE(track_now.is_null());
84}
85
86TEST(TrackedTimeTest, TrackedTimerEnabled) {
87 if (!ThreadData::InitializeAndSetTrackingStatus(true))
88 return;
89 // Make sure that when we enable tracking, we get a real timer result.
90
91 // First get a 64 bit timer (which should not be null).
92 base::TimeTicks ticks_before = base::TimeTicks::Now();
93 EXPECT_FALSE(ticks_before.is_null());
94
95 // Then get a 32 bit timer that can be null when it wraps.
96 // Crtical difference from the TrackedTimerVsTimeTicks test, is that we use
97 // ThreadData::Now(). It can sometimes return the null time.
98 TrackedTime now = ThreadData::Now();
99
100 // Then get a bracketing time.
101 base::TimeTicks ticks_after = base::TimeTicks::Now();
102 EXPECT_FALSE(ticks_after.is_null());
103
104 // Now make sure that we bracketed our tracked time nicely.
105 Duration before = now - TrackedTime(ticks_before);
106 EXPECT_LE(0, before.InMilliseconds());
107 Duration after = now - TrackedTime(ticks_after);
108 EXPECT_GE(0, after.InMilliseconds());
109}
110
111} // namespace tracked_objects