OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/time/time.h" | 5 #include "base/time/time.h" |
6 | 6 |
7 #include <time.h> | 7 #include <time.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 #include <string> | 10 #include <string> |
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 // Unfortunately, our InMilliseconds() function truncates | 636 // Unfortunately, our InMilliseconds() function truncates |
637 // rather than rounds. We should consider fixing this | 637 // rather than rounds. We should consider fixing this |
638 // so that our averages come out better. | 638 // so that our averages come out better. |
639 EXPECT_GE(delta.InMilliseconds(), 9); | 639 EXPECT_GE(delta.InMilliseconds(), 9); |
640 EXPECT_GE(delta.InMicroseconds(), 9000); | 640 EXPECT_GE(delta.InMicroseconds(), 9000); |
641 EXPECT_EQ(delta.InSeconds(), 0); | 641 EXPECT_EQ(delta.InSeconds(), 0); |
642 } | 642 } |
643 } | 643 } |
644 | 644 |
645 static void HighResClockTest(TimeTicks (*GetTicks)()) { | 645 static void HighResClockTest(TimeTicks (*GetTicks)()) { |
646 // HighResNow doesn't work on some systems. Since the product still works | 646 // IsHighResolution() is false on some systems. Since the product still works |
647 // even if it doesn't work, it makes this entire test questionable. | 647 // even if it's false, it makes this entire test questionable. |
648 if (!TimeTicks::IsHighResolution()) | 648 if (!TimeTicks::IsHighResolution()) |
649 return; | 649 return; |
650 | 650 |
651 // Why do we loop here? | 651 // Why do we loop here? |
652 // We're trying to measure that intervals increment in a VERY small amount | 652 // We're trying to measure that intervals increment in a VERY small amount |
653 // of time -- less than 15ms. Unfortunately, if we happen to have a | 653 // of time -- less than 15ms. Unfortunately, if we happen to have a |
654 // context switch in the middle of our test, the context switch could easily | 654 // context switch in the middle of our test, the context switch could easily |
655 // exceed our limit. So, we iterate on this several times. As long as we're | 655 // exceed our limit. So, we iterate on this several times. As long as we're |
656 // able to detect the fine-granularity timers at least once, then the test | 656 // able to detect the fine-granularity timers at least once, then the test |
657 // has succeeded. | 657 // has succeeded. |
(...skipping 14 matching lines...) Expand all Loading... |
672 | 672 |
673 if (delta.InMicroseconds() <= kTargetGranularityUs) | 673 if (delta.InMicroseconds() <= kTargetGranularityUs) |
674 success = true; | 674 success = true; |
675 } | 675 } |
676 | 676 |
677 // In high resolution mode, we expect to see the clock increment | 677 // In high resolution mode, we expect to see the clock increment |
678 // in intervals less than 15ms. | 678 // in intervals less than 15ms. |
679 EXPECT_TRUE(success); | 679 EXPECT_TRUE(success); |
680 } | 680 } |
681 | 681 |
682 TEST(TimeTicks, HighResNow) { | 682 TEST(TimeTicks, HighRes) { |
683 HighResClockTest(&TimeTicks::HighResNow); | 683 HighResClockTest(&TimeTicks::Now); |
684 } | 684 } |
685 | 685 |
686 // Fails frequently on Android http://crbug.com/352633 with: | 686 // Fails frequently on Android http://crbug.com/352633 with: |
687 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0 | 687 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0 |
688 #if defined(OS_ANDROID) | 688 #if defined(OS_ANDROID) |
689 #define MAYBE_ThreadNow DISABLED_ThreadNow | 689 #define MAYBE_ThreadNow DISABLED_ThreadNow |
690 #else | 690 #else |
691 #define MAYBE_ThreadNow ThreadNow | 691 #define MAYBE_ThreadNow ThreadNow |
692 #endif | 692 #endif |
693 TEST(TimeTicks, MAYBE_ThreadNow) { | 693 TEST(TimeTicks, MAYBE_ThreadNow) { |
(...skipping 10 matching lines...) Expand all Loading... |
704 TimeDelta delta_thread = end_thread - begin_thread; | 704 TimeDelta delta_thread = end_thread - begin_thread; |
705 // Make sure that some thread time have elapsed. | 705 // Make sure that some thread time have elapsed. |
706 EXPECT_GT(delta_thread.InMicroseconds(), 0); | 706 EXPECT_GT(delta_thread.InMicroseconds(), 0); |
707 // But the thread time is at least 9ms less than clock time. | 707 // But the thread time is at least 9ms less than clock time. |
708 TimeDelta difference = delta - delta_thread; | 708 TimeDelta difference = delta - delta_thread; |
709 EXPECT_GE(difference.InMicroseconds(), 9000); | 709 EXPECT_GE(difference.InMicroseconds(), 9000); |
710 } | 710 } |
711 } | 711 } |
712 | 712 |
713 TEST(TimeTicks, NowFromSystemTraceTime) { | 713 TEST(TimeTicks, NowFromSystemTraceTime) { |
714 // Re-use HighResNow test for now since clock properties are identical. | 714 // Re-use HighRes test for now since clock properties are identical. |
715 HighResClockTest(&TimeTicks::NowFromSystemTraceTime); | 715 HighResClockTest(&TimeTicks::NowFromSystemTraceTime); |
716 } | 716 } |
717 | 717 |
718 TEST(TimeDelta, FromAndIn) { | 718 TEST(TimeDelta, FromAndIn) { |
719 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); | 719 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); |
720 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); | 720 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); |
721 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); | 721 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); |
722 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); | 722 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); |
723 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == | 723 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == |
724 TimeDelta::FromMicroseconds(2000)); | 724 TimeDelta::FromMicroseconds(2000)); |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 | 891 |
892 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 892 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
893 std::ostringstream oss; | 893 std::ostringstream oss; |
894 oss << TimeTicks(); | 894 oss << TimeTicks(); |
895 EXPECT_TRUE(oss.good()); | 895 EXPECT_TRUE(oss.good()); |
896 } | 896 } |
897 | 897 |
898 } // namespace | 898 } // namespace |
899 | 899 |
900 } // namespace base | 900 } // namespace base |
OLD | NEW |