Convert raw pointers to scoped_ptr in net module.
[chromium-blink-merge.git] / net / url_request / url_request_throttler_simulation_unittest.cc
blobda6afe1741d64927f7fe45e5695daea75a95222e
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
3 // found in the LICENSE file.
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
7 // anti-DDoS throttling logic, i.e. that its traffic spike will subside
8 // and be distributed over a longer period of time;
9 // b) That "well-behaved" clients of a server under DDoS attack actually
10 // benefit from the anti-DDoS throttling logic; and
11 // c) That the approximate increase in "perceived downtime" introduced by
12 // anti-DDoS throttling for various different actual downtimes is what
13 // we expect it to be.
15 #include <cmath>
16 #include <limits>
17 #include <vector>
19 #include "base/environment.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/memory/scoped_vector.h"
22 #include "base/rand_util.h"
23 #include "base/time/time.h"
24 #include "net/base/request_priority.h"
25 #include "net/url_request/url_request.h"
26 #include "net/url_request/url_request_context.h"
27 #include "net/url_request/url_request_test_util.h"
28 #include "net/url_request/url_request_throttler_manager.h"
29 #include "net/url_request/url_request_throttler_test_support.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 using base::TimeDelta;
33 using base::TimeTicks;
35 namespace net {
36 namespace {
38 // Set this variable in your environment if you want to see verbose results
39 // of the simulation tests.
40 const char kShowSimulationVariableName[] = "SHOW_SIMULATION_RESULTS";
42 // Prints output only if a given environment variable is set. We use this
43 // to not print any output for human evaluation when the test is run without
44 // supervision.
45 void VerboseOut(const char* format, ...) {
46 static bool have_checked_environment = false;
47 static bool should_print = false;
48 if (!have_checked_environment) {
49 have_checked_environment = true;
50 scoped_ptr<base::Environment> env(base::Environment::Create());
51 if (env->HasVar(kShowSimulationVariableName))
52 should_print = true;
55 if (should_print) {
56 va_list arglist;
57 va_start(arglist, format);
58 vprintf(format, arglist);
59 va_end(arglist);
63 // A simple two-phase discrete time simulation. Actors are added in the order
64 // they should take action at every tick of the clock. Ticks of the clock
65 // are two-phase:
66 // - Phase 1 advances every actor's time to a new absolute time.
67 // - Phase 2 asks each actor to perform their action.
68 class DiscreteTimeSimulation {
69 public:
70 class Actor {
71 public:
72 virtual ~Actor() {}
73 virtual void AdvanceTime(const TimeTicks& absolute_time) = 0;
74 virtual void PerformAction() = 0;
77 DiscreteTimeSimulation() {}
79 // Adds an |actor| to the simulation. The client of the simulation maintains
80 // ownership of |actor| and must ensure its lifetime exceeds that of the
81 // simulation. Actors should be added in the order you wish for them to
82 // act at each tick of the simulation.
83 void AddActor(Actor* actor) {
84 actors_.push_back(actor);
87 // Runs the simulation for, pretending |time_between_ticks| passes from one
88 // tick to the next. The start time will be the current real time. The
89 // simulation will stop when the simulated duration is equal to or greater
90 // than |maximum_simulated_duration|.
91 void RunSimulation(const TimeDelta& maximum_simulated_duration,
92 const TimeDelta& time_between_ticks) {
93 TimeTicks start_time = TimeTicks();
94 TimeTicks now = start_time;
95 while ((now - start_time) <= maximum_simulated_duration) {
96 for (std::vector<Actor*>::iterator it = actors_.begin();
97 it != actors_.end();
98 ++it) {
99 (*it)->AdvanceTime(now);
102 for (std::vector<Actor*>::iterator it = actors_.begin();
103 it != actors_.end();
104 ++it) {
105 (*it)->PerformAction();
108 now += time_between_ticks;
112 private:
113 std::vector<Actor*> actors_;
115 DISALLOW_COPY_AND_ASSIGN(DiscreteTimeSimulation);
118 // Represents a web server in a simulation of a server under attack by
119 // a lot of clients. Must be added to the simulation's list of actors
120 // after all |Requester| objects.
121 class Server : public DiscreteTimeSimulation::Actor {
122 public:
123 Server(int max_queries_per_tick, double request_drop_ratio)
124 : max_queries_per_tick_(max_queries_per_tick),
125 request_drop_ratio_(request_drop_ratio),
126 num_overloaded_ticks_remaining_(0),
127 num_current_tick_queries_(0),
128 num_overloaded_ticks_(0),
129 max_experienced_queries_per_tick_(0),
130 mock_request_(context_.CreateRequest(GURL(), DEFAULT_PRIORITY, NULL)) {}
132 void SetDowntime(const TimeTicks& start_time, const TimeDelta& duration) {
133 start_downtime_ = start_time;
134 end_downtime_ = start_time + duration;
137 void AdvanceTime(const TimeTicks& absolute_time) override {
138 now_ = absolute_time;
141 void PerformAction() override {
142 // We are inserted at the end of the actor's list, so all Requester
143 // instances have already done their bit.
144 if (num_current_tick_queries_ > max_experienced_queries_per_tick_)
145 max_experienced_queries_per_tick_ = num_current_tick_queries_;
147 if (num_current_tick_queries_ > max_queries_per_tick_) {
148 // We pretend the server fails for the next several ticks after it
149 // gets overloaded.
150 num_overloaded_ticks_remaining_ = 5;
151 ++num_overloaded_ticks_;
152 } else if (num_overloaded_ticks_remaining_ > 0) {
153 --num_overloaded_ticks_remaining_;
156 requests_per_tick_.push_back(num_current_tick_queries_);
157 num_current_tick_queries_ = 0;
160 // This is called by Requester. It returns the response code from
161 // the server.
162 int HandleRequest() {
163 ++num_current_tick_queries_;
164 if (!start_downtime_.is_null() &&
165 start_downtime_ < now_ && now_ < end_downtime_) {
166 // For the simulation measuring the increase in perceived
167 // downtime, it might be interesting to count separately the
168 // queries seen by the server (assuming a front-end reverse proxy
169 // is what actually serves up the 503s in this case) so that we could
170 // visualize the traffic spike seen by the server when it comes up,
171 // which would in many situations be ameliorated by the anti-DDoS
172 // throttling.
173 return 503;
176 if ((num_overloaded_ticks_remaining_ > 0 ||
177 num_current_tick_queries_ > max_queries_per_tick_) &&
178 base::RandDouble() < request_drop_ratio_) {
179 return 503;
182 return 200;
185 int num_overloaded_ticks() const {
186 return num_overloaded_ticks_;
189 int max_experienced_queries_per_tick() const {
190 return max_experienced_queries_per_tick_;
193 const URLRequest& mock_request() const {
194 return *mock_request_.get();
197 std::string VisualizeASCII(int terminal_width) {
198 // Account for | characters we place at left of graph.
199 terminal_width -= 1;
201 VerboseOut("Overloaded for %d of %d ticks.\n",
202 num_overloaded_ticks_, requests_per_tick_.size());
203 VerboseOut("Got maximum of %d requests in a tick.\n\n",
204 max_experienced_queries_per_tick_);
206 VerboseOut("Traffic graph:\n\n");
208 // Printing the graph like this is a bit overkill, but was very useful
209 // while developing the various simulations to see if they were testing
210 // the corner cases we want to simulate.
212 // Find the smallest number of whole ticks we need to group into a
213 // column that will let all ticks fit into the column width we have.
214 int num_ticks = requests_per_tick_.size();
215 double ticks_per_column_exact =
216 static_cast<double>(num_ticks) / static_cast<double>(terminal_width);
217 int ticks_per_column = std::ceil(ticks_per_column_exact);
218 DCHECK_GE(ticks_per_column * terminal_width, num_ticks);
220 // Sum up the column values.
221 int num_columns = num_ticks / ticks_per_column;
222 if (num_ticks % ticks_per_column)
223 ++num_columns;
224 DCHECK_LE(num_columns, terminal_width);
225 scoped_ptr<int[]> columns(new int[num_columns]);
226 for (int tx = 0; tx < num_ticks; ++tx) {
227 int cx = tx / ticks_per_column;
228 if (tx % ticks_per_column == 0)
229 columns[cx] = 0;
230 columns[cx] += requests_per_tick_[tx];
233 // Find the lowest integer divisor that will let the column values
234 // be represented in a graph of maximum height 50.
235 int max_value = 0;
236 for (int cx = 0; cx < num_columns; ++cx)
237 max_value = std::max(max_value, columns[cx]);
238 const int kNumRows = 50;
239 double row_divisor_exact = max_value / static_cast<double>(kNumRows);
240 int row_divisor = std::ceil(row_divisor_exact);
241 DCHECK_GE(row_divisor * kNumRows, max_value);
243 // To show the overload line, we calculate the appropriate value.
244 int overload_value = max_queries_per_tick_ * ticks_per_column;
246 // When num_ticks is not a whole multiple of ticks_per_column, the last
247 // column includes fewer ticks than the others. In this case, don't
248 // print it so that we don't show an inconsistent value.
249 int num_printed_columns = num_columns;
250 if (num_ticks % ticks_per_column)
251 --num_printed_columns;
253 // This is a top-to-bottom traversal of rows, left-to-right per row.
254 std::string output;
255 for (int rx = 0; rx < kNumRows; ++rx) {
256 int range_min = (kNumRows - rx) * row_divisor;
257 int range_max = range_min + row_divisor;
258 if (range_min == 0)
259 range_min = -1; // Make 0 values fit in the bottom range.
260 output.append("|");
261 for (int cx = 0; cx < num_printed_columns; ++cx) {
262 char block = ' ';
263 // Show the overload line.
264 if (range_min < overload_value && overload_value <= range_max)
265 block = '-';
267 // Preferentially, show the graph line.
268 if (range_min < columns[cx] && columns[cx] <= range_max)
269 block = '#';
271 output.append(1, block);
273 output.append("\n");
275 output.append("|");
276 output.append(num_printed_columns, '=');
278 return output;
281 const URLRequestContext& context() const { return context_; }
283 private:
284 TimeTicks now_;
285 TimeTicks start_downtime_; // Can be 0 to say "no downtime".
286 TimeTicks end_downtime_;
287 const int max_queries_per_tick_;
288 const double request_drop_ratio_; // Ratio of requests to 503 when failing.
289 int num_overloaded_ticks_remaining_;
290 int num_current_tick_queries_;
291 int num_overloaded_ticks_;
292 int max_experienced_queries_per_tick_;
293 std::vector<int> requests_per_tick_;
295 TestURLRequestContext context_;
296 scoped_ptr<URLRequest> mock_request_;
298 DISALLOW_COPY_AND_ASSIGN(Server);
301 // Mock throttler entry used by Requester class.
302 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry {
303 public:
304 explicit MockURLRequestThrottlerEntry(URLRequestThrottlerManager* manager)
305 : URLRequestThrottlerEntry(manager, std::string()),
306 backoff_entry_(&backoff_policy_, &fake_clock_) {}
308 const BackoffEntry* GetBackoffEntry() const override {
309 return &backoff_entry_;
312 BackoffEntry* GetBackoffEntry() override { return &backoff_entry_; }
314 TimeTicks ImplGetTimeNow() const override { return fake_clock_.NowTicks(); }
316 void SetFakeNow(const TimeTicks& fake_time) {
317 fake_clock_.set_now(fake_time);
320 protected:
321 ~MockURLRequestThrottlerEntry() override {}
323 private:
324 mutable TestTickClock fake_clock_;
325 BackoffEntry backoff_entry_;
328 // Registry of results for a class of |Requester| objects (e.g. attackers vs.
329 // regular clients).
330 class RequesterResults {
331 public:
332 RequesterResults()
333 : num_attempts_(0), num_successful_(0), num_failed_(0), num_blocked_(0) {
336 void AddSuccess() {
337 ++num_attempts_;
338 ++num_successful_;
341 void AddFailure() {
342 ++num_attempts_;
343 ++num_failed_;
346 void AddBlocked() {
347 ++num_attempts_;
348 ++num_blocked_;
351 int num_attempts() const { return num_attempts_; }
352 int num_successful() const { return num_successful_; }
353 int num_failed() const { return num_failed_; }
354 int num_blocked() const { return num_blocked_; }
356 double GetBlockedRatio() {
357 DCHECK(num_attempts_);
358 return static_cast<double>(num_blocked_) /
359 static_cast<double>(num_attempts_);
362 double GetSuccessRatio() {
363 DCHECK(num_attempts_);
364 return static_cast<double>(num_successful_) /
365 static_cast<double>(num_attempts_);
368 void PrintResults(const char* class_description) {
369 if (num_attempts_ == 0) {
370 VerboseOut("No data for %s\n", class_description);
371 return;
374 VerboseOut("Requester results for %s\n", class_description);
375 VerboseOut(" %d attempts\n", num_attempts_);
376 VerboseOut(" %d successes\n", num_successful_);
377 VerboseOut(" %d 5xx responses\n", num_failed_);
378 VerboseOut(" %d requests blocked\n", num_blocked_);
379 VerboseOut(" %.2f success ratio\n", GetSuccessRatio());
380 VerboseOut(" %.2f blocked ratio\n", GetBlockedRatio());
381 VerboseOut("\n");
384 private:
385 int num_attempts_;
386 int num_successful_;
387 int num_failed_;
388 int num_blocked_;
391 // Represents an Requester in a simulated DDoS situation, that periodically
392 // requests a specific resource.
393 class Requester : public DiscreteTimeSimulation::Actor {
394 public:
395 Requester(MockURLRequestThrottlerEntry* throttler_entry,
396 const TimeDelta& time_between_requests,
397 Server* server,
398 RequesterResults* results)
399 : throttler_entry_(throttler_entry),
400 time_between_requests_(time_between_requests),
401 last_attempt_was_failure_(false),
402 server_(server),
403 results_(results) {
404 DCHECK(server_);
407 void AdvanceTime(const TimeTicks& absolute_time) override {
408 if (time_of_last_success_.is_null())
409 time_of_last_success_ = absolute_time;
411 throttler_entry_->SetFakeNow(absolute_time);
414 void PerformAction() override {
415 TimeDelta effective_delay = time_between_requests_;
416 TimeDelta current_jitter = TimeDelta::FromMilliseconds(
417 request_jitter_.InMilliseconds() * base::RandDouble());
418 if (base::RandInt(0, 1)) {
419 effective_delay -= current_jitter;
420 } else {
421 effective_delay += current_jitter;
424 if (throttler_entry_->ImplGetTimeNow() - time_of_last_attempt_ >
425 effective_delay) {
426 if (!throttler_entry_->ShouldRejectRequest(
427 server_->mock_request(),
428 server_->context().network_delegate())) {
429 int status_code = server_->HandleRequest();
430 throttler_entry_->UpdateWithResponse(status_code);
432 if (status_code == 200) {
433 if (results_)
434 results_->AddSuccess();
436 if (last_attempt_was_failure_) {
437 last_downtime_duration_ =
438 throttler_entry_->ImplGetTimeNow() - time_of_last_success_;
441 time_of_last_success_ = throttler_entry_->ImplGetTimeNow();
442 last_attempt_was_failure_ = false;
443 } else {
444 if (results_)
445 results_->AddFailure();
446 last_attempt_was_failure_ = true;
448 } else {
449 if (results_)
450 results_->AddBlocked();
451 last_attempt_was_failure_ = true;
454 time_of_last_attempt_ = throttler_entry_->ImplGetTimeNow();
458 // Adds a delay until the first request, equal to a uniformly distributed
459 // value between now and now + max_delay.
460 void SetStartupJitter(const TimeDelta& max_delay) {
461 int delay_ms = base::RandInt(0, max_delay.InMilliseconds());
462 time_of_last_attempt_ = TimeTicks() +
463 TimeDelta::FromMilliseconds(delay_ms) - time_between_requests_;
466 void SetRequestJitter(const TimeDelta& request_jitter) {
467 request_jitter_ = request_jitter;
470 TimeDelta last_downtime_duration() const { return last_downtime_duration_; }
472 private:
473 scoped_refptr<MockURLRequestThrottlerEntry> throttler_entry_;
474 const TimeDelta time_between_requests_;
475 TimeDelta request_jitter_;
476 TimeTicks time_of_last_attempt_;
477 TimeTicks time_of_last_success_;
478 bool last_attempt_was_failure_;
479 TimeDelta last_downtime_duration_;
480 Server* const server_;
481 RequesterResults* const results_; // May be NULL.
483 DISALLOW_COPY_AND_ASSIGN(Requester);
486 void SimulateAttack(Server* server,
487 RequesterResults* attacker_results,
488 RequesterResults* client_results,
489 bool enable_throttling) {
490 const size_t kNumAttackers = 50;
491 const size_t kNumClients = 50;
492 DiscreteTimeSimulation simulation;
493 URLRequestThrottlerManager manager;
494 ScopedVector<Requester> requesters;
495 for (size_t i = 0; i < kNumAttackers; ++i) {
496 // Use a tiny time_between_requests so the attackers will ping the
497 // server at every tick of the simulation.
498 scoped_refptr<MockURLRequestThrottlerEntry> throttler_entry(
499 new MockURLRequestThrottlerEntry(&manager));
500 if (!enable_throttling)
501 throttler_entry->DisableBackoffThrottling();
503 Requester* attacker = new Requester(throttler_entry.get(),
504 TimeDelta::FromMilliseconds(1),
505 server,
506 attacker_results);
507 attacker->SetStartupJitter(TimeDelta::FromSeconds(120));
508 requesters.push_back(attacker);
509 simulation.AddActor(attacker);
511 for (size_t i = 0; i < kNumClients; ++i) {
512 // Normal clients only make requests every 2 minutes, plus/minus 1 minute.
513 scoped_refptr<MockURLRequestThrottlerEntry> throttler_entry(
514 new MockURLRequestThrottlerEntry(&manager));
515 if (!enable_throttling)
516 throttler_entry->DisableBackoffThrottling();
518 Requester* client = new Requester(throttler_entry.get(),
519 TimeDelta::FromMinutes(2),
520 server,
521 client_results);
522 client->SetStartupJitter(TimeDelta::FromSeconds(120));
523 client->SetRequestJitter(TimeDelta::FromMinutes(1));
524 requesters.push_back(client);
525 simulation.AddActor(client);
527 simulation.AddActor(server);
529 simulation.RunSimulation(TimeDelta::FromMinutes(6),
530 TimeDelta::FromSeconds(1));
533 TEST(URLRequestThrottlerSimulation, HelpsInAttack) {
534 Server unprotected_server(30, 1.0);
535 RequesterResults unprotected_attacker_results;
536 RequesterResults unprotected_client_results;
537 Server protected_server(30, 1.0);
538 RequesterResults protected_attacker_results;
539 RequesterResults protected_client_results;
540 SimulateAttack(&unprotected_server,
541 &unprotected_attacker_results,
542 &unprotected_client_results,
543 false);
544 SimulateAttack(&protected_server,
545 &protected_attacker_results,
546 &protected_client_results,
547 true);
549 // These assert that the DDoS protection actually benefits the
550 // server. Manual inspection of the traffic graphs will show this
551 // even more clearly.
552 EXPECT_GT(unprotected_server.num_overloaded_ticks(),
553 protected_server.num_overloaded_ticks());
554 EXPECT_GT(unprotected_server.max_experienced_queries_per_tick(),
555 protected_server.max_experienced_queries_per_tick());
557 // These assert that the DDoS protection actually benefits non-malicious
558 // (and non-degenerate/accidentally DDoSing) users.
559 EXPECT_LT(protected_client_results.GetBlockedRatio(),
560 protected_attacker_results.GetBlockedRatio());
561 EXPECT_GT(protected_client_results.GetSuccessRatio(),
562 unprotected_client_results.GetSuccessRatio());
564 // The rest is just for optional manual evaluation of the results;
565 // in particular the traffic pattern is interesting.
567 VerboseOut("\nUnprotected server's results:\n\n");
568 VerboseOut(unprotected_server.VisualizeASCII(132).c_str());
569 VerboseOut("\n\n");
570 VerboseOut("Protected server's results:\n\n");
571 VerboseOut(protected_server.VisualizeASCII(132).c_str());
572 VerboseOut("\n\n");
574 unprotected_attacker_results.PrintResults(
575 "attackers attacking unprotected server.");
576 unprotected_client_results.PrintResults(
577 "normal clients making requests to unprotected server.");
578 protected_attacker_results.PrintResults(
579 "attackers attacking protected server.");
580 protected_client_results.PrintResults(
581 "normal clients making requests to protected server.");
584 // Returns the downtime perceived by the client, as a ratio of the
585 // actual downtime.
586 double SimulateDowntime(const TimeDelta& duration,
587 const TimeDelta& average_client_interval,
588 bool enable_throttling) {
589 TimeDelta time_between_ticks = duration / 200;
590 TimeTicks start_downtime = TimeTicks() + (duration / 2);
592 // A server that never rejects requests, but will go down for maintenance.
593 Server server(std::numeric_limits<int>::max(), 1.0);
594 server.SetDowntime(start_downtime, duration);
596 URLRequestThrottlerManager manager;
597 scoped_refptr<MockURLRequestThrottlerEntry> throttler_entry(
598 new MockURLRequestThrottlerEntry(&manager));
599 if (!enable_throttling)
600 throttler_entry->DisableBackoffThrottling();
602 Requester requester(
603 throttler_entry.get(), average_client_interval, &server, NULL);
604 requester.SetStartupJitter(duration / 3);
605 requester.SetRequestJitter(average_client_interval);
607 DiscreteTimeSimulation simulation;
608 simulation.AddActor(&requester);
609 simulation.AddActor(&server);
611 simulation.RunSimulation(duration * 2, time_between_ticks);
613 return static_cast<double>(
614 requester.last_downtime_duration().InMilliseconds()) /
615 static_cast<double>(duration.InMilliseconds());
618 TEST(URLRequestThrottlerSimulation, PerceivedDowntimeRatio) {
619 struct Stats {
620 // Expected interval that we expect the ratio of downtime when anti-DDoS
621 // is enabled and downtime when anti-DDoS is not enabled to fall within.
623 // The expected interval depends on two things: The exponential back-off
624 // policy encoded in URLRequestThrottlerEntry, and the test or set of
625 // tests that the Stats object is tracking (e.g. a test where the client
626 // retries very rapidly on a very long downtime will tend to increase the
627 // number).
629 // To determine an appropriate new interval when parameters have changed,
630 // run the test a few times (you may have to Ctrl-C out of it after a few
631 // seconds) and choose an interval that the test converges quickly and
632 // reliably to. Then set the new interval, and run the test e.g. 20 times
633 // in succession to make sure it never takes an obscenely long time to
634 // converge to this interval.
635 double expected_min_increase;
636 double expected_max_increase;
638 size_t num_runs;
639 double total_ratio_unprotected;
640 double total_ratio_protected;
642 bool DidConverge(double* increase_ratio_out) {
643 double unprotected_ratio = total_ratio_unprotected / num_runs;
644 double protected_ratio = total_ratio_protected / num_runs;
645 double increase_ratio = protected_ratio / unprotected_ratio;
646 if (increase_ratio_out)
647 *increase_ratio_out = increase_ratio;
648 return expected_min_increase <= increase_ratio &&
649 increase_ratio <= expected_max_increase;
652 void ReportTrialResult(double increase_ratio) {
653 VerboseOut(
654 " Perceived downtime with throttling is %.4f times without.\n",
655 increase_ratio);
656 VerboseOut(" Test result after %d trials.\n", num_runs);
660 Stats global_stats = { 1.08, 1.15 };
662 struct Trial {
663 TimeDelta duration;
664 TimeDelta average_client_interval;
665 Stats stats;
667 void PrintTrialDescription() {
668 double duration_minutes =
669 static_cast<double>(duration.InSeconds()) / 60.0;
670 double interval_minutes =
671 static_cast<double>(average_client_interval.InSeconds()) / 60.0;
672 VerboseOut("Trial with %.2f min downtime, avg. interval %.2f min.\n",
673 duration_minutes, interval_minutes);
677 // We don't set or check expected ratio intervals on individual
678 // experiments as this might make the test too fragile, but we
679 // print them out at the end for manual evaluation (we want to be
680 // able to make claims about the expected ratios depending on the
681 // type of behavior of the client and the downtime, e.g. the difference
682 // in behavior between a client making requests every few minutes vs.
683 // one that makes a request every 15 seconds).
684 Trial trials[] = {
685 { TimeDelta::FromSeconds(10), TimeDelta::FromSeconds(3) },
686 { TimeDelta::FromSeconds(30), TimeDelta::FromSeconds(7) },
687 { TimeDelta::FromMinutes(5), TimeDelta::FromSeconds(30) },
688 { TimeDelta::FromMinutes(10), TimeDelta::FromSeconds(20) },
689 { TimeDelta::FromMinutes(20), TimeDelta::FromSeconds(15) },
690 { TimeDelta::FromMinutes(20), TimeDelta::FromSeconds(50) },
691 { TimeDelta::FromMinutes(30), TimeDelta::FromMinutes(2) },
692 { TimeDelta::FromMinutes(30), TimeDelta::FromMinutes(5) },
693 { TimeDelta::FromMinutes(40), TimeDelta::FromMinutes(7) },
694 { TimeDelta::FromMinutes(40), TimeDelta::FromMinutes(2) },
695 { TimeDelta::FromMinutes(40), TimeDelta::FromSeconds(15) },
696 { TimeDelta::FromMinutes(60), TimeDelta::FromMinutes(7) },
697 { TimeDelta::FromMinutes(60), TimeDelta::FromMinutes(2) },
698 { TimeDelta::FromMinutes(60), TimeDelta::FromSeconds(15) },
699 { TimeDelta::FromMinutes(80), TimeDelta::FromMinutes(20) },
700 { TimeDelta::FromMinutes(80), TimeDelta::FromMinutes(3) },
701 { TimeDelta::FromMinutes(80), TimeDelta::FromSeconds(15) },
703 // Most brutal?
704 { TimeDelta::FromMinutes(45), TimeDelta::FromMilliseconds(500) },
707 // If things don't converge by the time we've done 100K trials, then
708 // clearly one or more of the expected intervals are wrong.
709 while (global_stats.num_runs < 100000) {
710 for (size_t i = 0; i < arraysize(trials); ++i) {
711 ++global_stats.num_runs;
712 ++trials[i].stats.num_runs;
713 double ratio_unprotected = SimulateDowntime(
714 trials[i].duration, trials[i].average_client_interval, false);
715 double ratio_protected = SimulateDowntime(
716 trials[i].duration, trials[i].average_client_interval, true);
717 global_stats.total_ratio_unprotected += ratio_unprotected;
718 global_stats.total_ratio_protected += ratio_protected;
719 trials[i].stats.total_ratio_unprotected += ratio_unprotected;
720 trials[i].stats.total_ratio_protected += ratio_protected;
723 double increase_ratio;
724 if (global_stats.DidConverge(&increase_ratio))
725 break;
727 if (global_stats.num_runs > 200) {
728 VerboseOut("Test has not yet converged on expected interval.\n");
729 global_stats.ReportTrialResult(increase_ratio);
733 double average_increase_ratio;
734 EXPECT_TRUE(global_stats.DidConverge(&average_increase_ratio));
736 // Print individual trial results for optional manual evaluation.
737 double max_increase_ratio = 0.0;
738 for (size_t i = 0; i < arraysize(trials); ++i) {
739 double increase_ratio;
740 trials[i].stats.DidConverge(&increase_ratio);
741 max_increase_ratio = std::max(max_increase_ratio, increase_ratio);
742 trials[i].PrintTrialDescription();
743 trials[i].stats.ReportTrialResult(increase_ratio);
746 VerboseOut("Average increase ratio was %.4f\n", average_increase_ratio);
747 VerboseOut("Maximum increase ratio was %.4f\n", max_increase_ratio);
750 } // namespace
751 } // namespace net