partition_lock_perftest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/allocator/partition_allocator/partition_lock.h"
  5. #include <vector>
  6. #include "base/allocator/partition_allocator/partition_alloc_base/threading/platform_thread_for_testing.h"
  7. #include "base/allocator/partition_allocator/partition_alloc_base/time/time.h"
  8. #include "base/timer/lap_timer.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "testing/perf/perf_result_reporter.h"
  11. namespace partition_alloc::internal {
  12. namespace {
  13. constexpr int kWarmupRuns = 1;
  14. constexpr ::base::TimeDelta kTimeLimit = ::base::Seconds(1);
  15. constexpr int kTimeCheckInterval = 100000;
  16. constexpr char kMetricPrefixLock[] = "PartitionLock.";
  17. constexpr char kMetricLockUnlockThroughput[] = "lock_unlock_throughput";
  18. constexpr char kMetricLockUnlockLatency[] = "lock_unlock_latency_ns";
  19. constexpr char kStoryBaseline[] = "baseline_story";
  20. constexpr char kStoryWithCompetingThread[] = "with_competing_thread";
  21. perf_test::PerfResultReporter SetUpReporter(const std::string& story_name) {
  22. perf_test::PerfResultReporter reporter(kMetricPrefixLock, story_name);
  23. reporter.RegisterImportantMetric(kMetricLockUnlockThroughput, "runs/s");
  24. reporter.RegisterImportantMetric(kMetricLockUnlockLatency, "ns");
  25. return reporter;
  26. }
  27. class Spin : public base::PlatformThreadForTesting::Delegate {
  28. public:
  29. Spin(Lock* lock, uint32_t* data)
  30. : lock_(lock), data_(data), should_stop_(false) {}
  31. ~Spin() override = default;
  32. void ThreadMain() override {
  33. started_count_++;
  34. // Local variable to avoid "cache line ping-pong" from influencing the
  35. // results.
  36. uint32_t count = 0;
  37. while (!should_stop_.load(std::memory_order_relaxed)) {
  38. lock_->Acquire();
  39. count++;
  40. lock_->Release();
  41. }
  42. lock_->Acquire();
  43. (*data_) += count;
  44. lock_->Release();
  45. }
  46. // Called from another thread to stop the loop.
  47. void Stop() { should_stop_ = true; }
  48. int started_count() const { return started_count_; }
  49. private:
  50. Lock* lock_;
  51. uint32_t* data_ GUARDED_BY(lock_);
  52. std::atomic<bool> should_stop_;
  53. std::atomic<int> started_count_{0};
  54. };
  55. } // namespace
  56. TEST(PartitionLockPerfTest, Simple) {
  57. base::LapTimer timer(kWarmupRuns, kTimeLimit, kTimeCheckInterval);
  58. [[maybe_unused]] uint32_t data = 0;
  59. Lock lock;
  60. do {
  61. lock.Acquire();
  62. data += 1;
  63. lock.Release();
  64. timer.NextLap();
  65. } while (!timer.HasTimeLimitExpired());
  66. auto reporter = SetUpReporter(kStoryBaseline);
  67. reporter.AddResult(kMetricLockUnlockThroughput, timer.LapsPerSecond());
  68. reporter.AddResult(kMetricLockUnlockLatency, 1e9 / timer.LapsPerSecond());
  69. }
  70. TEST(PartitionLockPerfTest, WithCompetingThreads) {
  71. uint32_t data = 0;
  72. Lock lock;
  73. // Starts a competing thread executing the same loop as this thread.
  74. Spin thread_main(&lock, &data);
  75. std::vector<base::PlatformThreadHandle> thread_handles;
  76. constexpr int kThreads = 4;
  77. for (int i = 0; i < kThreads; i++) {
  78. base::PlatformThreadHandle thread_handle;
  79. ASSERT_TRUE(base::PlatformThreadForTesting::Create(0, &thread_main,
  80. &thread_handle));
  81. thread_handles.push_back(thread_handle);
  82. }
  83. // Wait for all the threads to start.
  84. while (thread_main.started_count() != kThreads) {
  85. }
  86. base::LapTimer timer(kWarmupRuns, kTimeLimit, kTimeCheckInterval);
  87. do {
  88. lock.Acquire();
  89. data += 1;
  90. lock.Release();
  91. timer.NextLap();
  92. } while (!timer.HasTimeLimitExpired());
  93. thread_main.Stop();
  94. for (int i = 0; i < kThreads; i++) {
  95. base::PlatformThreadForTesting::Join(thread_handles[i]);
  96. }
  97. auto reporter = SetUpReporter(kStoryWithCompetingThread);
  98. reporter.AddResult(kMetricLockUnlockThroughput, timer.LapsPerSecond());
  99. reporter.AddResult(kMetricLockUnlockLatency, 1e9 / timer.LapsPerSecond());
  100. }
  101. } // namespace partition_alloc::internal