Bye-bye googledriver.
[beagle.git] / bludgeon / Abuse.cs
blobf5c35daa292cc8960b0966dbcbbf1dbec45fe1f9
2 using System;
3 using System.Collections;
5 using Beagle.Util;
7 namespace Bludgeon {
9 public class Abuse {
11 DirectoryObject root;
12 EventTracker tracker;
14 IHammer[] hammers;
15 Random random = new Random ();
17 public int TotalCount = -1; // in iterations
18 public double TotalTime = -1; // in minutes
20 public int Cycles = -1;
21 public int MinCycles = 1;
22 public int MaxCycles = -1;
24 // These are delays that are introduced between calls
25 // to HammerOnce. They are measured in seconds.
26 public double Pause = -1;
27 public double MinPause = 0;
28 public double MaxPause = -1;
30 const int default_count = 10;
31 const int default_cycles = 100;
32 const double default_pause = 0;
34 // This is where we track the state of our current cycle
35 // of abuse.
36 int count;
37 int cycles_remaining;
38 DateTime start_time;
40 GLib.IdleHandler idle_handler;
41 GLib.TimeoutHandler timeout_handler;
42 Daemon.VerifiedHandler verified_handler;
44 public Abuse (DirectoryObject root,
45 EventTracker tracker,
46 ICollection hammers)
48 this.root = root;
49 this.tracker = tracker;
51 this.hammers = new IHammer [hammers.Count];
52 int i = 0;
53 foreach (IHammer hammer in hammers)
54 this.hammers [i++] = hammer;
56 idle_handler = new GLib.IdleHandler (AbuseWorker);
57 timeout_handler = new GLib.TimeoutHandler (RescheduleAbuse);
58 verified_handler = new Daemon.VerifiedHandler (VerifiedWorker);
61 public void Run ()
63 count = 0;
64 cycles_remaining = GetCycles ();
65 start_time = DateTime.Now;
67 // We start by verifying the index, to make sure we
68 // are in a reasonable state.
69 Daemon.WaitUntilVerified (root, verified_handler);
72 ///////////////////////////////////////////////////////////////////////
74 private int GetCycles ()
76 if (Cycles > 0)
77 return Cycles;
78 else if (MaxCycles > MinCycles)
79 return MinCycles + random.Next (MaxCycles - MinCycles);
80 return default_cycles;
83 private int GetPauseInMs ()
85 double t = default_pause;
86 if (Pause >= 0)
87 t = Pause;
88 else if (MaxPause > MinPause)
89 t = MinPause + random.NextDouble () * (MaxPause - MinPause);
90 return (int) (1000 * t);
93 private bool AbuseWorker ()
95 // Pick a hammer, and use it.
96 int i;
97 i = random.Next (hammers.Length);
98 if (! hammers [i].HammerOnce (root, tracker))
99 return false;
101 --cycles_remaining;
102 if (cycles_remaining == 0) {
103 cycles_remaining = GetCycles ();
104 ++count;
106 // Verify the index
107 Daemon.WaitUntilVerified (root, verified_handler);
108 return false;
111 return true;
114 private bool RescheduleAbuse ()
116 Action.Add (idle_handler);
118 return false;
121 private void VerifiedWorker (bool index_is_sane)
123 // If the index is bad, just return. The index-checking
124 // code will generate spew to tell us what went wrong, so
125 // we don't need to output anything.
126 if (! index_is_sane)
127 return;
129 // Are we finished yet?
130 bool finished = false;
131 if (hammers.Length == 0) {
132 finished = true;
133 } else if (TotalTime > 0) {
134 double t;
135 t = (DateTime.Now - start_time).TotalSeconds;
136 finished = (t > 60*TotalTime);
137 } else {
138 int target_count;
139 target_count = TotalCount;
140 if (target_count < 0)
141 target_count = default_count;
142 finished = (count >= target_count);
145 // If we aren't finished, schedule some more abuse.
146 if (! finished) {
147 if (count == 0)
148 Action.Add (idle_handler);
149 else
150 Action.Add ((uint) GetPauseInMs (), timeout_handler);