| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/renderer/scheduler/resource_dispatch_throttler.h" | 5 #include "content/renderer/scheduler/resource_dispatch_throttler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 class ResourceDispatchThrottlerForTest : public ResourceDispatchThrottler { | 69 class ResourceDispatchThrottlerForTest : public ResourceDispatchThrottler { |
| 70 public: | 70 public: |
| 71 ResourceDispatchThrottlerForTest(IPC::Sender* sender, | 71 ResourceDispatchThrottlerForTest(IPC::Sender* sender, |
| 72 scheduler::RendererScheduler* scheduler) | 72 scheduler::RendererScheduler* scheduler) |
| 73 : ResourceDispatchThrottler( | 73 : ResourceDispatchThrottler( |
| 74 sender, | 74 sender, |
| 75 scheduler, | 75 scheduler, |
| 76 base::TimeDelta::FromSecondsD(kFlushPeriodSeconds), | 76 base::TimeDelta::FromSecondsD(kFlushPeriodSeconds), |
| 77 kRequestsPerFlush), | 77 kRequestsPerFlush), |
| 78 now_(base::TimeTicks() + base::TimeDelta::FromDays(1)), |
| 78 flush_scheduled_(false) {} | 79 flush_scheduled_(false) {} |
| 79 ~ResourceDispatchThrottlerForTest() override {} | 80 ~ResourceDispatchThrottlerForTest() override {} |
| 80 | 81 |
| 81 void Advance(base::TimeDelta delta) { now_ += delta; } | 82 void Advance(base::TimeDelta delta) { now_ += delta; } |
| 82 | 83 |
| 83 bool RunScheduledFlush() { | 84 bool RunScheduledFlush() { |
| 84 if (!flush_scheduled_) | 85 if (!flush_scheduled_) |
| 85 return false; | 86 return false; |
| 86 | 87 |
| 87 flush_scheduled_ = false; | 88 flush_scheduled_ = false; |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 SetHighPriorityWorkAnticipated(true); | 246 SetHighPriorityWorkAnticipated(true); |
| 246 | 247 |
| 247 for (size_t i = 0; i < kRequestsPerFlush * 2; ++i) { | 248 for (size_t i = 0; i < kRequestsPerFlush * 2; ++i) { |
| 248 Advance(base::TimeDelta::FromSecondsD(kFlushPeriodSeconds * 2)); | 249 Advance(base::TimeDelta::FromSecondsD(kFlushPeriodSeconds * 2)); |
| 249 RequestResource(); | 250 RequestResource(); |
| 250 EXPECT_EQ(1U, GetAndResetSentMessageCount()); | 251 EXPECT_EQ(1U, GetAndResetSentMessageCount()); |
| 251 EXPECT_FALSE(FlushScheduled()); | 252 EXPECT_FALSE(FlushScheduled()); |
| 252 } | 253 } |
| 253 } | 254 } |
| 254 | 255 |
| 256 TEST_F(ResourceDispatchThrottlerTest, NotThrottledIfSendRateSufficientlyLow) { |
| 257 SetHighPriorityWorkAnticipated(true); |
| 258 |
| 259 // Continuous dispatch of resource requests below the allowed send rate |
| 260 // should never throttled. |
| 261 const base::TimeDelta kAllowedContinuousSendInterval = |
| 262 base::TimeDelta::FromSecondsD((kFlushPeriodSeconds / kRequestsPerFlush) + |
| 263 .00001); |
| 264 for (size_t i = 0; i < kRequestsPerFlush * 10; ++i) { |
| 265 Advance(kAllowedContinuousSendInterval); |
| 266 RequestResource(); |
| 267 EXPECT_EQ(1U, GetAndResetSentMessageCount()); |
| 268 EXPECT_FALSE(FlushScheduled()); |
| 269 } |
| 270 } |
| 271 |
| 272 TEST_F(ResourceDispatchThrottlerTest, ThrottledIfSendRateSufficientlyHigh) { |
| 273 SetHighPriorityWorkAnticipated(true); |
| 274 |
| 275 // Continuous dispatch of resource requests above the allowed send rate |
| 276 // should be throttled. |
| 277 const base::TimeDelta kThrottledContinuousSendInterval = |
| 278 base::TimeDelta::FromSecondsD((kFlushPeriodSeconds / kRequestsPerFlush) - |
| 279 .00001); |
| 280 |
| 281 for (size_t i = 0; i < kRequestsPerFlush * 10; ++i) { |
| 282 Advance(kThrottledContinuousSendInterval); |
| 283 RequestResource(); |
| 284 // Only the first batch of requests under the limit should be unthrottled. |
| 285 if (i < kRequestsPerFlush) { |
| 286 EXPECT_EQ(1U, GetAndResetSentMessageCount()); |
| 287 EXPECT_FALSE(FlushScheduled()); |
| 288 } else { |
| 289 EXPECT_EQ(0U, GetAndResetSentMessageCount()); |
| 290 EXPECT_TRUE(FlushScheduled()); |
| 291 } |
| 292 } |
| 293 } |
| 294 |
| 255 TEST_F(ResourceDispatchThrottlerTest, NotThrottledIfSyncMessage) { | 295 TEST_F(ResourceDispatchThrottlerTest, NotThrottledIfSyncMessage) { |
| 256 SetHighPriorityWorkAnticipated(true); | 296 SetHighPriorityWorkAnticipated(true); |
| 257 | 297 |
| 258 RequestResourceSync(); | 298 RequestResourceSync(); |
| 259 EXPECT_EQ(1U, GetAndResetSentMessageCount()); | 299 EXPECT_EQ(1U, GetAndResetSentMessageCount()); |
| 260 | 300 |
| 261 // Saturate the queue. | 301 // Saturate the queue. |
| 262 for (size_t i = 0; i < kRequestsPerFlush * 2; ++i) | 302 for (size_t i = 0; i < kRequestsPerFlush * 2; ++i) |
| 263 RequestResource(); | 303 RequestResource(); |
| 264 ASSERT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount()); | 304 ASSERT_EQ(kRequestsPerFlush, GetAndResetSentMessageCount()); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 EXPECT_EQ(priority_msg.type(), ResourceHostMsg_DidChangePriority::ID); | 405 EXPECT_EQ(priority_msg.type(), ResourceHostMsg_DidChangePriority::ID); |
| 366 EXPECT_EQ(cancel_msg.type(), ResourceHostMsg_CancelRequest::ID); | 406 EXPECT_EQ(cancel_msg.type(), ResourceHostMsg_CancelRequest::ID); |
| 367 | 407 |
| 368 EXPECT_EQ(GetRequestId(request_msg), GetRequestId(priority_msg)); | 408 EXPECT_EQ(GetRequestId(request_msg), GetRequestId(priority_msg)); |
| 369 EXPECT_EQ(GetRequestId(request_msg) - 1, GetRequestId(cancel_msg)); | 409 EXPECT_EQ(GetRequestId(request_msg) - 1, GetRequestId(cancel_msg)); |
| 370 } | 410 } |
| 371 EXPECT_FALSE(FlushScheduled()); | 411 EXPECT_FALSE(FlushScheduled()); |
| 372 } | 412 } |
| 373 | 413 |
| 374 } // namespace content | 414 } // namespace content |
| OLD | NEW |