Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(864)

Side by Side Diff: base/stats_table_unittest.cc

Issue 8751: Port last remaining test case in base/stats_table_unittest.cc, and... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« base/stats_table.cc ('K') | « base/stats_table.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/stats_table.h" 7 #include "base/stats_table.h"
8 #include "base/stats_counters.h" 8 #include "base/stats_counters.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 // CounterIncrement will be incremented each time. 59 // CounterIncrement will be incremented each time.
60 const std::wstring kCounterIncrement = L"CounterIncrement"; 60 const std::wstring kCounterIncrement = L"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::wstring kCounterDecrement = L"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::wstring kCounterMixed = L"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 // TODO(estade): port this test 69 class StatsTableThread : public PlatformThread::Delegate {
Dean McNamee 2008/10/31 19:20:37 Can you use SimpleThread, it was designed for unit
70 #if defined(OS_WIN) 70 public:
71 unsigned __stdcall StatsTableMultipleThreadMain(void* param) { 71 void ThreadMain();
72 PlatformThreadHandle thread_;
73 int id_;
74 };
75
76 void StatsTableThread::ThreadMain() {
72 // Each thread will open the shared memory and set counters 77 // Each thread will open the shared memory and set counters
73 // concurrently in a loop. We'll use some pauses to 78 // concurrently in a loop. We'll use some pauses to
74 // mixup the thread scheduling. 79 // mixup the thread scheduling.
75 int16 id = reinterpret_cast<int16>(param);
76 80
77 StatsCounter zero_counter(kCounterZero); 81 StatsCounter zero_counter(kCounterZero);
78 StatsCounter lucky13_counter(kCounter1313); 82 StatsCounter lucky13_counter(kCounter1313);
79 StatsCounter increment_counter(kCounterIncrement); 83 StatsCounter increment_counter(kCounterIncrement);
80 StatsCounter decrement_counter(kCounterDecrement); 84 StatsCounter decrement_counter(kCounterDecrement);
81 for (int index = 0; index < kThreadLoops; index++) { 85 for (int index = 0; index < kThreadLoops; index++) {
82 StatsCounter mixed_counter(kCounterMixed); // create this one in the loop 86 StatsCounter mixed_counter(kCounterMixed); // create this one in the loop
83 zero_counter.Set(0); 87 zero_counter.Set(0);
84 lucky13_counter.Set(1313); 88 lucky13_counter.Set(1313);
85 increment_counter.Increment(); 89 increment_counter.Increment();
86 decrement_counter.Decrement(); 90 decrement_counter.Decrement();
87 if (id % 2) 91 if (id_ % 2)
88 mixed_counter.Decrement(); 92 mixed_counter.Decrement();
89 else 93 else
90 mixed_counter.Increment(); 94 mixed_counter.Increment();
91 PlatformThread::Sleep(index % 10); // short wait 95 PlatformThread::Sleep(index % 10); // short wait
92 } 96 }
93 return 0;
94 } 97 }
95 98
96 // Create a few threads and have them poke on their counters. 99 // Create a few threads and have them poke on their counters.
97 TEST_F(StatsTableTest, MultipleThreads) { 100 TEST_F(StatsTableTest, MultipleThreads) {
98 // Create a stats table. 101 // Create a stats table.
99 const std::wstring kTableName = L"MultipleThreadStatTable"; 102 const std::wstring kTableName = L"MultipleThreadStatTable";
100 const int kMaxThreads = 20; 103 const int kMaxThreads = 20;
101 const int kMaxCounter = 5; 104 const int kMaxCounter = 5;
102 StatsTable table(kTableName, kMaxThreads, kMaxCounter); 105 StatsTable table(kTableName, kMaxThreads, kMaxCounter);
103 StatsTable::set_current(&table); 106 StatsTable::set_current(&table);
104 107
105 EXPECT_EQ(0, table.CountThreadsRegistered()); 108 EXPECT_EQ(0, table.CountThreadsRegistered());
106 109
107 // Spin up a set of threads to go bang on the various counters. 110 // Spin up a set of threads to go bang on the various counters.
108 // After we join the threads, we'll make sure the counters 111 // After we join the threads, we'll make sure the counters
109 // contain the values we expected. 112 // contain the values we expected.
110 HANDLE threads[kMaxThreads]; 113 StatsTableThread threads[kMaxThreads];
111 114
112 // Spawn the threads. 115 // Spawn the threads.
113 for (int16 index = 0; index < kMaxThreads; index++) { 116 for (int index = 0; index < kMaxThreads; index++) {
114 void* argument = reinterpret_cast<void*>(index); 117 threads[index].id_ = index;
115 unsigned thread_id; 118 bool created =
116 threads[index] = reinterpret_cast<HANDLE>( 119 PlatformThread::Create(0, &threads[index], &threads[index].thread_);
117 _beginthreadex(NULL, 0, StatsTableMultipleThreadMain, argument, 0, 120 EXPECT_EQ(true, created);
118 &thread_id)); 121 EXPECT_NE(static_cast<PlatformThreadHandle>(0), threads[index].thread_);
119 EXPECT_NE((HANDLE)NULL, threads[index]);
120 } 122 }
121 123
122 // Wait for the threads to finish. 124 // Wait for the threads to finish.
123 for (int index = 0; index < kMaxThreads; index++) { 125 for (int index = 0; index < kMaxThreads; index++) {
124 DWORD rv = WaitForSingleObject(threads[index], 60 * 1000); 126 PlatformThread::Join(threads[index].thread_);
125 EXPECT_EQ(rv, WAIT_OBJECT_0); // verify all threads finished
126 } 127 }
127 StatsCounter zero_counter(kCounterZero); 128 StatsCounter zero_counter(kCounterZero);
128 StatsCounter lucky13_counter(kCounter1313); 129 StatsCounter lucky13_counter(kCounter1313);
129 StatsCounter increment_counter(kCounterIncrement); 130 StatsCounter increment_counter(kCounterIncrement);
130 StatsCounter decrement_counter(kCounterDecrement); 131 StatsCounter decrement_counter(kCounterDecrement);
131 StatsCounter mixed_counter(kCounterMixed); 132 StatsCounter mixed_counter(kCounterMixed);
132 133
133 // Verify the various counters are correct. 134 // Verify the various counters are correct.
134 std::wstring name; 135 std::wstring name;
135 name = L"c:" + kCounterZero; 136 name = L"c:" + kCounterZero;
136 EXPECT_EQ(0, table.GetCounterValue(name)); 137 EXPECT_EQ(0, table.GetCounterValue(name));
137 name = L"c:" + kCounter1313; 138 name = L"c:" + kCounter1313;
138 EXPECT_EQ(1313 * kMaxThreads, 139 EXPECT_EQ(1313 * kMaxThreads,
139 table.GetCounterValue(name)); 140 table.GetCounterValue(name));
140 name = L"c:" + kCounterIncrement; 141 name = L"c:" + kCounterIncrement;
141 EXPECT_EQ(kMaxThreads * kThreadLoops, 142 EXPECT_EQ(kMaxThreads * kThreadLoops,
142 table.GetCounterValue(name)); 143 table.GetCounterValue(name));
143 name = L"c:" + kCounterDecrement; 144 name = L"c:" + kCounterDecrement;
144 EXPECT_EQ(-kMaxThreads * kThreadLoops, 145 EXPECT_EQ(-kMaxThreads * kThreadLoops,
145 table.GetCounterValue(name)); 146 table.GetCounterValue(name));
146 name = L"c:" + kCounterMixed; 147 name = L"c:" + kCounterMixed;
147 EXPECT_EQ((kMaxThreads % 2) * kThreadLoops, 148 EXPECT_EQ((kMaxThreads % 2) * kThreadLoops,
148 table.GetCounterValue(name)); 149 table.GetCounterValue(name));
149 EXPECT_EQ(0, table.CountThreadsRegistered()); 150 EXPECT_EQ(0, table.CountThreadsRegistered());
150 } 151 }
151 #endif // defined(OS_WIN)
152 152
153 const std::wstring kTableName = L"MultipleProcessStatTable"; 153 const std::wstring kTableName = L"MultipleProcessStatTable";
154 154
155 MULTIPROCESS_TEST_MAIN(StatsTableMultipleProcessMain) { 155 MULTIPROCESS_TEST_MAIN(StatsTableMultipleProcessMain) {
156 // Each process will open the shared memory and set counters 156 // Each process will open the shared memory and set counters
157 // concurrently in a loop. We'll use some pauses to 157 // concurrently in a loop. We'll use some pauses to
158 // mixup the scheduling. 158 // mixup the scheduling.
159 159
160 StatsTable table(kTableName, 0, 0); 160 StatsTable table(kTableName, 0, 0);
161 StatsTable::set_current(&table); 161 StatsTable::set_current(&table);
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 StatsScope<StatsCounterTimer> timer(foo); 373 StatsScope<StatsCounterTimer> timer(foo);
374 StatsScope<StatsRate> timer2(bar); 374 StatsScope<StatsRate> timer2(bar);
375 PlatformThread::Sleep(500); 375 PlatformThread::Sleep(500);
376 } 376 }
377 EXPECT_LE(1000, table.GetCounterValue(L"t:foo")); 377 EXPECT_LE(1000, table.GetCounterValue(L"t:foo"));
378 EXPECT_LE(1000, table.GetCounterValue(L"t:bar")); 378 EXPECT_LE(1000, table.GetCounterValue(L"t:bar"));
379 EXPECT_EQ(2, table.GetCounterValue(L"c:bar")); 379 EXPECT_EQ(2, table.GetCounterValue(L"c:bar"));
380 } 380 }
381 381
382 } // namespace 382 } // namespace
OLDNEW
« base/stats_table.cc ('K') | « base/stats_table.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698