| 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/stats_table.h" | 8 #include "base/stats_table.h" |
| 8 #include "base/stats_counters.h" | 9 #include "base/stats_counters.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/multiprocess_func_list.h" | 12 #include "testing/multiprocess_func_list.h" |
| 12 | 13 |
| 13 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
| 14 #include <process.h> | 15 #include <process.h> |
| 15 #include <windows.h> | 16 #include <windows.h> |
| 16 #endif | 17 #endif |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 // CounterIncrement will be incremented each time. | 60 // CounterIncrement will be incremented each time. |
| 60 const std::wstring kCounterIncrement = L"CounterIncrement"; | 61 const std::wstring kCounterIncrement = L"CounterIncrement"; |
| 61 // CounterDecrement will be decremented each time. | 62 // CounterDecrement will be decremented each time. |
| 62 const std::wstring kCounterDecrement = L"CounterDecrement"; | 63 const std::wstring kCounterDecrement = L"CounterDecrement"; |
| 63 // CounterMixed will be incremented by odd numbered threads and | 64 // CounterMixed will be incremented by odd numbered threads and |
| 64 // decremented by even threads. | 65 // decremented by even threads. |
| 65 const std::wstring kCounterMixed = L"CounterMixed"; | 66 const std::wstring kCounterMixed = L"CounterMixed"; |
| 66 // The number of thread loops that we will do. | 67 // The number of thread loops that we will do. |
| 67 const int kThreadLoops = 1000; | 68 const int kThreadLoops = 1000; |
| 68 | 69 |
| 69 class StatsTableThread : public PlatformThread::Delegate { | 70 class StatsTableThread : public base::SimpleThread { |
| 70 public: | 71 public: |
| 71 void ThreadMain(); | 72 StatsTableThread(std::string name, int id) |
| 72 PlatformThreadHandle thread_; | 73 : base::SimpleThread(name), id_(id) { } |
| 74 virtual void Run(); |
| 75 private: |
| 73 int id_; | 76 int id_; |
| 74 }; | 77 }; |
| 75 | 78 |
| 76 void StatsTableThread::ThreadMain() { | 79 void StatsTableThread::Run() { |
| 77 // Each thread will open the shared memory and set counters | 80 // Each thread will open the shared memory and set counters |
| 78 // concurrently in a loop. We'll use some pauses to | 81 // concurrently in a loop. We'll use some pauses to |
| 79 // mixup the thread scheduling. | 82 // mixup the thread scheduling. |
| 80 | 83 |
| 81 StatsCounter zero_counter(kCounterZero); | 84 StatsCounter zero_counter(kCounterZero); |
| 82 StatsCounter lucky13_counter(kCounter1313); | 85 StatsCounter lucky13_counter(kCounter1313); |
| 83 StatsCounter increment_counter(kCounterIncrement); | 86 StatsCounter increment_counter(kCounterIncrement); |
| 84 StatsCounter decrement_counter(kCounterDecrement); | 87 StatsCounter decrement_counter(kCounterDecrement); |
| 85 for (int index = 0; index < kThreadLoops; index++) { | 88 for (int index = 0; index < kThreadLoops; index++) { |
| 86 StatsCounter mixed_counter(kCounterMixed); // create this one in the loop | 89 StatsCounter mixed_counter(kCounterMixed); // create this one in the loop |
| (...skipping 16 matching lines...) Expand all Loading... |
| 103 const int kMaxThreads = 20; | 106 const int kMaxThreads = 20; |
| 104 const int kMaxCounter = 5; | 107 const int kMaxCounter = 5; |
| 105 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 108 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 106 StatsTable::set_current(&table); | 109 StatsTable::set_current(&table); |
| 107 | 110 |
| 108 EXPECT_EQ(0, table.CountThreadsRegistered()); | 111 EXPECT_EQ(0, table.CountThreadsRegistered()); |
| 109 | 112 |
| 110 // Spin up a set of threads to go bang on the various counters. | 113 // Spin up a set of threads to go bang on the various counters. |
| 111 // After we join the threads, we'll make sure the counters | 114 // After we join the threads, we'll make sure the counters |
| 112 // contain the values we expected. | 115 // contain the values we expected. |
| 113 StatsTableThread threads[kMaxThreads]; | 116 StatsTableThread* threads[kMaxThreads]; |
| 114 | 117 |
| 115 // Spawn the threads. | 118 // Spawn the threads. |
| 116 for (int index = 0; index < kMaxThreads; index++) { | 119 for (int index = 0; index < kMaxThreads; index++) { |
| 117 threads[index].id_ = index; | 120 threads[index] = new StatsTableThread("MultipleThreadsTest", index); |
| 118 bool created = | 121 threads[index]->Start(); |
| 119 PlatformThread::Create(0, &threads[index], &threads[index].thread_); | |
| 120 EXPECT_EQ(true, created); | |
| 121 EXPECT_NE(static_cast<PlatformThreadHandle>(0), threads[index].thread_); | |
| 122 } | 122 } |
| 123 | 123 |
| 124 // Wait for the threads to finish. | 124 // Wait for the threads to finish. |
| 125 for (int index = 0; index < kMaxThreads; index++) { | 125 for (int index = 0; index < kMaxThreads; index++) { |
| 126 PlatformThread::Join(threads[index].thread_); | 126 threads[index]->Join(); |
| 127 delete threads[index]; |
| 127 } | 128 } |
| 129 |
| 128 StatsCounter zero_counter(kCounterZero); | 130 StatsCounter zero_counter(kCounterZero); |
| 129 StatsCounter lucky13_counter(kCounter1313); | 131 StatsCounter lucky13_counter(kCounter1313); |
| 130 StatsCounter increment_counter(kCounterIncrement); | 132 StatsCounter increment_counter(kCounterIncrement); |
| 131 StatsCounter decrement_counter(kCounterDecrement); | 133 StatsCounter decrement_counter(kCounterDecrement); |
| 132 StatsCounter mixed_counter(kCounterMixed); | 134 StatsCounter mixed_counter(kCounterMixed); |
| 133 | 135 |
| 134 // Verify the various counters are correct. | 136 // Verify the various counters are correct. |
| 135 std::wstring name; | 137 std::wstring name; |
| 136 name = L"c:" + kCounterZero; | 138 name = L"c:" + kCounterZero; |
| 137 EXPECT_EQ(0, table.GetCounterValue(name)); | 139 EXPECT_EQ(0, table.GetCounterValue(name)); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 StatsScope<StatsCounterTimer> timer(foo); | 375 StatsScope<StatsCounterTimer> timer(foo); |
| 374 StatsScope<StatsRate> timer2(bar); | 376 StatsScope<StatsRate> timer2(bar); |
| 375 PlatformThread::Sleep(500); | 377 PlatformThread::Sleep(500); |
| 376 } | 378 } |
| 377 EXPECT_LE(1000, table.GetCounterValue(L"t:foo")); | 379 EXPECT_LE(1000, table.GetCounterValue(L"t:foo")); |
| 378 EXPECT_LE(1000, table.GetCounterValue(L"t:bar")); | 380 EXPECT_LE(1000, table.GetCounterValue(L"t:bar")); |
| 379 EXPECT_EQ(2, table.GetCounterValue(L"c:bar")); | 381 EXPECT_EQ(2, table.GetCounterValue(L"c:bar")); |
| 380 } | 382 } |
| 381 | 383 |
| 382 } // namespace | 384 } // namespace |
| OLD | NEW |