OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project 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 "src/base/platform/time.h" | 5 #include "src/base/platform/time.h" |
6 | 6 |
7 #if V8_OS_MACOSX | 7 #if V8_OS_MACOSX |
8 #include <mach/mach_time.h> | 8 #include <mach/mach_time.h> |
9 #endif | 9 #endif |
10 #if V8_OS_POSIX | 10 #if V8_OS_POSIX |
11 #include <sys/time.h> | 11 #include <sys/time.h> |
12 #endif | 12 #endif |
13 | 13 |
14 #if V8_OS_WIN | 14 #if V8_OS_WIN |
15 #include "src/base/win32-headers.h" | 15 #include "src/base/win32-headers.h" |
16 #endif | 16 #endif |
17 | 17 |
| 18 #include <vector> |
| 19 |
18 #include "src/base/platform/elapsed-timer.h" | 20 #include "src/base/platform/elapsed-timer.h" |
19 #include "src/base/platform/platform.h" | 21 #include "src/base/platform/platform.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 | 23 |
22 namespace v8 { | 24 namespace v8 { |
23 namespace base { | 25 namespace base { |
24 | 26 |
25 TEST(TimeDelta, FromAndIn) { | 27 TEST(TimeDelta, FromAndIn) { |
26 EXPECT_EQ(TimeDelta::FromDays(2), TimeDelta::FromHours(48)); | 28 EXPECT_EQ(TimeDelta::FromDays(2), TimeDelta::FromHours(48)); |
27 EXPECT_EQ(TimeDelta::FromHours(3), TimeDelta::FromMinutes(180)); | 29 EXPECT_EQ(TimeDelta::FromHours(3), TimeDelta::FromMinutes(180)); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 EXPECT_GE((normal_ticks - previous_normal_ticks).InMicroseconds(), 0); | 180 EXPECT_GE((normal_ticks - previous_normal_ticks).InMicroseconds(), 0); |
179 EXPECT_GE(highres_ticks, previous_highres_ticks); | 181 EXPECT_GE(highres_ticks, previous_highres_ticks); |
180 EXPECT_GE((highres_ticks - previous_highres_ticks).InMicroseconds(), 0); | 182 EXPECT_GE((highres_ticks - previous_highres_ticks).InMicroseconds(), 0); |
181 previous_normal_ticks = normal_ticks; | 183 previous_normal_ticks = normal_ticks; |
182 previous_highres_ticks = highres_ticks; | 184 previous_highres_ticks = highres_ticks; |
183 } | 185 } |
184 } | 186 } |
185 | 187 |
186 | 188 |
187 // Disable on windows until it is implemented. | 189 // Disable on windows until it is implemented. |
188 #if V8_OS_ANDROID || V8_OS_WIN | 190 #if V8_OS_ANDROID |
189 #define MAYBE_ThreadNow DISABLED_ThreadNow | 191 #define MAYBE_ThreadNow DISABLED_ThreadNow |
190 #else | 192 #else |
191 #define MAYBE_ThreadNow ThreadNow | 193 #define MAYBE_ThreadNow ThreadNow |
192 #endif | 194 #endif |
193 TEST(ThreadTicks, MAYBE_ThreadNow) { | 195 TEST(ThreadTicks, MAYBE_ThreadNow) { |
194 if (ThreadTicks::IsSupported()) { | 196 if (ThreadTicks::IsSupported()) { |
195 TimeTicks begin = TimeTicks::Now(); | 197 TimeTicks begin = TimeTicks::Now(); |
196 ThreadTicks begin_thread = ThreadTicks::Now(); | 198 ThreadTicks begin_thread = ThreadTicks::Now(); |
197 // Make sure that ThreadNow value is non-zero. | 199 // Make sure that ThreadNow value is non-zero. |
198 EXPECT_GT(begin_thread, ThreadTicks()); | 200 EXPECT_GT(begin_thread, ThreadTicks()); |
199 // Sleep for 10 milliseconds to get the thread de-scheduled. | 201 // Sleep for 10 milliseconds to get the thread de-scheduled. |
200 OS::Sleep(base::TimeDelta::FromMilliseconds(10)); | 202 OS::Sleep(base::TimeDelta::FromMilliseconds(10)); |
201 ThreadTicks end_thread = ThreadTicks::Now(); | 203 ThreadTicks end_thread = ThreadTicks::Now(); |
202 TimeTicks end = TimeTicks::Now(); | 204 TimeTicks end = TimeTicks::Now(); |
203 TimeDelta delta = end - begin; | 205 TimeDelta delta = end - begin; |
204 TimeDelta delta_thread = end_thread - begin_thread; | 206 TimeDelta delta_thread = end_thread - begin_thread; |
205 // Make sure that some thread time have elapsed. | 207 // Make sure that some thread time have elapsed. |
206 EXPECT_GT(delta_thread.InMicroseconds(), 0); | 208 EXPECT_GT(delta_thread.InMicroseconds(), 0); |
207 // But the thread time is at least 9ms less than clock time. | 209 // But the thread time is at least 9ms less than clock time. |
208 TimeDelta difference = delta - delta_thread; | 210 TimeDelta difference = delta - delta_thread; |
209 EXPECT_GE(difference.InMicroseconds(), 9000); | 211 EXPECT_GE(difference.InMicroseconds(), 9000); |
210 } | 212 } |
211 } | 213 } |
212 | 214 |
| 215 |
| 216 #if V8_OS_WIN |
| 217 TEST(TimeTicks, TimerPerformance) { |
| 218 // Verify that various timer mechanisms can always complete quickly. |
| 219 // Note: This is a somewhat arbitrary test. |
| 220 const int kLoops = 10000; |
| 221 |
| 222 typedef TimeTicks (*TestFunc)(); |
| 223 struct TestCase { |
| 224 TestFunc func; |
| 225 const char *description; |
| 226 }; |
| 227 // Cheating a bit here: assumes sizeof(TimeTicks) == sizeof(Time) |
| 228 // in order to create a single test case list. |
| 229 static_assert(sizeof(TimeTicks) == sizeof(Time), |
| 230 "TimeTicks and Time must be the same size"); |
| 231 std::vector<TestCase> cases; |
| 232 cases.push_back({reinterpret_cast<TestFunc>(&Time::Now), "Time::Now"}); |
| 233 cases.push_back({&TimeTicks::Now, "TimeTicks::Now"}); |
| 234 |
| 235 if (ThreadTicks::IsSupported()) { |
| 236 ThreadTicks::WaitUntilInitialized(); |
| 237 cases.push_back( |
| 238 {reinterpret_cast<TestFunc>(&ThreadTicks::Now), "ThreadTicks::Now"}); |
| 239 } |
| 240 |
| 241 for (const auto& test_case : cases) { |
| 242 TimeTicks start = TimeTicks::Now(); |
| 243 for (int index = 0; index < kLoops; index++) |
| 244 test_case.func(); |
| 245 TimeTicks stop = TimeTicks::Now(); |
| 246 // Turning off the check for acceptible delays. Without this check, |
| 247 // the test really doesn't do much other than measure. But the |
| 248 // measurements are still useful for testing timers on various platforms. |
| 249 // The reason to remove the check is because the tests run on many |
| 250 // buildbots, some of which are VMs. These machines can run horribly |
| 251 // slow, and there is really no value for checking against a max timer. |
| 252 // const int kMaxTime = 35; // Maximum acceptible milliseconds for test. |
| 253 // EXPECT_LT((stop - start).InMilliseconds(), kMaxTime); |
| 254 printf("%s: %1.2fus per call\n", test_case.description, |
| 255 (stop - start).InMillisecondsF() * 1000 / kLoops); |
| 256 } |
| 257 } |
| 258 #endif // V8_OS_WIN |
| 259 |
213 } // namespace base | 260 } // namespace base |
214 } // namespace v8 | 261 } // namespace v8 |
OLD | NEW |