OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/inter_process_time_ticks_converter.h" | 5 #include "content/common/inter_process_time_ticks_converter.h" |
6 | 6 |
7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 using base::TimeTicks; | 10 using base::TimeTicks; |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 struct TestParams { | 16 struct TestParams { |
17 int64 local_lower_bound; | 17 int64 local_lower_bound; |
18 int64 remote_lower_bound; | 18 int64 remote_lower_bound; |
19 int64 remote_upper_bound; | 19 int64 remote_upper_bound; |
20 int64 local_upper_bound; | 20 int64 local_upper_bound; |
21 int64 test_time; | 21 int64 test_time; |
22 int64 test_delta; | 22 int64 test_delta; |
23 }; | 23 }; |
24 | 24 |
25 struct TestResults { | 25 struct TestResults { |
26 int64 result_time; | 26 int64 result_time; |
27 int32 result_delta; | 27 int32 result_delta; |
| 28 bool is_skew_additive; |
| 29 int64 skew; |
28 }; | 30 }; |
29 | 31 |
30 TestResults RunTest(const TestParams& params) { | 32 TestResults RunTest(const TestParams& params) { |
31 TimeTicks local_lower_bound = TimeTicks::FromInternalValue( | 33 TimeTicks local_lower_bound = TimeTicks::FromInternalValue( |
32 params.local_lower_bound); | 34 params.local_lower_bound); |
33 TimeTicks local_upper_bound = TimeTicks::FromInternalValue( | 35 TimeTicks local_upper_bound = TimeTicks::FromInternalValue( |
34 params.local_upper_bound); | 36 params.local_upper_bound); |
35 TimeTicks remote_lower_bound = TimeTicks::FromInternalValue( | 37 TimeTicks remote_lower_bound = TimeTicks::FromInternalValue( |
36 params.remote_lower_bound); | 38 params.remote_lower_bound); |
37 TimeTicks remote_upper_bound = TimeTicks::FromInternalValue( | 39 TimeTicks remote_upper_bound = TimeTicks::FromInternalValue( |
38 params.remote_upper_bound); | 40 params.remote_upper_bound); |
39 TimeTicks test_time = TimeTicks::FromInternalValue(params.test_time); | 41 TimeTicks test_time = TimeTicks::FromInternalValue(params.test_time); |
40 | 42 |
41 InterProcessTimeTicksConverter converter( | 43 InterProcessTimeTicksConverter converter( |
42 LocalTimeTicks::FromTimeTicks(local_lower_bound), | 44 LocalTimeTicks::FromTimeTicks(local_lower_bound), |
43 LocalTimeTicks::FromTimeTicks(local_upper_bound), | 45 LocalTimeTicks::FromTimeTicks(local_upper_bound), |
44 RemoteTimeTicks::FromTimeTicks(remote_lower_bound), | 46 RemoteTimeTicks::FromTimeTicks(remote_lower_bound), |
45 RemoteTimeTicks::FromTimeTicks(remote_upper_bound)); | 47 RemoteTimeTicks::FromTimeTicks(remote_upper_bound)); |
46 | 48 |
47 TestResults results; | 49 TestResults results; |
48 results.result_time = converter.ToLocalTimeTicks( | 50 results.result_time = converter.ToLocalTimeTicks( |
49 RemoteTimeTicks::FromTimeTicks( | 51 RemoteTimeTicks::FromTimeTicks( |
50 test_time)).ToTimeTicks().ToInternalValue(); | 52 test_time)).ToTimeTicks().ToInternalValue(); |
51 results.result_delta = converter.ToLocalTimeDelta( | 53 results.result_delta = converter.ToLocalTimeDelta( |
52 RemoteTimeDelta::FromRawDelta(params.test_delta)).ToInt32(); | 54 RemoteTimeDelta::FromRawDelta(params.test_delta)).ToInt32(); |
| 55 results.is_skew_additive = converter.IsSkewAdditiveForMetrics(); |
| 56 results.skew = converter.GetSkewForMetrics().ToInternalValue(); |
53 return results; | 57 return results; |
54 } | 58 } |
55 | 59 |
56 TEST(InterProcessTimeTicksConverterTest, NullTime) { | 60 TEST(InterProcessTimeTicksConverterTest, NullTime) { |
57 // Null / zero times should remain null. | 61 // Null / zero times should remain null. |
58 TestParams p; | 62 TestParams p; |
59 p.local_lower_bound = 1; | 63 p.local_lower_bound = 1; |
60 p.remote_lower_bound = 2; | 64 p.remote_lower_bound = 2; |
61 p.remote_upper_bound = 5; | 65 p.remote_upper_bound = 5; |
62 p.local_upper_bound = 6; | 66 p.local_upper_bound = 6; |
63 p.test_time = 0; | 67 p.test_time = 0; |
64 p.test_delta = 0; | 68 p.test_delta = 0; |
65 TestResults results = RunTest(p); | 69 TestResults results = RunTest(p); |
66 EXPECT_EQ(0, results.result_time); | 70 EXPECT_EQ(0, results.result_time); |
67 EXPECT_EQ(0, results.result_delta); | 71 EXPECT_EQ(0, results.result_delta); |
68 } | 72 } |
69 | 73 |
70 TEST(InterProcessTimeTicksConverterTest, NoSkew) { | 74 TEST(InterProcessTimeTicksConverterTest, NoSkew) { |
71 // All times are monotonic and centered, so no adjustment should occur. | 75 // All times are monotonic and centered, so no adjustment should occur. |
72 TestParams p; | 76 TestParams p; |
73 p.local_lower_bound = 1; | 77 p.local_lower_bound = 1; |
74 p.remote_lower_bound = 2; | 78 p.remote_lower_bound = 2; |
75 p.remote_upper_bound = 5; | 79 p.remote_upper_bound = 5; |
76 p.local_upper_bound = 6; | 80 p.local_upper_bound = 6; |
77 p.test_time = 3; | 81 p.test_time = 3; |
78 p.test_delta = 1; | 82 p.test_delta = 1; |
79 TestResults results = RunTest(p); | 83 TestResults results = RunTest(p); |
80 EXPECT_EQ(3, results.result_time); | 84 EXPECT_EQ(3, results.result_time); |
81 EXPECT_EQ(1, results.result_delta); | 85 EXPECT_EQ(1, results.result_delta); |
| 86 EXPECT_TRUE(results.is_skew_additive); |
| 87 EXPECT_EQ(0, results.skew); |
82 } | 88 } |
83 | 89 |
84 TEST(InterProcessTimeTicksConverterTest, OffsetMidpoints) { | 90 TEST(InterProcessTimeTicksConverterTest, OffsetMidpoints) { |
85 // All times are monotonic, but not centered. Adjust the |remote_*| times so | 91 // All times are monotonic, but not centered. Adjust the |remote_*| times so |
86 // they are centered within the |local_*| times. | 92 // they are centered within the |local_*| times. |
87 TestParams p; | 93 TestParams p; |
88 p.local_lower_bound = 1; | 94 p.local_lower_bound = 1; |
89 p.remote_lower_bound = 3; | 95 p.remote_lower_bound = 3; |
90 p.remote_upper_bound = 6; | 96 p.remote_upper_bound = 6; |
91 p.local_upper_bound = 6; | 97 p.local_upper_bound = 6; |
92 p.test_time = 4; | 98 p.test_time = 4; |
93 p.test_delta = 1; | 99 p.test_delta = 1; |
94 TestResults results = RunTest(p); | 100 TestResults results = RunTest(p); |
95 EXPECT_EQ(3, results.result_time); | 101 EXPECT_EQ(3, results.result_time); |
96 EXPECT_EQ(1, results.result_delta); | 102 EXPECT_EQ(1, results.result_delta); |
| 103 EXPECT_TRUE(results.is_skew_additive); |
| 104 EXPECT_EQ(1, results.skew); |
97 } | 105 } |
98 | 106 |
99 TEST(InterProcessTimeTicksConverterTest, DoubleEndedSkew) { | 107 TEST(InterProcessTimeTicksConverterTest, DoubleEndedSkew) { |
100 // |remote_lower_bound| occurs before |local_lower_bound| and | 108 // |remote_lower_bound| occurs before |local_lower_bound| and |
101 // |remote_upper_bound| occurs after |local_upper_bound|. We must adjust both | 109 // |remote_upper_bound| occurs after |local_upper_bound|. We must adjust both |
102 // bounds and scale down the delta. |test_time| is on the midpoint, so it | 110 // bounds and scale down the delta. |test_time| is on the midpoint, so it |
103 // doesn't change. The ratio of local time to network time is 1:2, so we scale | 111 // doesn't change. The ratio of local time to network time is 1:2, so we scale |
104 // |test_delta| to half. | 112 // |test_delta| to half. |
105 TestParams p; | 113 TestParams p; |
106 p.local_lower_bound = 3; | 114 p.local_lower_bound = 3; |
107 p.remote_lower_bound = 1; | 115 p.remote_lower_bound = 1; |
108 p.remote_upper_bound = 9; | 116 p.remote_upper_bound = 9; |
109 p.local_upper_bound = 7; | 117 p.local_upper_bound = 7; |
110 p.test_time = 5; | 118 p.test_time = 5; |
111 p.test_delta = 2; | 119 p.test_delta = 2; |
112 TestResults results = RunTest(p); | 120 TestResults results = RunTest(p); |
113 EXPECT_EQ(5, results.result_time); | 121 EXPECT_EQ(5, results.result_time); |
114 EXPECT_EQ(1, results.result_delta); | 122 EXPECT_EQ(1, results.result_delta); |
| 123 EXPECT_FALSE(results.is_skew_additive); |
115 } | 124 } |
116 | 125 |
117 TEST(InterProcessTimeTicksConverterTest, FrontEndSkew) { | 126 TEST(InterProcessTimeTicksConverterTest, FrontEndSkew) { |
118 // |remote_upper_bound| is coherent, but |remote_lower_bound| is not. So we | 127 // |remote_upper_bound| is coherent, but |remote_lower_bound| is not. So we |
119 // adjust the lower bound and move |test_time| out. The scale factor is 2:3, | 128 // adjust the lower bound and move |test_time| out. The scale factor is 2:3, |
120 // but since we use integers, the numbers truncate from 3.33 to 3 and 1.33 | 129 // but since we use integers, the numbers truncate from 3.33 to 3 and 1.33 |
121 // to 1. | 130 // to 1. |
122 TestParams p; | 131 TestParams p; |
123 p.local_lower_bound = 3; | 132 p.local_lower_bound = 3; |
124 p.remote_lower_bound = 1; | 133 p.remote_lower_bound = 1; |
125 p.remote_upper_bound = 7; | 134 p.remote_upper_bound = 7; |
126 p.local_upper_bound = 7; | 135 p.local_upper_bound = 7; |
127 p.test_time = 3; | 136 p.test_time = 3; |
128 p.test_delta = 2; | 137 p.test_delta = 2; |
129 TestResults results = RunTest(p); | 138 TestResults results = RunTest(p); |
130 EXPECT_EQ(4, results.result_time); | 139 EXPECT_EQ(4, results.result_time); |
131 EXPECT_EQ(1, results.result_delta); | 140 EXPECT_EQ(1, results.result_delta); |
| 141 EXPECT_FALSE(results.is_skew_additive); |
132 } | 142 } |
133 | 143 |
134 TEST(InterProcessTimeTicksConverterTest, BackEndSkew) { | 144 TEST(InterProcessTimeTicksConverterTest, BackEndSkew) { |
135 // Like the previous test, but |remote_lower_bound| is coherent and | 145 // Like the previous test, but |remote_lower_bound| is coherent and |
136 // |remote_upper_bound| is skewed. | 146 // |remote_upper_bound| is skewed. |
137 TestParams p; | 147 TestParams p; |
138 p.local_lower_bound = 1; | 148 p.local_lower_bound = 1; |
139 p.remote_lower_bound = 1; | 149 p.remote_lower_bound = 1; |
140 p.remote_upper_bound = 7; | 150 p.remote_upper_bound = 7; |
141 p.local_upper_bound = 5; | 151 p.local_upper_bound = 5; |
142 p.test_time = 3; | 152 p.test_time = 3; |
143 p.test_delta = 2; | 153 p.test_delta = 2; |
144 TestResults results = RunTest(p); | 154 TestResults results = RunTest(p); |
145 EXPECT_EQ(2, results.result_time); | 155 EXPECT_EQ(2, results.result_time); |
146 EXPECT_EQ(1, results.result_delta); | 156 EXPECT_EQ(1, results.result_delta); |
| 157 EXPECT_FALSE(results.is_skew_additive); |
147 } | 158 } |
148 | 159 |
149 TEST(InterProcessTimeTicksConverterTest, Instantaneous) { | 160 TEST(InterProcessTimeTicksConverterTest, Instantaneous) { |
150 // The bounds are all okay, but the |remote_lower_bound| and | 161 // The bounds are all okay, but the |remote_lower_bound| and |
151 // |remote_upper_bound| have the same value. No adjustments should be made and | 162 // |remote_upper_bound| have the same value. No adjustments should be made and |
152 // no divide-by-zero errors should occur. | 163 // no divide-by-zero errors should occur. |
153 TestParams p; | 164 TestParams p; |
154 p.local_lower_bound = 1; | 165 p.local_lower_bound = 1; |
155 p.remote_lower_bound = 2; | 166 p.remote_lower_bound = 2; |
156 p.remote_upper_bound = 2; | 167 p.remote_upper_bound = 2; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 p.test_time = 41; | 243 p.test_time = 41; |
233 p.test_delta = 0; | 244 p.test_delta = 0; |
234 TestResults results = RunTest(p); | 245 TestResults results = RunTest(p); |
235 EXPECT_EQ(20, results.result_time); | 246 EXPECT_EQ(20, results.result_time); |
236 EXPECT_EQ(0, results.result_delta); | 247 EXPECT_EQ(0, results.result_delta); |
237 } | 248 } |
238 | 249 |
239 } // anonymous namespace | 250 } // anonymous namespace |
240 | 251 |
241 } // namespace content | 252 } // namespace content |
OLD | NEW |