| 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_ptr.h" |
| 20 #include "base/memory/scoped_vector.h" | 21 #include "base/memory/scoped_vector.h" |
| 21 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
| 22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 23 #include "net/base/request_priority.h" | 24 #include "net/base/request_priority.h" |
| 25 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_context.h" | 26 #include "net/url_request/url_request_context.h" |
| 25 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
| 26 #include "net/url_request/url_request_throttler_manager.h" | 28 #include "net/url_request/url_request_throttler_manager.h" |
| 27 #include "net/url_request/url_request_throttler_test_support.h" | 29 #include "net/url_request/url_request_throttler_test_support.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 31 |
| 30 using base::TimeDelta; | 32 using base::TimeDelta; |
| 31 using base::TimeTicks; | 33 using base::TimeTicks; |
| 32 | 34 |
| 33 namespace net { | 35 namespace net { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 // after all |Requester| objects. | 120 // after all |Requester| objects. |
| 119 class Server : public DiscreteTimeSimulation::Actor { | 121 class Server : public DiscreteTimeSimulation::Actor { |
| 120 public: | 122 public: |
| 121 Server(int max_queries_per_tick, double request_drop_ratio) | 123 Server(int max_queries_per_tick, double request_drop_ratio) |
| 122 : max_queries_per_tick_(max_queries_per_tick), | 124 : max_queries_per_tick_(max_queries_per_tick), |
| 123 request_drop_ratio_(request_drop_ratio), | 125 request_drop_ratio_(request_drop_ratio), |
| 124 num_overloaded_ticks_remaining_(0), | 126 num_overloaded_ticks_remaining_(0), |
| 125 num_current_tick_queries_(0), | 127 num_current_tick_queries_(0), |
| 126 num_overloaded_ticks_(0), | 128 num_overloaded_ticks_(0), |
| 127 max_experienced_queries_per_tick_(0), | 129 max_experienced_queries_per_tick_(0), |
| 128 mock_request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} | 130 mock_request_(context_.CreateRequest( |
| 131 GURL(), DEFAULT_PRIORITY, NULL, NULL)) {} |
| 129 | 132 |
| 130 void SetDowntime(const TimeTicks& start_time, const TimeDelta& duration) { | 133 void SetDowntime(const TimeTicks& start_time, const TimeDelta& duration) { |
| 131 start_downtime_ = start_time; | 134 start_downtime_ = start_time; |
| 132 end_downtime_ = start_time + duration; | 135 end_downtime_ = start_time + duration; |
| 133 } | 136 } |
| 134 | 137 |
| 135 virtual void AdvanceTime(const TimeTicks& absolute_time) OVERRIDE { | 138 virtual void AdvanceTime(const TimeTicks& absolute_time) OVERRIDE { |
| 136 now_ = absolute_time; | 139 now_ = absolute_time; |
| 137 } | 140 } |
| 138 | 141 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 185 |
| 183 int num_overloaded_ticks() const { | 186 int num_overloaded_ticks() const { |
| 184 return num_overloaded_ticks_; | 187 return num_overloaded_ticks_; |
| 185 } | 188 } |
| 186 | 189 |
| 187 int max_experienced_queries_per_tick() const { | 190 int max_experienced_queries_per_tick() const { |
| 188 return max_experienced_queries_per_tick_; | 191 return max_experienced_queries_per_tick_; |
| 189 } | 192 } |
| 190 | 193 |
| 191 const URLRequest& mock_request() const { | 194 const URLRequest& mock_request() const { |
| 192 return mock_request_; | 195 return *mock_request_.get(); |
| 193 } | 196 } |
| 194 | 197 |
| 195 std::string VisualizeASCII(int terminal_width) { | 198 std::string VisualizeASCII(int terminal_width) { |
| 196 // Account for | characters we place at left of graph. | 199 // Account for | characters we place at left of graph. |
| 197 terminal_width -= 1; | 200 terminal_width -= 1; |
| 198 | 201 |
| 199 VerboseOut("Overloaded for %d of %d ticks.\n", | 202 VerboseOut("Overloaded for %d of %d ticks.\n", |
| 200 num_overloaded_ticks_, requests_per_tick_.size()); | 203 num_overloaded_ticks_, requests_per_tick_.size()); |
| 201 VerboseOut("Got maximum of %d requests in a tick.\n\n", | 204 VerboseOut("Got maximum of %d requests in a tick.\n\n", |
| 202 max_experienced_queries_per_tick_); | 205 max_experienced_queries_per_tick_); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 TimeTicks end_downtime_; | 287 TimeTicks end_downtime_; |
| 285 const int max_queries_per_tick_; | 288 const int max_queries_per_tick_; |
| 286 const double request_drop_ratio_; // Ratio of requests to 503 when failing. | 289 const double request_drop_ratio_; // Ratio of requests to 503 when failing. |
| 287 int num_overloaded_ticks_remaining_; | 290 int num_overloaded_ticks_remaining_; |
| 288 int num_current_tick_queries_; | 291 int num_current_tick_queries_; |
| 289 int num_overloaded_ticks_; | 292 int num_overloaded_ticks_; |
| 290 int max_experienced_queries_per_tick_; | 293 int max_experienced_queries_per_tick_; |
| 291 std::vector<int> requests_per_tick_; | 294 std::vector<int> requests_per_tick_; |
| 292 | 295 |
| 293 TestURLRequestContext context_; | 296 TestURLRequestContext context_; |
| 294 TestURLRequest mock_request_; | 297 scoped_ptr<URLRequest> mock_request_; |
| 295 | 298 |
| 296 DISALLOW_COPY_AND_ASSIGN(Server); | 299 DISALLOW_COPY_AND_ASSIGN(Server); |
| 297 }; | 300 }; |
| 298 | 301 |
| 299 // Mock throttler entry used by Requester class. | 302 // Mock throttler entry used by Requester class. |
| 300 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 303 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
| 301 public: | 304 public: |
| 302 explicit MockURLRequestThrottlerEntry(URLRequestThrottlerManager* manager) | 305 explicit MockURLRequestThrottlerEntry(URLRequestThrottlerManager* manager) |
| 303 : URLRequestThrottlerEntry(manager, std::string()), | 306 : URLRequestThrottlerEntry(manager, std::string()), |
| 304 mock_backoff_entry_(&backoff_policy_) {} | 307 mock_backoff_entry_(&backoff_policy_) {} |
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 trials[i].PrintTrialDescription(); | 753 trials[i].PrintTrialDescription(); |
| 751 trials[i].stats.ReportTrialResult(increase_ratio); | 754 trials[i].stats.ReportTrialResult(increase_ratio); |
| 752 } | 755 } |
| 753 | 756 |
| 754 VerboseOut("Average increase ratio was %.4f\n", average_increase_ratio); | 757 VerboseOut("Average increase ratio was %.4f\n", average_increase_ratio); |
| 755 VerboseOut("Maximum increase ratio was %.4f\n", max_increase_ratio); | 758 VerboseOut("Maximum increase ratio was %.4f\n", max_increase_ratio); |
| 756 } | 759 } |
| 757 | 760 |
| 758 } // namespace | 761 } // namespace |
| 759 } // namespace net | 762 } // namespace net |
| OLD | NEW |