Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "timing_logger.h" |
| 18 | |
| 19 | #include "common_test.h" |
| 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | class TimingLoggerTest : public CommonTest {}; |
| 24 | |
| 25 | // TODO: Negative test cases (improper pairing of EndSplit, etc.) |
| 26 | |
| 27 | TEST_F(TimingLoggerTest, StartEnd) { |
| 28 | const char* split1name = "First Split"; |
| 29 | base::TimingLogger timings("StartEnd", true, false); |
| 30 | |
| 31 | timings.StartSplit(split1name); |
| 32 | |
| 33 | timings.EndSplit(); // Ends split1. |
| 34 | |
| 35 | const base::TimingLogger::SplitTimings& splits = timings.GetSplits(); |
| 36 | |
| 37 | EXPECT_EQ(1U, splits.size()); |
| 38 | EXPECT_STREQ(splits[0].second, split1name); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | TEST_F(TimingLoggerTest, StartNewEnd) { |
| 43 | const char* split1name = "First Split"; |
| 44 | const char* split2name = "Second Split"; |
| 45 | const char* split3name = "Third Split"; |
| 46 | base::TimingLogger timings("StartNewEnd", true, false); |
| 47 | |
| 48 | timings.StartSplit(split1name); |
| 49 | |
| 50 | timings.NewSplit(split2name); // Ends split1. |
| 51 | |
| 52 | timings.NewSplit(split3name); // Ends split2. |
| 53 | |
| 54 | timings.EndSplit(); // Ends split3. |
| 55 | |
| 56 | const base::TimingLogger::SplitTimings& splits = timings.GetSplits(); |
| 57 | |
| 58 | EXPECT_EQ(3U, splits.size()); |
| 59 | EXPECT_STREQ(splits[0].second, split1name); |
| 60 | EXPECT_STREQ(splits[1].second, split2name); |
| 61 | EXPECT_STREQ(splits[2].second, split3name); |
| 62 | } |
| 63 | |
| 64 | TEST_F(TimingLoggerTest, StartNewEndNested) { |
| 65 | const char* split1name = "First Split"; |
| 66 | const char* split2name = "Second Split"; |
| 67 | const char* split3name = "Third Split"; |
| 68 | const char* split4name = "Fourth Split"; |
| 69 | const char* split5name = "Fifth Split"; |
| 70 | base::TimingLogger timings("StartNewEndNested", true, false); |
| 71 | |
| 72 | timings.StartSplit(split1name); |
| 73 | |
| 74 | timings.NewSplit(split2name); // Ends split1. |
| 75 | |
| 76 | timings.StartSplit(split3name); |
| 77 | |
| 78 | timings.StartSplit(split4name); |
| 79 | |
| 80 | timings.NewSplit(split5name); // Ends split4. |
| 81 | |
| 82 | timings.EndSplit(); // Ends split5. |
| 83 | |
| 84 | timings.EndSplit(); // Ends split3. |
| 85 | |
| 86 | timings.EndSplit(); // Ends split2. |
| 87 | |
| 88 | const base::TimingLogger::SplitTimings& splits = timings.GetSplits(); |
| 89 | |
| 90 | EXPECT_EQ(5U, splits.size()); |
| 91 | EXPECT_STREQ(splits[0].second, split1name); |
| 92 | EXPECT_STREQ(splits[1].second, split4name); |
| 93 | EXPECT_STREQ(splits[2].second, split5name); |
| 94 | EXPECT_STREQ(splits[3].second, split3name); |
| 95 | EXPECT_STREQ(splits[4].second, split2name); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | TEST_F(TimingLoggerTest, Scoped) { |
| 100 | const char* outersplit = "Outer Split"; |
| 101 | const char* innersplit1 = "Inner Split 1"; |
| 102 | const char* innerinnersplit1 = "Inner Inner Split 1"; |
| 103 | const char* innersplit2 = "Inner Split 2"; |
| 104 | base::TimingLogger timings("Scoped", true, false); |
| 105 | |
| 106 | { |
| 107 | base::TimingLogger::ScopedSplit outer(outersplit, &timings); |
| 108 | |
| 109 | { |
| 110 | base::TimingLogger::ScopedSplit inner1(innersplit1, &timings); |
| 111 | |
| 112 | { |
| 113 | base::TimingLogger::ScopedSplit innerinner1(innerinnersplit1, &timings); |
| 114 | } // Ends innerinnersplit1. |
| 115 | } // Ends innersplit1. |
| 116 | |
| 117 | { |
| 118 | base::TimingLogger::ScopedSplit inner2(innersplit2, &timings); |
| 119 | } // Ends innersplit2. |
| 120 | } // Ends outersplit. |
| 121 | |
| 122 | const base::TimingLogger::SplitTimings& splits = timings.GetSplits(); |
| 123 | |
| 124 | EXPECT_EQ(4U, splits.size()); |
| 125 | EXPECT_STREQ(splits[0].second, innerinnersplit1); |
| 126 | EXPECT_STREQ(splits[1].second, innersplit1); |
| 127 | EXPECT_STREQ(splits[2].second, innersplit2); |
| 128 | EXPECT_STREQ(splits[3].second, outersplit); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | TEST_F(TimingLoggerTest, ScopedAndExplicit) { |
| 133 | const char* outersplit = "Outer Split"; |
| 134 | const char* innersplit = "Inner Split"; |
| 135 | const char* innerinnersplit1 = "Inner Inner Split 1"; |
| 136 | const char* innerinnersplit2 = "Inner Inner Split 2"; |
| 137 | base::TimingLogger timings("Scoped", true, false); |
| 138 | |
| 139 | timings.StartSplit(outersplit); |
| 140 | |
| 141 | { |
| 142 | base::TimingLogger::ScopedSplit inner(innersplit, &timings); |
| 143 | |
| 144 | timings.StartSplit(innerinnersplit1); |
| 145 | |
| 146 | timings.NewSplit(innerinnersplit2); // Ends innerinnersplit1. |
| 147 | } // Ends innerinnersplit2, then innersplit. |
| 148 | |
| 149 | timings.EndSplit(); // Ends outersplit. |
| 150 | |
| 151 | const base::TimingLogger::SplitTimings& splits = timings.GetSplits(); |
| 152 | |
| 153 | EXPECT_EQ(4U, splits.size()); |
| 154 | EXPECT_STREQ(splits[0].second, innerinnersplit1); |
| 155 | EXPECT_STREQ(splits[1].second, innerinnersplit2); |
| 156 | EXPECT_STREQ(splits[2].second, innersplit); |
| 157 | EXPECT_STREQ(splits[3].second, outersplit); |
| 158 | } |
| 159 | |
| 160 | } // namespace art |