2006-04-25 Hendrik Brandt <heb@gnome-de.org>
[beagle.git] / bludgeon / SanityCheck.cs
blobdfec0c7735003e1add7a1cf2dc749d9932cb8747
2 using System;
3 using System.Collections;
4 using System.Threading;
6 using Beagle.Util;
7 using Beagle;
9 namespace Bludgeon {
11 public class SanityCheck {
13 const double default_minutes_to_run = 1.0;
15 private SanityCheck () { }
17 static public bool CheckQuery (Query query, FileSystemObject root)
19 // Find the set of objects that we expect to match the query,
20 // based on our knowledge of the current state of the tree.
21 ICollection matching_fsos;
22 matching_fsos = root.RecursiveQuery (query);
24 // Query the daemon and get the actual list of hits.
25 Hashtable matching_hits;
26 matching_hits = QueryFu.GetHits (query);
28 bool success;
29 success = true;
31 foreach (FileSystemObject fso in matching_fsos) {
32 if (matching_hits.Contains (fso.Uri))
33 matching_hits.Remove (fso.Uri);
34 else {
35 Log.Failure ("Hit missing from beagled query results: {0}", fso.Uri);
36 success = false;
40 foreach (Hit hit in matching_hits.Values) {
41 Log.Failure ("Unexpected extra hit in beagled query results: {0}", hit.Uri);
42 Log.Failure (" Properties:");
43 foreach (Property prop in hit.Properties)
44 Log.Failure (" {0} = {1}", prop.Key, prop.Value);
45 success = false;
48 return success;
51 static public bool VerifyIndex (FileSystemObject root)
53 bool success;
54 success = true;
56 Log.Info ("Verifying index for root {0}", root.FullName);
58 for (int i = 0; i < Token.Count; ++i) {
59 Query query;
60 query = QueryFu.NewTokenQuery (i);
62 if (! CheckQuery (query, root)) {
63 Log.Spew ("Failed query is:");
64 QueryFu.SpewQuery (query);
65 success = false;
69 if (success)
70 Log.Info ("Index successfully verified");
71 else
72 Log.Info ("Verification failed");
74 return success;
77 static public bool TestRandomQueries (double minutes_to_run, FileSystemObject root)
79 if (minutes_to_run < 0)
80 minutes_to_run = default_minutes_to_run;
82 Log.Info ("Running random queries for {0:0.0} minutes", minutes_to_run);
84 bool success;
85 success = true;
87 Stopwatch sw;
88 sw = new Stopwatch ();
89 sw.Start ();
91 int count = 0;
92 while (true) {
93 if ((count % 100 == 0) && sw.ElapsedTime > minutes_to_run * 60)
94 break;
95 ++count;
97 if (count % 1000 == 0)
98 Log.Spew ("{0} queries run", count);
100 Beagle.Query query;
101 query = QueryFu.NewRandomQuery ();
103 if (! CheckQuery (query, root)) {
104 Log.Spew ("Failed query is:");
105 QueryFu.SpewQuery (query);
106 success = false;
107 break;
111 // In case we ended early
112 minutes_to_run = sw.ElapsedTime / 60;
114 Log.Spew ("Ran {0} queries in {1:0.0} minutes ({2:0.0} queries/s)",
115 count, minutes_to_run, count / (minutes_to_run * 60));
117 return success;