blob: ef84f153a62cb55ff31478f5a603fe6f5fc2de4a [file] [log] [blame]
jar@chromium.org23b00722012-02-11 04:43:42 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
jar@chromium.org27cf2972011-11-09 02:09:21 +09002// 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"
avi@chromium.orgb45ec932013-06-29 00:14:18 +09008#include "base/time/time.h"
jar@chromium.org27cf2972011-11-09 02:09:21 +09009#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);
jar@chromium.org27cf2972011-11-09 02:09:21 +090031 // Expect wrapping at 32 bits.
32 EXPECT_EQ(kSomeMilliseconds, (wrapped_big - TrackedTime()).InMilliseconds());
jar@chromium.org27cf2972011-11-09 02:09:21 +090033}
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).
jar@chromium.org23b00722012-02-11 04:43:42 +090073 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED))
jar@chromium.org27cf2972011-11-09 02:09:21 +090074 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());
jar@chromium.orgb5c974b2011-12-14 10:36:48 +090078 track_now = ThreadData::NowForStartOfRun(NULL);
jar@chromium.orgb536eef2011-11-14 14:24:07 +090079 EXPECT_TRUE(track_now.is_null());
80 track_now = ThreadData::NowForEndOfRun();
81 EXPECT_TRUE(track_now.is_null());
jar@chromium.org27cf2972011-11-09 02:09:21 +090082}
83
84TEST(TrackedTimeTest, TrackedTimerEnabled) {
jar@chromium.org23b00722012-02-11 04:43:42 +090085 if (!ThreadData::InitializeAndSetTrackingStatus(
86 ThreadData::PROFILING_CHILDREN_ACTIVE))
jar@chromium.org27cf2972011-11-09 02:09:21 +090087 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