| 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) |
| 6 #include <process.h> |
| 7 #include <windows.h> |
| 8 #endif |
| 9 |
| 5 #include "base/multiprocess_test.h" | 10 #include "base/multiprocess_test.h" |
| 6 #include "base/platform_thread.h" | 11 #include "base/platform_thread.h" |
| 7 #include "base/simple_thread.h" | 12 #include "base/simple_thread.h" |
| 8 #include "base/shared_memory.h" | 13 #include "base/shared_memory.h" |
| 9 #include "base/stats_table.h" | 14 #include "base/stats_table.h" |
| 10 #include "base/stats_counters.h" | 15 #include "base/stats_counters.h" |
| 11 #include "base/string_util.h" | 16 #include "base/utf_string_conversions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/multiprocess_func_list.h" | 18 #include "testing/multiprocess_func_list.h" |
| 14 | 19 |
| 15 #if defined(OS_WIN) | |
| 16 #include <process.h> | |
| 17 #include <windows.h> | |
| 18 #endif | |
| 19 | |
| 20 namespace base { | 20 namespace base { |
| 21 | 21 |
| 22 class StatsTableTest : public MultiProcessTest { | 22 class StatsTableTest : public MultiProcessTest { |
| 23 public: | 23 public: |
| 24 void DeleteShmem(std::string name) { | 24 void DeleteShmem(std::string name) { |
| 25 base::SharedMemory mem; | 25 base::SharedMemory mem; |
| 26 mem.Delete(UTF8ToWide(name)); | 26 mem.Delete(UTF8ToWide(name)); |
| 27 } | 27 } |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 // Open a StatsTable and verify that we can write to each of the | 30 // Open a StatsTable and verify that we can write to each of the |
| 31 // locations in the table. | 31 // locations in the table. |
| 32 TEST_F(StatsTableTest, VerifySlots) { | 32 TEST_F(StatsTableTest, VerifySlots) { |
| 33 const std::string kTableName = "VerifySlotsStatTable"; | 33 const std::string kTableName = "VerifySlotsStatTable"; |
| 34 const int kMaxThreads = 1; | 34 const int kMaxThreads = 1; |
| 35 const int kMaxCounter = 5; | 35 const int kMaxCounter = 5; |
| 36 DeleteShmem(kTableName); | 36 DeleteShmem(kTableName); |
| 37 StatsTable table(kTableName, kMaxThreads, kMaxCounter); | 37 StatsTable table(kTableName, kMaxThreads, kMaxCounter); |
| 38 | 38 |
| 39 // Register a single thread. | 39 // Register a single thread. |
| 40 std::string thread_name = "mainThread"; | 40 std::string thread_name = "mainThread"; |
| 41 int slot_id = table.RegisterThread(thread_name); | 41 int slot_id = table.RegisterThread(thread_name); |
| 42 EXPECT_NE(slot_id, 0); | 42 EXPECT_NE(slot_id, 0); |
| 43 | 43 |
| 44 // Fill up the table with counters. | 44 // Fill up the table with counters. |
| 45 std::string counter_base_name = "counter"; | 45 std::string counter_base_name = "counter"; |
| 46 for (int index=0; index < kMaxCounter; index++) { | 46 for (int index = 0; index < kMaxCounter; index++) { |
| 47 std::string counter_name = counter_base_name; | 47 std::string counter_name = counter_base_name; |
| 48 StringAppendF(&counter_name, "counter.ctr%d", index); | 48 StringAppendF(&counter_name, "counter.ctr%d", index); |
| 49 int counter_id = table.FindCounter(counter_name); | 49 int counter_id = table.FindCounter(counter_name); |
| 50 EXPECT_GT(counter_id, 0); | 50 EXPECT_GT(counter_id, 0); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Try to allocate an additional thread. Verify it fails. | 53 // Try to allocate an additional thread. Verify it fails. |
| 54 slot_id = table.RegisterThread("too many threads"); | 54 slot_id = table.RegisterThread("too many threads"); |
| 55 EXPECT_EQ(slot_id, 0); | 55 EXPECT_EQ(slot_id, 0); |
| 56 | 56 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 69 const std::string kCounterIncrement = "CounterIncrement"; | 69 const std::string kCounterIncrement = "CounterIncrement"; |
| 70 // CounterDecrement will be decremented each time. | 70 // CounterDecrement will be decremented each time. |
| 71 const std::string kCounterDecrement = "CounterDecrement"; | 71 const std::string kCounterDecrement = "CounterDecrement"; |
| 72 // CounterMixed will be incremented by odd numbered threads and | 72 // CounterMixed will be incremented by odd numbered threads and |
| 73 // decremented by even threads. | 73 // decremented by even threads. |
| 74 const std::string kCounterMixed = "CounterMixed"; | 74 const std::string kCounterMixed = "CounterMixed"; |
| 75 // The number of thread loops that we will do. | 75 // The number of thread loops that we will do. |
| 76 const int kThreadLoops = 1000; | 76 const int kThreadLoops = 1000; |
| 77 | 77 |
| 78 class StatsTableThread : public base::SimpleThread { | 78 class StatsTableThread : public base::SimpleThread { |
| 79 public: | 79 public: |
| 80 StatsTableThread(std::string name, int id) | 80 StatsTableThread(std::string name, int id) |
| 81 : base::SimpleThread(name), id_(id) { } | 81 : base::SimpleThread(name), id_(id) { } |
| 82 virtual void Run(); | 82 virtual void Run(); |
| 83 private: | 83 private: |
| 84 int id_; | 84 int id_; |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 void StatsTableThread::Run() { | 87 void StatsTableThread::Run() { |
| 88 // Each thread will open the shared memory and set counters | 88 // Each thread will open the shared memory and set counters |
| 89 // concurrently in a loop. We'll use some pauses to | 89 // concurrently in a loop. We'll use some pauses to |
| 90 // mixup the thread scheduling. | 90 // mixup the thread scheduling. |
| 91 | 91 |
| 92 StatsCounter zero_counter(kCounterZero); | 92 StatsCounter zero_counter(kCounterZero); |
| 93 StatsCounter lucky13_counter(kCounter1313); | 93 StatsCounter lucky13_counter(kCounter1313); |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 PlatformThread::Sleep(500); | 398 PlatformThread::Sleep(500); |
| 399 } | 399 } |
| 400 EXPECT_LE(1000, table.GetCounterValue("t:foo")); | 400 EXPECT_LE(1000, table.GetCounterValue("t:foo")); |
| 401 EXPECT_LE(1000, table.GetCounterValue("t:bar")); | 401 EXPECT_LE(1000, table.GetCounterValue("t:bar")); |
| 402 EXPECT_EQ(2, table.GetCounterValue("c:bar")); | 402 EXPECT_EQ(2, table.GetCounterValue("c:bar")); |
| 403 | 403 |
| 404 DeleteShmem(kTableName); | 404 DeleteShmem(kTableName); |
| 405 } | 405 } |
| 406 | 406 |
| 407 } // namespace base | 407 } // namespace base |
| OLD | NEW |