| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/metrics/stats_counters.h" | 5 #include "base/metrics/stats_counters.h" |
| 6 #include "base/metrics/stats_table.h" | 6 #include "base/metrics/stats_table.h" |
| 7 #include "base/shared_memory.h" | 7 #include "base/shared_memory.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "base/test/multiprocess_test.h" | 10 #include "base/test/multiprocess_test.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 for (int index = 0; index < kThreadLoops; index++) { | 94 for (int index = 0; index < kThreadLoops; index++) { |
| 95 StatsCounter mixed_counter(kCounterMixed); // create this one in the loop | 95 StatsCounter mixed_counter(kCounterMixed); // create this one in the loop |
| 96 zero_counter.Set(0); | 96 zero_counter.Set(0); |
| 97 lucky13_counter.Set(1313); | 97 lucky13_counter.Set(1313); |
| 98 increment_counter.Increment(); | 98 increment_counter.Increment(); |
| 99 decrement_counter.Decrement(); | 99 decrement_counter.Decrement(); |
| 100 if (id_ % 2) | 100 if (id_ % 2) |
| 101 mixed_counter.Decrement(); | 101 mixed_counter.Decrement(); |
| 102 else | 102 else |
| 103 mixed_counter.Increment(); | 103 mixed_counter.Increment(); |
| 104 PlatformThread::Sleep(index % 10); // short wait | 104 PlatformThread::Sleep(TimeDelta::FromMilliseconds(index % 10)); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Create a few threads and have them poke on their counters. | 108 // Create a few threads and have them poke on their counters. |
| 109 // Flaky, http://crbug.com/10611. | 109 // Flaky, http://crbug.com/10611. |
| 110 TEST_F(StatsTableTest, FLAKY_MultipleThreads) { | 110 TEST_F(StatsTableTest, FLAKY_MultipleThreads) { |
| 111 // Create a stats table. | 111 // Create a stats table. |
| 112 const std::string kTableName = "MultipleThreadStatTable"; | 112 const std::string kTableName = "MultipleThreadStatTable"; |
| 113 const int kMaxThreads = 20; | 113 const int kMaxThreads = 20; |
| 114 const int kMaxCounter = 5; | 114 const int kMaxCounter = 5; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 StatsTable::set_current(&table); | 173 StatsTable::set_current(&table); |
| 174 StatsCounter zero_counter(kCounterZero); | 174 StatsCounter zero_counter(kCounterZero); |
| 175 StatsCounter lucky13_counter(kCounter1313); | 175 StatsCounter lucky13_counter(kCounter1313); |
| 176 StatsCounter increment_counter(kCounterIncrement); | 176 StatsCounter increment_counter(kCounterIncrement); |
| 177 StatsCounter decrement_counter(kCounterDecrement); | 177 StatsCounter decrement_counter(kCounterDecrement); |
| 178 for (int index = 0; index < kThreadLoops; index++) { | 178 for (int index = 0; index < kThreadLoops; index++) { |
| 179 zero_counter.Set(0); | 179 zero_counter.Set(0); |
| 180 lucky13_counter.Set(1313); | 180 lucky13_counter.Set(1313); |
| 181 increment_counter.Increment(); | 181 increment_counter.Increment(); |
| 182 decrement_counter.Decrement(); | 182 decrement_counter.Decrement(); |
| 183 PlatformThread::Sleep(index % 10); // short wait | 183 PlatformThread::Sleep(TimeDelta::FromMilliseconds(index % 10)); |
| 184 } | 184 } |
| 185 return 0; | 185 return 0; |
| 186 } | 186 } |
| 187 | 187 |
| 188 // Create a few processes and have them poke on their counters. | 188 // Create a few processes and have them poke on their counters. |
| 189 // This test is slow and flaky http://crbug.com/10611 | 189 // This test is slow and flaky http://crbug.com/10611 |
| 190 TEST_F(StatsTableTest, FLAKY_MultipleProcesses) { | 190 TEST_F(StatsTableTest, FLAKY_MultipleProcesses) { |
| 191 // Create a stats table. | 191 // Create a stats table. |
| 192 const int kMaxProcs = 20; | 192 const int kMaxProcs = 20; |
| 193 const int kMaxCounter = 5; | 193 const int kMaxCounter = 5; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 308 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 309 StatsTable::set_current(&table); | 309 StatsTable::set_current(&table); |
| 310 | 310 |
| 311 MockStatsCounterTimer bar("bar"); | 311 MockStatsCounterTimer bar("bar"); |
| 312 | 312 |
| 313 // Test initial state. | 313 // Test initial state. |
| 314 EXPECT_FALSE(bar.Running()); | 314 EXPECT_FALSE(bar.Running()); |
| 315 EXPECT_TRUE(bar.start_time().is_null()); | 315 EXPECT_TRUE(bar.start_time().is_null()); |
| 316 EXPECT_TRUE(bar.stop_time().is_null()); | 316 EXPECT_TRUE(bar.stop_time().is_null()); |
| 317 | 317 |
| 318 const int kRunMs = 100; | 318 const TimeDelta kDuration = TimeDelta::FromMilliseconds(100); |
| 319 | 319 |
| 320 // Do some timing. | 320 // Do some timing. |
| 321 bar.Start(); | 321 bar.Start(); |
| 322 PlatformThread::Sleep(kRunMs); | 322 PlatformThread::Sleep(kDuration); |
| 323 bar.Stop(); | 323 bar.Stop(); |
| 324 EXPECT_GT(table.GetCounterValue("t:bar"), 0); | 324 EXPECT_GT(table.GetCounterValue("t:bar"), 0); |
| 325 EXPECT_LE(kRunMs, table.GetCounterValue("t:bar")); | 325 EXPECT_LE(kDuration.InMilliseconds(), table.GetCounterValue("t:bar")); |
| 326 | 326 |
| 327 // Verify that timing again is additive. | 327 // Verify that timing again is additive. |
| 328 bar.Start(); | 328 bar.Start(); |
| 329 PlatformThread::Sleep(kRunMs); | 329 PlatformThread::Sleep(kDuration); |
| 330 bar.Stop(); | 330 bar.Stop(); |
| 331 EXPECT_GT(table.GetCounterValue("t:bar"), 0); | 331 EXPECT_GT(table.GetCounterValue("t:bar"), 0); |
| 332 EXPECT_LE(kRunMs * 2, table.GetCounterValue("t:bar")); | 332 EXPECT_LE(kDuration.InMilliseconds() * 2, table.GetCounterValue("t:bar")); |
| 333 } | 333 } |
| 334 | 334 |
| 335 // Test some basic StatsRate operations | 335 // Test some basic StatsRate operations |
| 336 TEST_F(StatsTableTest, StatsRate) { | 336 TEST_F(StatsTableTest, StatsRate) { |
| 337 // Create a stats table. | 337 // Create a stats table. |
| 338 const std::string kTableName = "StatTable"; | 338 const std::string kTableName = "StatTable"; |
| 339 const int kMaxThreads = 20; | 339 const int kMaxThreads = 20; |
| 340 const int kMaxCounter = 5; | 340 const int kMaxCounter = 5; |
| 341 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 341 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 342 StatsTable::set_current(&table); | 342 StatsTable::set_current(&table); |
| 343 | 343 |
| 344 StatsRate baz("baz"); | 344 StatsRate baz("baz"); |
| 345 | 345 |
| 346 // Test initial state. | 346 // Test initial state. |
| 347 EXPECT_FALSE(baz.Running()); | 347 EXPECT_FALSE(baz.Running()); |
| 348 EXPECT_EQ(0, table.GetCounterValue("c:baz")); | 348 EXPECT_EQ(0, table.GetCounterValue("c:baz")); |
| 349 EXPECT_EQ(0, table.GetCounterValue("t:baz")); | 349 EXPECT_EQ(0, table.GetCounterValue("t:baz")); |
| 350 | 350 |
| 351 const int kRunMs = 100; | 351 const TimeDelta kDuration = TimeDelta::FromMilliseconds(100); |
| 352 | 352 |
| 353 // Do some timing. | 353 // Do some timing. |
| 354 baz.Start(); | 354 baz.Start(); |
| 355 PlatformThread::Sleep(kRunMs); | 355 PlatformThread::Sleep(kDuration); |
| 356 baz.Stop(); | 356 baz.Stop(); |
| 357 EXPECT_EQ(1, table.GetCounterValue("c:baz")); | 357 EXPECT_EQ(1, table.GetCounterValue("c:baz")); |
| 358 EXPECT_LE(kRunMs, table.GetCounterValue("t:baz")); | 358 EXPECT_LE(kDuration.InMilliseconds(), table.GetCounterValue("t:baz")); |
| 359 | 359 |
| 360 // Verify that timing again is additive. | 360 // Verify that timing again is additive. |
| 361 baz.Start(); | 361 baz.Start(); |
| 362 PlatformThread::Sleep(kRunMs); | 362 PlatformThread::Sleep(kDuration); |
| 363 baz.Stop(); | 363 baz.Stop(); |
| 364 EXPECT_EQ(2, table.GetCounterValue("c:baz")); | 364 EXPECT_EQ(2, table.GetCounterValue("c:baz")); |
| 365 EXPECT_LE(kRunMs * 2, table.GetCounterValue("t:baz")); | 365 EXPECT_LE(kDuration.InMilliseconds() * 2, table.GetCounterValue("t:baz")); |
| 366 } | 366 } |
| 367 | 367 |
| 368 // Test some basic StatsScope operations | 368 // Test some basic StatsScope operations |
| 369 TEST_F(StatsTableTest, StatsScope) { | 369 TEST_F(StatsTableTest, StatsScope) { |
| 370 // Create a stats table. | 370 // Create a stats table. |
| 371 const std::string kTableName = "StatTable"; | 371 const std::string kTableName = "StatTable"; |
| 372 const int kMaxThreads = 20; | 372 const int kMaxThreads = 20; |
| 373 const int kMaxCounter = 5; | 373 const int kMaxCounter = 5; |
| 374 DeleteShmem(kTableName); | 374 DeleteShmem(kTableName); |
| 375 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 375 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 376 StatsTable::set_current(&table); | 376 StatsTable::set_current(&table); |
| 377 | 377 |
| 378 StatsCounterTimer foo("foo"); | 378 StatsCounterTimer foo("foo"); |
| 379 StatsRate bar("bar"); | 379 StatsRate bar("bar"); |
| 380 | 380 |
| 381 // Test initial state. | 381 // Test initial state. |
| 382 EXPECT_EQ(0, table.GetCounterValue("t:foo")); | 382 EXPECT_EQ(0, table.GetCounterValue("t:foo")); |
| 383 EXPECT_EQ(0, table.GetCounterValue("t:bar")); | 383 EXPECT_EQ(0, table.GetCounterValue("t:bar")); |
| 384 EXPECT_EQ(0, table.GetCounterValue("c:bar")); | 384 EXPECT_EQ(0, table.GetCounterValue("c:bar")); |
| 385 | 385 |
| 386 const int kRunMs = 100; | 386 const TimeDelta kDuration = TimeDelta::FromMilliseconds(100); |
| 387 | 387 |
| 388 // Try a scope. | 388 // Try a scope. |
| 389 { | 389 { |
| 390 StatsScope<StatsCounterTimer> timer(foo); | 390 StatsScope<StatsCounterTimer> timer(foo); |
| 391 StatsScope<StatsRate> timer2(bar); | 391 StatsScope<StatsRate> timer2(bar); |
| 392 PlatformThread::Sleep(kRunMs); | 392 PlatformThread::Sleep(kDuration); |
| 393 } | 393 } |
| 394 EXPECT_LE(kRunMs, table.GetCounterValue("t:foo")); | 394 EXPECT_LE(kDuration.InMilliseconds(), table.GetCounterValue("t:foo")); |
| 395 EXPECT_LE(kRunMs, table.GetCounterValue("t:bar")); | 395 EXPECT_LE(kDuration.InMilliseconds(), table.GetCounterValue("t:bar")); |
| 396 EXPECT_EQ(1, table.GetCounterValue("c:bar")); | 396 EXPECT_EQ(1, table.GetCounterValue("c:bar")); |
| 397 | 397 |
| 398 // Try a second scope. | 398 // Try a second scope. |
| 399 { | 399 { |
| 400 StatsScope<StatsCounterTimer> timer(foo); | 400 StatsScope<StatsCounterTimer> timer(foo); |
| 401 StatsScope<StatsRate> timer2(bar); | 401 StatsScope<StatsRate> timer2(bar); |
| 402 PlatformThread::Sleep(kRunMs); | 402 PlatformThread::Sleep(kDuration); |
| 403 } | 403 } |
| 404 EXPECT_LE(kRunMs * 2, table.GetCounterValue("t:foo")); | 404 EXPECT_LE(kDuration.InMilliseconds() * 2, table.GetCounterValue("t:foo")); |
| 405 EXPECT_LE(kRunMs * 2, table.GetCounterValue("t:bar")); | 405 EXPECT_LE(kDuration.InMilliseconds() * 2, table.GetCounterValue("t:bar")); |
| 406 EXPECT_EQ(2, table.GetCounterValue("c:bar")); | 406 EXPECT_EQ(2, table.GetCounterValue("c:bar")); |
| 407 | 407 |
| 408 DeleteShmem(kTableName); | 408 DeleteShmem(kTableName); |
| 409 } | 409 } |
| 410 | 410 |
| 411 } // namespace base | 411 } // namespace base |
| OLD | NEW |