| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #if defined(OS_WIN) | 5 #if defined(OS_WIN) |
| 6 #include <process.h> | 6 #include <process.h> |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #endif | 8 #endif |
| 9 | 9 |
| 10 #include "base/platform_thread.h" | 10 #include "base/platform_thread.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 // Counter1313 will continually be set to 1313. | 68 // Counter1313 will continually be set to 1313. |
| 69 const std::string kCounter1313 = "Counter1313"; | 69 const std::string kCounter1313 = "Counter1313"; |
| 70 // CounterIncrement will be incremented each time. | 70 // CounterIncrement will be incremented each time. |
| 71 const std::string kCounterIncrement = "CounterIncrement"; | 71 const std::string kCounterIncrement = "CounterIncrement"; |
| 72 // CounterDecrement will be decremented each time. | 72 // CounterDecrement will be decremented each time. |
| 73 const std::string kCounterDecrement = "CounterDecrement"; | 73 const std::string kCounterDecrement = "CounterDecrement"; |
| 74 // CounterMixed will be incremented by odd numbered threads and | 74 // CounterMixed will be incremented by odd numbered threads and |
| 75 // decremented by even threads. | 75 // decremented by even threads. |
| 76 const std::string kCounterMixed = "CounterMixed"; | 76 const std::string kCounterMixed = "CounterMixed"; |
| 77 // The number of thread loops that we will do. | 77 // The number of thread loops that we will do. |
| 78 const int kThreadLoops = 1000; | 78 const int kThreadLoops = 100; |
| 79 | 79 |
| 80 class StatsTableThread : public base::SimpleThread { | 80 class StatsTableThread : public base::SimpleThread { |
| 81 public: | 81 public: |
| 82 StatsTableThread(std::string name, int id) | 82 StatsTableThread(std::string name, int id) |
| 83 : base::SimpleThread(name), id_(id) { } | 83 : base::SimpleThread(name), id_(id) { } |
| 84 virtual void Run(); | 84 virtual void Run(); |
| 85 private: | 85 private: |
| 86 int id_; | 86 int id_; |
| 87 }; | 87 }; |
| 88 | 88 |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 312 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 313 StatsTable::set_current(&table); | 313 StatsTable::set_current(&table); |
| 314 | 314 |
| 315 MockStatsCounterTimer bar("bar"); | 315 MockStatsCounterTimer bar("bar"); |
| 316 | 316 |
| 317 // Test initial state. | 317 // Test initial state. |
| 318 EXPECT_FALSE(bar.Running()); | 318 EXPECT_FALSE(bar.Running()); |
| 319 EXPECT_TRUE(bar.start_time().is_null()); | 319 EXPECT_TRUE(bar.start_time().is_null()); |
| 320 EXPECT_TRUE(bar.stop_time().is_null()); | 320 EXPECT_TRUE(bar.stop_time().is_null()); |
| 321 | 321 |
| 322 const int kRunMs = 100; |
| 323 |
| 322 // Do some timing. | 324 // Do some timing. |
| 323 bar.Start(); | 325 bar.Start(); |
| 324 PlatformThread::Sleep(500); | 326 PlatformThread::Sleep(kRunMs); |
| 325 bar.Stop(); | 327 bar.Stop(); |
| 326 EXPECT_LE(500, table.GetCounterValue("t:bar")); | 328 EXPECT_GT(table.GetCounterValue("t:bar"), 0); |
| 329 EXPECT_LE(kRunMs, table.GetCounterValue("t:bar")); |
| 327 | 330 |
| 328 // Verify that timing again is additive. | 331 // Verify that timing again is additive. |
| 329 bar.Start(); | 332 bar.Start(); |
| 330 PlatformThread::Sleep(500); | 333 PlatformThread::Sleep(kRunMs); |
| 331 bar.Stop(); | 334 bar.Stop(); |
| 332 EXPECT_LE(1000, table.GetCounterValue("t:bar")); | 335 EXPECT_GT(table.GetCounterValue("t:bar"), 0); |
| 336 EXPECT_LE(kRunMs * 2, table.GetCounterValue("t:bar")); |
| 333 } | 337 } |
| 334 | 338 |
| 335 // Test some basic StatsRate operations | 339 // Test some basic StatsRate operations |
| 336 TEST_F(StatsTableTest, StatsRate) { | 340 TEST_F(StatsTableTest, StatsRate) { |
| 337 // Create a stats table. | 341 // Create a stats table. |
| 338 const std::string kTableName = "StatTable"; | 342 const std::string kTableName = "StatTable"; |
| 339 const int kMaxThreads = 20; | 343 const int kMaxThreads = 20; |
| 340 const int kMaxCounter = 5; | 344 const int kMaxCounter = 5; |
| 341 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 345 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 342 StatsTable::set_current(&table); | 346 StatsTable::set_current(&table); |
| 343 | 347 |
| 344 StatsRate baz("baz"); | 348 StatsRate baz("baz"); |
| 345 | 349 |
| 346 // Test initial state. | 350 // Test initial state. |
| 347 EXPECT_FALSE(baz.Running()); | 351 EXPECT_FALSE(baz.Running()); |
| 348 EXPECT_EQ(0, table.GetCounterValue("c:baz")); | 352 EXPECT_EQ(0, table.GetCounterValue("c:baz")); |
| 349 EXPECT_EQ(0, table.GetCounterValue("t:baz")); | 353 EXPECT_EQ(0, table.GetCounterValue("t:baz")); |
| 350 | 354 |
| 355 const int kRunMs = 100; |
| 356 |
| 351 // Do some timing. | 357 // Do some timing. |
| 352 baz.Start(); | 358 baz.Start(); |
| 353 PlatformThread::Sleep(500); | 359 PlatformThread::Sleep(kRunMs); |
| 354 baz.Stop(); | 360 baz.Stop(); |
| 355 EXPECT_EQ(1, table.GetCounterValue("c:baz")); | 361 EXPECT_EQ(1, table.GetCounterValue("c:baz")); |
| 356 EXPECT_LE(500, table.GetCounterValue("t:baz")); | 362 EXPECT_LE(kRunMs, table.GetCounterValue("t:baz")); |
| 357 | 363 |
| 358 // Verify that timing again is additive. | 364 // Verify that timing again is additive. |
| 359 baz.Start(); | 365 baz.Start(); |
| 360 PlatformThread::Sleep(500); | 366 PlatformThread::Sleep(kRunMs); |
| 361 baz.Stop(); | 367 baz.Stop(); |
| 362 EXPECT_EQ(2, table.GetCounterValue("c:baz")); | 368 EXPECT_EQ(2, table.GetCounterValue("c:baz")); |
| 363 EXPECT_LE(1000, table.GetCounterValue("t:baz")); | 369 EXPECT_LE(kRunMs * 2, table.GetCounterValue("t:baz")); |
| 364 } | 370 } |
| 365 | 371 |
| 366 // Test some basic StatsScope operations | 372 // Test some basic StatsScope operations |
| 367 TEST_F(StatsTableTest, StatsScope) { | 373 TEST_F(StatsTableTest, StatsScope) { |
| 368 // Create a stats table. | 374 // Create a stats table. |
| 369 const std::string kTableName = "StatTable"; | 375 const std::string kTableName = "StatTable"; |
| 370 const int kMaxThreads = 20; | 376 const int kMaxThreads = 20; |
| 371 const int kMaxCounter = 5; | 377 const int kMaxCounter = 5; |
| 372 DeleteShmem(kTableName); | 378 DeleteShmem(kTableName); |
| 373 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 379 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 374 StatsTable::set_current(&table); | 380 StatsTable::set_current(&table); |
| 375 | 381 |
| 376 StatsCounterTimer foo("foo"); | 382 StatsCounterTimer foo("foo"); |
| 377 StatsRate bar("bar"); | 383 StatsRate bar("bar"); |
| 378 | 384 |
| 379 // Test initial state. | 385 // Test initial state. |
| 380 EXPECT_EQ(0, table.GetCounterValue("t:foo")); | 386 EXPECT_EQ(0, table.GetCounterValue("t:foo")); |
| 381 EXPECT_EQ(0, table.GetCounterValue("t:bar")); | 387 EXPECT_EQ(0, table.GetCounterValue("t:bar")); |
| 382 EXPECT_EQ(0, table.GetCounterValue("c:bar")); | 388 EXPECT_EQ(0, table.GetCounterValue("c:bar")); |
| 383 | 389 |
| 390 const int kRunMs = 100; |
| 391 |
| 384 // Try a scope. | 392 // Try a scope. |
| 385 { | 393 { |
| 386 StatsScope<StatsCounterTimer> timer(foo); | 394 StatsScope<StatsCounterTimer> timer(foo); |
| 387 StatsScope<StatsRate> timer2(bar); | 395 StatsScope<StatsRate> timer2(bar); |
| 388 PlatformThread::Sleep(500); | 396 PlatformThread::Sleep(kRunMs); |
| 389 } | 397 } |
| 390 EXPECT_LE(500, table.GetCounterValue("t:foo")); | 398 EXPECT_LE(kRunMs, table.GetCounterValue("t:foo")); |
| 391 EXPECT_LE(500, table.GetCounterValue("t:bar")); | 399 EXPECT_LE(kRunMs, table.GetCounterValue("t:bar")); |
| 392 EXPECT_EQ(1, table.GetCounterValue("c:bar")); | 400 EXPECT_EQ(1, table.GetCounterValue("c:bar")); |
| 393 | 401 |
| 394 // Try a second scope. | 402 // Try a second scope. |
| 395 { | 403 { |
| 396 StatsScope<StatsCounterTimer> timer(foo); | 404 StatsScope<StatsCounterTimer> timer(foo); |
| 397 StatsScope<StatsRate> timer2(bar); | 405 StatsScope<StatsRate> timer2(bar); |
| 398 PlatformThread::Sleep(500); | 406 PlatformThread::Sleep(kRunMs); |
| 399 } | 407 } |
| 400 EXPECT_LE(1000, table.GetCounterValue("t:foo")); | 408 EXPECT_LE(kRunMs * 2, table.GetCounterValue("t:foo")); |
| 401 EXPECT_LE(1000, table.GetCounterValue("t:bar")); | 409 EXPECT_LE(kRunMs * 2, table.GetCounterValue("t:bar")); |
| 402 EXPECT_EQ(2, table.GetCounterValue("c:bar")); | 410 EXPECT_EQ(2, table.GetCounterValue("c:bar")); |
| 403 | 411 |
| 404 DeleteShmem(kTableName); | 412 DeleteShmem(kTableName); |
| 405 } | 413 } |
| 406 | 414 |
| 407 } // namespace base | 415 } // namespace base |
| OLD | NEW |