| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // The tests in this file attempt to verify the following through simulation: | 5 // The tests in this file attempt to verify the following through simulation: |
| 6 // a) That a server experiencing overload will actually benefit from the | 6 // a) That a server experiencing overload will actually benefit from the |
| 7 // anti-DDoS throttling logic, i.e. that its traffic spike will subside | 7 // anti-DDoS throttling logic, i.e. that its traffic spike will subside |
| 8 // and be distributed over a longer period of time; | 8 // and be distributed over a longer period of time; |
| 9 // b) That "well-behaved" clients of a server under DDoS attack actually | 9 // b) That "well-behaved" clients of a server under DDoS attack actually |
| 10 // benefit from the anti-DDoS throttling logic; and | 10 // benefit from the anti-DDoS throttling logic; and |
| 11 // c) That the approximate increase in "perceived downtime" introduced by | 11 // c) That the approximate increase in "perceived downtime" introduced by |
| 12 // anti-DDoS throttling for various different actual downtimes is what | 12 // anti-DDoS throttling for various different actual downtimes is what |
| 13 // we expect it to be. | 13 // we expect it to be. |
| 14 | 14 |
| 15 #include <cmath> | 15 #include <cmath> |
| 16 #include <limits> | 16 #include <limits> |
| 17 #include <vector> | 17 #include <vector> |
| 18 | 18 |
| 19 #include "base/environment.h" | 19 #include "base/environment.h" |
| 20 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
| 21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 22 #include "base/time.h" | 22 #include "base/time.h" |
| 23 #include "net/url_request/url_request_test_util.h" |
| 23 #include "net/url_request/url_request_throttler_manager.h" | 24 #include "net/url_request/url_request_throttler_manager.h" |
| 24 #include "net/url_request/url_request_throttler_test_support.h" | 25 #include "net/url_request/url_request_throttler_test_support.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 27 |
| 27 using base::TimeDelta; | 28 using base::TimeDelta; |
| 28 using base::TimeTicks; | 29 using base::TimeTicks; |
| 29 | 30 |
| 30 namespace net { | 31 namespace net { |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 requests_per_tick_.push_back(num_current_tick_queries_); | 153 requests_per_tick_.push_back(num_current_tick_queries_); |
| 153 num_current_tick_queries_ = 0; | 154 num_current_tick_queries_ = 0; |
| 154 } | 155 } |
| 155 | 156 |
| 156 // This is called by Requester. It returns the response code from | 157 // This is called by Requester. It returns the response code from |
| 157 // the server. | 158 // the server. |
| 158 int HandleRequest() { | 159 int HandleRequest() { |
| 159 ++num_current_tick_queries_; | 160 ++num_current_tick_queries_; |
| 160 if (!start_downtime_.is_null() && | 161 if (!start_downtime_.is_null() && |
| 161 start_downtime_ < now_ && now_ < end_downtime_) { | 162 start_downtime_ < now_ && now_ < end_downtime_) { |
| 162 // TODO(joi): For the simulation measuring the increase in perceived | 163 // For the simulation measuring the increase in perceived |
| 163 // downtime, it might be interesting to count separately the | 164 // downtime, it might be interesting to count separately the |
| 164 // queries seen by the server (assuming a front-end reverse proxy | 165 // queries seen by the server (assuming a front-end reverse proxy |
| 165 // is what actually serves up the 503s in this case) so that we could | 166 // is what actually serves up the 503s in this case) so that we could |
| 166 // visualize the traffic spike seen by the server when it comes up, | 167 // visualize the traffic spike seen by the server when it comes up, |
| 167 // which would in many situations be ameliorated by the anti-DDoS | 168 // which would in many situations be ameliorated by the anti-DDoS |
| 168 // throttling. | 169 // throttling. |
| 169 return 503; | 170 return 503; |
| 170 } | 171 } |
| 171 | 172 |
| 172 if ((num_overloaded_ticks_remaining_ > 0 || | 173 if ((num_overloaded_ticks_remaining_ > 0 || |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 int num_current_tick_queries_; | 281 int num_current_tick_queries_; |
| 281 int num_overloaded_ticks_; | 282 int num_overloaded_ticks_; |
| 282 int max_experienced_queries_per_tick_; | 283 int max_experienced_queries_per_tick_; |
| 283 std::vector<int> requests_per_tick_; | 284 std::vector<int> requests_per_tick_; |
| 284 | 285 |
| 285 DISALLOW_COPY_AND_ASSIGN(Server); | 286 DISALLOW_COPY_AND_ASSIGN(Server); |
| 286 }; | 287 }; |
| 287 | 288 |
| 288 class TestingURLRequestThrottlerManager : public URLRequestThrottlerManager { | 289 class TestingURLRequestThrottlerManager : public URLRequestThrottlerManager { |
| 289 public: | 290 public: |
| 290 TestingURLRequestThrottlerManager() : URLRequestThrottlerManager() { | 291 TestingURLRequestThrottlerManager() : URLRequestThrottlerManager(&delegate_) { |
| 291 } | 292 } |
| 293 |
| 294 private: |
| 295 TestURLRequestThrottlerManagerDelegate delegate_; |
| 292 }; | 296 }; |
| 293 | 297 |
| 294 // Mock throttler entry used by Requester class. | 298 // Mock throttler entry used by Requester class. |
| 295 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 299 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
| 296 public: | 300 public: |
| 297 explicit MockURLRequestThrottlerEntry( | 301 explicit MockURLRequestThrottlerEntry( |
| 298 URLRequestThrottlerManager* manager) | 302 URLRequestThrottlerManager* manager) |
| 299 : URLRequestThrottlerEntry(manager, ""), | 303 : URLRequestThrottlerEntry(manager, ""), |
| 300 mock_backoff_entry_(&backoff_policy_) { | 304 mock_backoff_entry_(&backoff_policy_) { |
| 301 } | 305 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 TimeDelta current_jitter = TimeDelta::FromMilliseconds( | 424 TimeDelta current_jitter = TimeDelta::FromMilliseconds( |
| 421 request_jitter_.InMilliseconds() * base::RandDouble()); | 425 request_jitter_.InMilliseconds() * base::RandDouble()); |
| 422 if (base::RandInt(0, 1)) { | 426 if (base::RandInt(0, 1)) { |
| 423 effective_delay -= current_jitter; | 427 effective_delay -= current_jitter; |
| 424 } else { | 428 } else { |
| 425 effective_delay += current_jitter; | 429 effective_delay += current_jitter; |
| 426 } | 430 } |
| 427 | 431 |
| 428 if (throttler_entry_->fake_now() - time_of_last_attempt_ > | 432 if (throttler_entry_->fake_now() - time_of_last_attempt_ > |
| 429 effective_delay) { | 433 effective_delay) { |
| 430 if (!throttler_entry_->ShouldRejectRequest(0)) { | 434 if (!throttler_entry_->ShouldRejectRequest(NULL, 0)) { |
| 431 int status_code = server_->HandleRequest(); | 435 int status_code = server_->HandleRequest(); |
| 432 MockURLRequestThrottlerHeaderAdapter response_headers(status_code); | 436 MockURLRequestThrottlerHeaderAdapter response_headers(status_code); |
| 433 throttler_entry_->UpdateWithResponse("", &response_headers); | 437 throttler_entry_->UpdateWithResponse("", &response_headers); |
| 434 | 438 |
| 435 if (status_code == 200) { | 439 if (status_code == 200) { |
| 436 if (results_) | 440 if (results_) |
| 437 results_->AddSuccess(); | 441 results_->AddSuccess(); |
| 438 | 442 |
| 439 if (last_attempt_was_failure_) { | 443 if (last_attempt_was_failure_) { |
| 440 last_downtime_duration_ = | 444 last_downtime_duration_ = |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 trials[i].PrintTrialDescription(); | 749 trials[i].PrintTrialDescription(); |
| 746 trials[i].stats.ReportTrialResult(increase_ratio); | 750 trials[i].stats.ReportTrialResult(increase_ratio); |
| 747 } | 751 } |
| 748 | 752 |
| 749 VerboseOut("Average increase ratio was %.4f\n", average_increase_ratio); | 753 VerboseOut("Average increase ratio was %.4f\n", average_increase_ratio); |
| 750 VerboseOut("Maximum increase ratio was %.4f\n", max_increase_ratio); | 754 VerboseOut("Maximum increase ratio was %.4f\n", max_increase_ratio); |
| 751 } | 755 } |
| 752 | 756 |
| 753 } // namespace | 757 } // namespace |
| 754 } // namespace net | 758 } // namespace net |
| OLD | NEW |