| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/multiprocess_test.h" | 5 #include "base/multiprocess_test.h" |
| 6 #include "base/platform_thread.h" | 6 #include "base/platform_thread.h" |
| 7 #include "base/simple_thread.h" | 7 #include "base/simple_thread.h" |
| 8 #include "base/stats_table.h" | 8 #include "base/stats_table.h" |
| 9 #include "base/stats_counters.h" | 9 #include "base/stats_counters.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "testing/multiprocess_func_list.h" | 12 #include "testing/multiprocess_func_list.h" |
| 13 | 13 |
| 14 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
| 15 #include <process.h> | 15 #include <process.h> |
| 16 #include <windows.h> | 16 #include <windows.h> |
| 17 #endif | 17 #endif |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 | 20 |
| 21 class StatsTableTest : public MultiProcessTest { | 21 class StatsTableTest : public MultiProcessTest { |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 // Open a StatsTable and verify that we can write to each of the | 24 // Open a StatsTable and verify that we can write to each of the |
| 25 // locations in the table. | 25 // locations in the table. |
| 26 TEST_F(StatsTableTest, VerifySlots) { | 26 TEST_F(StatsTableTest, VerifySlots) { |
| 27 const std::wstring kTableName = L"VerifySlotsStatTable"; | 27 const std::string kTableName = "VerifySlotsStatTable"; |
| 28 const int kMaxThreads = 1; | 28 const int kMaxThreads = 1; |
| 29 const int kMaxCounter = 5; | 29 const int kMaxCounter = 5; |
| 30 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 30 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 31 | 31 |
| 32 // Register a single thread. | 32 // Register a single thread. |
| 33 std::wstring thread_name = L"mainThread"; | 33 std::string thread_name = "mainThread"; |
| 34 int slot_id = table.RegisterThread(thread_name); | 34 int slot_id = table.RegisterThread(thread_name); |
| 35 EXPECT_TRUE(slot_id); | 35 EXPECT_TRUE(slot_id); |
| 36 | 36 |
| 37 // Fill up the table with counters. | 37 // Fill up the table with counters. |
| 38 std::wstring counter_base_name = L"counter"; | 38 std::string counter_base_name = "counter"; |
| 39 for (int index=0; index < kMaxCounter; index++) { | 39 for (int index=0; index < kMaxCounter; index++) { |
| 40 std::wstring counter_name = counter_base_name; | 40 std::string counter_name = counter_base_name; |
| 41 StringAppendF(&counter_name, L"counter.ctr%d", index); | 41 StringAppendF(&counter_name, "counter.ctr%d", index); |
| 42 int counter_id = table.FindCounter(counter_name); | 42 int counter_id = table.FindCounter(counter_name); |
| 43 EXPECT_GT(counter_id, 0); | 43 EXPECT_GT(counter_id, 0); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Try to allocate an additional thread. Verify it fails. | 46 // Try to allocate an additional thread. Verify it fails. |
| 47 slot_id = table.RegisterThread(L"too many threads"); | 47 slot_id = table.RegisterThread("too many threads"); |
| 48 EXPECT_EQ(slot_id, 0); | 48 EXPECT_EQ(slot_id, 0); |
| 49 | 49 |
| 50 // Try to allocate an additional counter. Verify it fails. | 50 // Try to allocate an additional counter. Verify it fails. |
| 51 int counter_id = table.FindCounter(counter_base_name); | 51 int counter_id = table.FindCounter(counter_base_name); |
| 52 EXPECT_EQ(counter_id, 0); | 52 EXPECT_EQ(counter_id, 0); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // CounterZero will continually be set to 0. | 55 // CounterZero will continually be set to 0. |
| 56 const std::wstring kCounterZero = L"CounterZero"; | 56 const std::string kCounterZero = "CounterZero"; |
| 57 // Counter1313 will continually be set to 1313. | 57 // Counter1313 will continually be set to 1313. |
| 58 const std::wstring kCounter1313 = L"Counter1313"; | 58 const std::string kCounter1313 = "Counter1313"; |
| 59 // CounterIncrement will be incremented each time. | 59 // CounterIncrement will be incremented each time. |
| 60 const std::wstring kCounterIncrement = L"CounterIncrement"; | 60 const std::string kCounterIncrement = "CounterIncrement"; |
| 61 // CounterDecrement will be decremented each time. | 61 // CounterDecrement will be decremented each time. |
| 62 const std::wstring kCounterDecrement = L"CounterDecrement"; | 62 const std::string kCounterDecrement = "CounterDecrement"; |
| 63 // CounterMixed will be incremented by odd numbered threads and | 63 // CounterMixed will be incremented by odd numbered threads and |
| 64 // decremented by even threads. | 64 // decremented by even threads. |
| 65 const std::wstring kCounterMixed = L"CounterMixed"; | 65 const std::string kCounterMixed = "CounterMixed"; |
| 66 // The number of thread loops that we will do. | 66 // The number of thread loops that we will do. |
| 67 const int kThreadLoops = 1000; | 67 const int kThreadLoops = 1000; |
| 68 | 68 |
| 69 class StatsTableThread : public base::SimpleThread { | 69 class StatsTableThread : public base::SimpleThread { |
| 70 public: | 70 public: |
| 71 StatsTableThread(std::string name, int id) | 71 StatsTableThread(std::string name, int id) |
| 72 : base::SimpleThread(name), id_(id) { } | 72 : base::SimpleThread(name), id_(id) { } |
| 73 virtual void Run(); | 73 virtual void Run(); |
| 74 private: | 74 private: |
| 75 int id_; | 75 int id_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 94 mixed_counter.Decrement(); | 94 mixed_counter.Decrement(); |
| 95 else | 95 else |
| 96 mixed_counter.Increment(); | 96 mixed_counter.Increment(); |
| 97 PlatformThread::Sleep(index % 10); // short wait | 97 PlatformThread::Sleep(index % 10); // short wait |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 // Create a few threads and have them poke on their counters. | 101 // Create a few threads and have them poke on their counters. |
| 102 TEST_F(StatsTableTest, MultipleThreads) { | 102 TEST_F(StatsTableTest, MultipleThreads) { |
| 103 // Create a stats table. | 103 // Create a stats table. |
| 104 const std::wstring kTableName = L"MultipleThreadStatTable"; | 104 const std::string kTableName = "MultipleThreadStatTable"; |
| 105 const int kMaxThreads = 20; | 105 const int kMaxThreads = 20; |
| 106 const int kMaxCounter = 5; | 106 const int kMaxCounter = 5; |
| 107 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 107 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 108 StatsTable::set_current(&table); | 108 StatsTable::set_current(&table); |
| 109 | 109 |
| 110 EXPECT_EQ(0, table.CountThreadsRegistered()); | 110 EXPECT_EQ(0, table.CountThreadsRegistered()); |
| 111 | 111 |
| 112 // Spin up a set of threads to go bang on the various counters. | 112 // Spin up a set of threads to go bang on the various counters. |
| 113 // After we join the threads, we'll make sure the counters | 113 // After we join the threads, we'll make sure the counters |
| 114 // contain the values we expected. | 114 // contain the values we expected. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 126 delete threads[index]; | 126 delete threads[index]; |
| 127 } | 127 } |
| 128 | 128 |
| 129 StatsCounter zero_counter(kCounterZero); | 129 StatsCounter zero_counter(kCounterZero); |
| 130 StatsCounter lucky13_counter(kCounter1313); | 130 StatsCounter lucky13_counter(kCounter1313); |
| 131 StatsCounter increment_counter(kCounterIncrement); | 131 StatsCounter increment_counter(kCounterIncrement); |
| 132 StatsCounter decrement_counter(kCounterDecrement); | 132 StatsCounter decrement_counter(kCounterDecrement); |
| 133 StatsCounter mixed_counter(kCounterMixed); | 133 StatsCounter mixed_counter(kCounterMixed); |
| 134 | 134 |
| 135 // Verify the various counters are correct. | 135 // Verify the various counters are correct. |
| 136 std::wstring name; | 136 std::string name; |
| 137 name = L"c:" + kCounterZero; | 137 name = "c:" + kCounterZero; |
| 138 EXPECT_EQ(0, table.GetCounterValue(name)); | 138 EXPECT_EQ(0, table.GetCounterValue(name)); |
| 139 name = L"c:" + kCounter1313; | 139 name = "c:" + kCounter1313; |
| 140 EXPECT_EQ(1313 * kMaxThreads, | 140 EXPECT_EQ(1313 * kMaxThreads, |
| 141 table.GetCounterValue(name)); | 141 table.GetCounterValue(name)); |
| 142 name = L"c:" + kCounterIncrement; | 142 name = "c:" + kCounterIncrement; |
| 143 EXPECT_EQ(kMaxThreads * kThreadLoops, | 143 EXPECT_EQ(kMaxThreads * kThreadLoops, |
| 144 table.GetCounterValue(name)); | 144 table.GetCounterValue(name)); |
| 145 name = L"c:" + kCounterDecrement; | 145 name = "c:" + kCounterDecrement; |
| 146 EXPECT_EQ(-kMaxThreads * kThreadLoops, | 146 EXPECT_EQ(-kMaxThreads * kThreadLoops, |
| 147 table.GetCounterValue(name)); | 147 table.GetCounterValue(name)); |
| 148 name = L"c:" + kCounterMixed; | 148 name = "c:" + kCounterMixed; |
| 149 EXPECT_EQ((kMaxThreads % 2) * kThreadLoops, | 149 EXPECT_EQ((kMaxThreads % 2) * kThreadLoops, |
| 150 table.GetCounterValue(name)); | 150 table.GetCounterValue(name)); |
| 151 EXPECT_EQ(0, table.CountThreadsRegistered()); | 151 EXPECT_EQ(0, table.CountThreadsRegistered()); |
| 152 } | 152 } |
| 153 | 153 |
| 154 const std::wstring kTableName = L"MultipleProcessStatTable"; | 154 const std::string kTableName = "MultipleProcessStatTable"; |
| 155 | 155 |
| 156 MULTIPROCESS_TEST_MAIN(StatsTableMultipleProcessMain) { | 156 MULTIPROCESS_TEST_MAIN(StatsTableMultipleProcessMain) { |
| 157 // Each process will open the shared memory and set counters | 157 // Each process will open the shared memory and set counters |
| 158 // concurrently in a loop. We'll use some pauses to | 158 // concurrently in a loop. We'll use some pauses to |
| 159 // mixup the scheduling. | 159 // mixup the scheduling. |
| 160 | 160 |
| 161 StatsTable table(kTableName, 0, 0); | 161 StatsTable table(kTableName, 0, 0); |
| 162 StatsTable::set_current(&table); | 162 StatsTable::set_current(&table); |
| 163 StatsCounter zero_counter(kCounterZero); | 163 StatsCounter zero_counter(kCounterZero); |
| 164 StatsCounter lucky13_counter(kCounter1313); | 164 StatsCounter lucky13_counter(kCounter1313); |
| 165 StatsCounter increment_counter(kCounterIncrement); | 165 StatsCounter increment_counter(kCounterIncrement); |
| 166 StatsCounter decrement_counter(kCounterDecrement); | 166 StatsCounter decrement_counter(kCounterDecrement); |
| 167 for (int index = 0; index < kThreadLoops; index++) { | 167 for (int index = 0; index < kThreadLoops; index++) { |
| 168 zero_counter.Set(0); | 168 zero_counter.Set(0); |
| 169 lucky13_counter.Set(1313); | 169 lucky13_counter.Set(1313); |
| 170 increment_counter.Increment(); | 170 increment_counter.Increment(); |
| 171 decrement_counter.Decrement(); | 171 decrement_counter.Decrement(); |
| 172 PlatformThread::Sleep(index % 10); // short wait | 172 PlatformThread::Sleep(index % 10); // short wait |
| 173 } | 173 } |
| 174 return 0; | 174 return 0; |
| 175 } | 175 } |
| 176 | 176 |
| 177 // Create a few processes and have them poke on their counters. | 177 // Create a few processes and have them poke on their counters. |
| 178 TEST_F(StatsTableTest, MultipleProcesses) { | 178 TEST_F(StatsTableTest, MultipleProcesses) { |
| 179 // Create a stats table. | 179 // Create a stats table. |
| 180 const std::wstring kTableName = L"MultipleProcessStatTable"; | 180 const std::string kTableName = "MultipleProcessStatTable"; |
| 181 const int kMaxProcs = 20; | 181 const int kMaxProcs = 20; |
| 182 const int kMaxCounter = 5; | 182 const int kMaxCounter = 5; |
| 183 StatsTable table(kTableName, kMaxProcs, kMaxCounter); | 183 StatsTable table(kTableName, kMaxProcs, kMaxCounter); |
| 184 StatsTable::set_current(&table); | 184 StatsTable::set_current(&table); |
| 185 | 185 |
| 186 EXPECT_EQ(0, table.CountThreadsRegistered()); | 186 EXPECT_EQ(0, table.CountThreadsRegistered()); |
| 187 | 187 |
| 188 // Spin up a set of processes to go bang on the various counters. | 188 // Spin up a set of processes to go bang on the various counters. |
| 189 // After we join the processes, we'll make sure the counters | 189 // After we join the processes, we'll make sure the counters |
| 190 // contain the values we expected. | 190 // contain the values we expected. |
| 191 ProcessHandle procs[kMaxProcs]; | 191 ProcessHandle procs[kMaxProcs]; |
| 192 | 192 |
| 193 // Spawn the processes. | 193 // Spawn the processes. |
| 194 for (int16 index = 0; index < kMaxProcs; index++) { | 194 for (int16 index = 0; index < kMaxProcs; index++) { |
| 195 procs[index] = this->SpawnChild(L"StatsTableMultipleProcessMain"); | 195 procs[index] = this->SpawnChild(L"StatsTableMultipleProcessMain"); |
| 196 EXPECT_NE(static_cast<ProcessHandle>(NULL), procs[index]); | 196 EXPECT_NE(static_cast<ProcessHandle>(NULL), procs[index]); |
| 197 } | 197 } |
| 198 | 198 |
| 199 // Wait for the processes to finish. | 199 // Wait for the processes to finish. |
| 200 for (int index = 0; index < kMaxProcs; index++) { | 200 for (int index = 0; index < kMaxProcs; index++) { |
| 201 EXPECT_TRUE(WaitForSingleProcess(procs[index], 60 * 1000)); | 201 EXPECT_TRUE(WaitForSingleProcess(procs[index], 60 * 1000)); |
| 202 } | 202 } |
| 203 | 203 |
| 204 StatsCounter zero_counter(kCounterZero); | 204 StatsCounter zero_counter(kCounterZero); |
| 205 StatsCounter lucky13_counter(kCounter1313); | 205 StatsCounter lucky13_counter(kCounter1313); |
| 206 StatsCounter increment_counter(kCounterIncrement); | 206 StatsCounter increment_counter(kCounterIncrement); |
| 207 StatsCounter decrement_counter(kCounterDecrement); | 207 StatsCounter decrement_counter(kCounterDecrement); |
| 208 | 208 |
| 209 // Verify the various counters are correct. | 209 // Verify the various counters are correct. |
| 210 std::wstring name; | 210 std::string name; |
| 211 name = L"c:" + kCounterZero; | 211 name = "c:" + kCounterZero; |
| 212 EXPECT_EQ(0, table.GetCounterValue(name)); | 212 EXPECT_EQ(0, table.GetCounterValue(name)); |
| 213 name = L"c:" + kCounter1313; | 213 name = "c:" + kCounter1313; |
| 214 EXPECT_EQ(1313 * kMaxProcs, | 214 EXPECT_EQ(1313 * kMaxProcs, |
| 215 table.GetCounterValue(name)); | 215 table.GetCounterValue(name)); |
| 216 name = L"c:" + kCounterIncrement; | 216 name = "c:" + kCounterIncrement; |
| 217 EXPECT_EQ(kMaxProcs * kThreadLoops, | 217 EXPECT_EQ(kMaxProcs * kThreadLoops, |
| 218 table.GetCounterValue(name)); | 218 table.GetCounterValue(name)); |
| 219 name = L"c:" + kCounterDecrement; | 219 name = "c:" + kCounterDecrement; |
| 220 EXPECT_EQ(-kMaxProcs * kThreadLoops, | 220 EXPECT_EQ(-kMaxProcs * kThreadLoops, |
| 221 table.GetCounterValue(name)); | 221 table.GetCounterValue(name)); |
| 222 EXPECT_EQ(0, table.CountThreadsRegistered()); | 222 EXPECT_EQ(0, table.CountThreadsRegistered()); |
| 223 } | 223 } |
| 224 | 224 |
| 225 class MockStatsCounter : public StatsCounter { | 225 class MockStatsCounter : public StatsCounter { |
| 226 public: | 226 public: |
| 227 MockStatsCounter(const std::wstring& name) | 227 MockStatsCounter(const std::string& name) |
| 228 : StatsCounter(name) {} | 228 : StatsCounter(name) {} |
| 229 int* Pointer() { return GetPtr(); } | 229 int* Pointer() { return GetPtr(); } |
| 230 }; | 230 }; |
| 231 | 231 |
| 232 // Test some basic StatsCounter operations | 232 // Test some basic StatsCounter operations |
| 233 TEST_F(StatsTableTest, StatsCounter) { | 233 TEST_F(StatsTableTest, StatsCounter) { |
| 234 // Create a stats table. | 234 // Create a stats table. |
| 235 const std::wstring kTableName = L"StatTable"; | 235 const std::string kTableName = "StatTable"; |
| 236 const int kMaxThreads = 20; | 236 const int kMaxThreads = 20; |
| 237 const int kMaxCounter = 5; | 237 const int kMaxCounter = 5; |
| 238 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 238 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 239 StatsTable::set_current(&table); | 239 StatsTable::set_current(&table); |
| 240 | 240 |
| 241 MockStatsCounter foo(L"foo"); | 241 MockStatsCounter foo("foo"); |
| 242 | 242 |
| 243 // Test initial state. | 243 // Test initial state. |
| 244 EXPECT_TRUE(foo.Enabled()); | 244 EXPECT_TRUE(foo.Enabled()); |
| 245 EXPECT_NE(foo.Pointer(), static_cast<int*>(0)); | 245 EXPECT_NE(foo.Pointer(), static_cast<int*>(0)); |
| 246 EXPECT_EQ(0, table.GetCounterValue(L"c:foo")); | 246 EXPECT_EQ(0, table.GetCounterValue("c:foo")); |
| 247 EXPECT_EQ(0, *(foo.Pointer())); | 247 EXPECT_EQ(0, *(foo.Pointer())); |
| 248 | 248 |
| 249 // Test Increment. | 249 // Test Increment. |
| 250 while(*(foo.Pointer()) < 123) foo.Increment(); | 250 while(*(foo.Pointer()) < 123) foo.Increment(); |
| 251 EXPECT_EQ(123, table.GetCounterValue(L"c:foo")); | 251 EXPECT_EQ(123, table.GetCounterValue("c:foo")); |
| 252 foo.Add(0); | 252 foo.Add(0); |
| 253 EXPECT_EQ(123, table.GetCounterValue(L"c:foo")); | 253 EXPECT_EQ(123, table.GetCounterValue("c:foo")); |
| 254 foo.Add(-1); | 254 foo.Add(-1); |
| 255 EXPECT_EQ(122, table.GetCounterValue(L"c:foo")); | 255 EXPECT_EQ(122, table.GetCounterValue("c:foo")); |
| 256 | 256 |
| 257 // Test Set. | 257 // Test Set. |
| 258 foo.Set(0); | 258 foo.Set(0); |
| 259 EXPECT_EQ(0, table.GetCounterValue(L"c:foo")); | 259 EXPECT_EQ(0, table.GetCounterValue("c:foo")); |
| 260 foo.Set(100); | 260 foo.Set(100); |
| 261 EXPECT_EQ(100, table.GetCounterValue(L"c:foo")); | 261 EXPECT_EQ(100, table.GetCounterValue("c:foo")); |
| 262 foo.Set(-1); | 262 foo.Set(-1); |
| 263 EXPECT_EQ(-1, table.GetCounterValue(L"c:foo")); | 263 EXPECT_EQ(-1, table.GetCounterValue("c:foo")); |
| 264 foo.Set(0); | 264 foo.Set(0); |
| 265 EXPECT_EQ(0, table.GetCounterValue(L"c:foo")); | 265 EXPECT_EQ(0, table.GetCounterValue("c:foo")); |
| 266 | 266 |
| 267 // Test Decrement. | 267 // Test Decrement. |
| 268 foo.Decrement(1); | 268 foo.Decrement(1); |
| 269 EXPECT_EQ(-1, table.GetCounterValue(L"c:foo")); | 269 EXPECT_EQ(-1, table.GetCounterValue("c:foo")); |
| 270 foo.Decrement(0); | 270 foo.Decrement(0); |
| 271 EXPECT_EQ(-1, table.GetCounterValue(L"c:foo")); | 271 EXPECT_EQ(-1, table.GetCounterValue("c:foo")); |
| 272 foo.Decrement(-1); | 272 foo.Decrement(-1); |
| 273 EXPECT_EQ(0, table.GetCounterValue(L"c:foo")); | 273 EXPECT_EQ(0, table.GetCounterValue("c:foo")); |
| 274 } | 274 } |
| 275 | 275 |
| 276 class MockStatsCounterTimer : public StatsCounterTimer { | 276 class MockStatsCounterTimer : public StatsCounterTimer { |
| 277 public: | 277 public: |
| 278 MockStatsCounterTimer(const std::wstring& name) | 278 MockStatsCounterTimer(const std::string& name) |
| 279 : StatsCounterTimer(name) {} | 279 : StatsCounterTimer(name) {} |
| 280 | 280 |
| 281 TimeTicks start_time() { return start_time_; } | 281 TimeTicks start_time() { return start_time_; } |
| 282 TimeTicks stop_time() { return stop_time_; } | 282 TimeTicks stop_time() { return stop_time_; } |
| 283 }; | 283 }; |
| 284 | 284 |
| 285 // Test some basic StatsCounterTimer operations | 285 // Test some basic StatsCounterTimer operations |
| 286 TEST_F(StatsTableTest, StatsCounterTimer) { | 286 TEST_F(StatsTableTest, StatsCounterTimer) { |
| 287 // Create a stats table. | 287 // Create a stats table. |
| 288 const std::wstring kTableName = L"StatTable"; | 288 const std::string kTableName = "StatTable"; |
| 289 const int kMaxThreads = 20; | 289 const int kMaxThreads = 20; |
| 290 const int kMaxCounter = 5; | 290 const int kMaxCounter = 5; |
| 291 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 291 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 292 StatsTable::set_current(&table); | 292 StatsTable::set_current(&table); |
| 293 | 293 |
| 294 MockStatsCounterTimer bar(L"bar"); | 294 MockStatsCounterTimer bar("bar"); |
| 295 | 295 |
| 296 // Test initial state. | 296 // Test initial state. |
| 297 EXPECT_FALSE(bar.Running()); | 297 EXPECT_FALSE(bar.Running()); |
| 298 EXPECT_TRUE(bar.start_time().is_null()); | 298 EXPECT_TRUE(bar.start_time().is_null()); |
| 299 EXPECT_TRUE(bar.stop_time().is_null()); | 299 EXPECT_TRUE(bar.stop_time().is_null()); |
| 300 | 300 |
| 301 // Do some timing. | 301 // Do some timing. |
| 302 bar.Start(); | 302 bar.Start(); |
| 303 PlatformThread::Sleep(500); | 303 PlatformThread::Sleep(500); |
| 304 bar.Stop(); | 304 bar.Stop(); |
| 305 EXPECT_LE(500, table.GetCounterValue(L"t:bar")); | 305 EXPECT_LE(500, table.GetCounterValue("t:bar")); |
| 306 | 306 |
| 307 // Verify that timing again is additive. | 307 // Verify that timing again is additive. |
| 308 bar.Start(); | 308 bar.Start(); |
| 309 PlatformThread::Sleep(500); | 309 PlatformThread::Sleep(500); |
| 310 bar.Stop(); | 310 bar.Stop(); |
| 311 EXPECT_LE(1000, table.GetCounterValue(L"t:bar")); | 311 EXPECT_LE(1000, table.GetCounterValue("t:bar")); |
| 312 } | 312 } |
| 313 | 313 |
| 314 // Test some basic StatsRate operations | 314 // Test some basic StatsRate operations |
| 315 TEST_F(StatsTableTest, StatsRate) { | 315 TEST_F(StatsTableTest, StatsRate) { |
| 316 // Create a stats table. | 316 // Create a stats table. |
| 317 const std::wstring kTableName = L"StatTable"; | 317 const std::string kTableName = "StatTable"; |
| 318 const int kMaxThreads = 20; | 318 const int kMaxThreads = 20; |
| 319 const int kMaxCounter = 5; | 319 const int kMaxCounter = 5; |
| 320 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 320 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 321 StatsTable::set_current(&table); | 321 StatsTable::set_current(&table); |
| 322 | 322 |
| 323 StatsRate baz(L"baz"); | 323 StatsRate baz("baz"); |
| 324 | 324 |
| 325 // Test initial state. | 325 // Test initial state. |
| 326 EXPECT_FALSE(baz.Running()); | 326 EXPECT_FALSE(baz.Running()); |
| 327 EXPECT_EQ(0, table.GetCounterValue(L"c:baz")); | 327 EXPECT_EQ(0, table.GetCounterValue("c:baz")); |
| 328 EXPECT_EQ(0, table.GetCounterValue(L"t:baz")); | 328 EXPECT_EQ(0, table.GetCounterValue("t:baz")); |
| 329 | 329 |
| 330 // Do some timing. | 330 // Do some timing. |
| 331 baz.Start(); | 331 baz.Start(); |
| 332 PlatformThread::Sleep(500); | 332 PlatformThread::Sleep(500); |
| 333 baz.Stop(); | 333 baz.Stop(); |
| 334 EXPECT_EQ(1, table.GetCounterValue(L"c:baz")); | 334 EXPECT_EQ(1, table.GetCounterValue("c:baz")); |
| 335 EXPECT_LE(500, table.GetCounterValue(L"t:baz")); | 335 EXPECT_LE(500, table.GetCounterValue("t:baz")); |
| 336 | 336 |
| 337 // Verify that timing again is additive. | 337 // Verify that timing again is additive. |
| 338 baz.Start(); | 338 baz.Start(); |
| 339 PlatformThread::Sleep(500); | 339 PlatformThread::Sleep(500); |
| 340 baz.Stop(); | 340 baz.Stop(); |
| 341 EXPECT_EQ(2, table.GetCounterValue(L"c:baz")); | 341 EXPECT_EQ(2, table.GetCounterValue("c:baz")); |
| 342 EXPECT_LE(1000, table.GetCounterValue(L"t:baz")); | 342 EXPECT_LE(1000, table.GetCounterValue("t:baz")); |
| 343 } | 343 } |
| 344 | 344 |
| 345 // Test some basic StatsScope operations | 345 // Test some basic StatsScope operations |
| 346 TEST_F(StatsTableTest, StatsScope) { | 346 TEST_F(StatsTableTest, StatsScope) { |
| 347 // Create a stats table. | 347 // Create a stats table. |
| 348 const std::wstring kTableName = L"StatTable"; | 348 const std::string kTableName = "StatTable"; |
| 349 const int kMaxThreads = 20; | 349 const int kMaxThreads = 20; |
| 350 const int kMaxCounter = 5; | 350 const int kMaxCounter = 5; |
| 351 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 351 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 352 StatsTable::set_current(&table); | 352 StatsTable::set_current(&table); |
| 353 | 353 |
| 354 StatsCounterTimer foo(L"foo"); | 354 StatsCounterTimer foo("foo"); |
| 355 StatsRate bar(L"bar"); | 355 StatsRate bar("bar"); |
| 356 | 356 |
| 357 // Test initial state. | 357 // Test initial state. |
| 358 EXPECT_EQ(0, table.GetCounterValue(L"t:foo")); | 358 EXPECT_EQ(0, table.GetCounterValue("t:foo")); |
| 359 EXPECT_EQ(0, table.GetCounterValue(L"t:bar")); | 359 EXPECT_EQ(0, table.GetCounterValue("t:bar")); |
| 360 EXPECT_EQ(0, table.GetCounterValue(L"c:bar")); | 360 EXPECT_EQ(0, table.GetCounterValue("c:bar")); |
| 361 | 361 |
| 362 // Try a scope. | 362 // Try a scope. |
| 363 { | 363 { |
| 364 StatsScope<StatsCounterTimer> timer(foo); | 364 StatsScope<StatsCounterTimer> timer(foo); |
| 365 StatsScope<StatsRate> timer2(bar); | 365 StatsScope<StatsRate> timer2(bar); |
| 366 PlatformThread::Sleep(500); | 366 PlatformThread::Sleep(500); |
| 367 } | 367 } |
| 368 EXPECT_LE(500, table.GetCounterValue(L"t:foo")); | 368 EXPECT_LE(500, table.GetCounterValue("t:foo")); |
| 369 EXPECT_LE(500, table.GetCounterValue(L"t:bar")); | 369 EXPECT_LE(500, table.GetCounterValue("t:bar")); |
| 370 EXPECT_EQ(1, table.GetCounterValue(L"c:bar")); | 370 EXPECT_EQ(1, table.GetCounterValue("c:bar")); |
| 371 | 371 |
| 372 // Try a second scope. | 372 // Try a second scope. |
| 373 { | 373 { |
| 374 StatsScope<StatsCounterTimer> timer(foo); | 374 StatsScope<StatsCounterTimer> timer(foo); |
| 375 StatsScope<StatsRate> timer2(bar); | 375 StatsScope<StatsRate> timer2(bar); |
| 376 PlatformThread::Sleep(500); | 376 PlatformThread::Sleep(500); |
| 377 } | 377 } |
| 378 EXPECT_LE(1000, table.GetCounterValue(L"t:foo")); | 378 EXPECT_LE(1000, table.GetCounterValue("t:foo")); |
| 379 EXPECT_LE(1000, table.GetCounterValue(L"t:bar")); | 379 EXPECT_LE(1000, table.GetCounterValue("t:bar")); |
| 380 EXPECT_EQ(2, table.GetCounterValue(L"c:bar")); | 380 EXPECT_EQ(2, table.GetCounterValue("c:bar")); |
| 381 } | 381 } |
| 382 | 382 |
| 383 } // namespace base | 383 } // namespace base |
| OLD | NEW |