Index: trunk/mwsearch/Benchmark/SampleTerms.cs |
— | — | @@ -1,7 +1,16 @@ |
2 | 2 | // created on 4/8/2005 at 5:22 PM |
3 | 3 | |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | + |
4 | 7 | namespace MediaWiki.Search.Benchmark { |
5 | | - class SampleTerms { |
| 8 | + interface ITermSet { |
| 9 | + string Next { |
| 10 | + get; |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + class SampleTerms : ITermSet { |
6 | 15 | private static string[] terms = { |
7 | 16 | "1406", |
8 | 17 | "new york", |
— | — | @@ -952,11 +961,12 @@ |
953 | 962 | "volcom", |
954 | 963 | "Charlotte ross" |
955 | 964 | }; |
956 | | - private static int position = 0; |
| 965 | + private int position = 0; |
| 966 | + private readonly object locker = new object(); |
957 | 967 | |
958 | | - public static string Next { |
| 968 | + public string Next { |
959 | 969 | get { |
960 | | - lock(terms) { |
| 970 | + lock(locker) { |
961 | 971 | string next = terms[position]; |
962 | 972 | if (++position >= terms.Length) |
963 | 973 | position = 0; |
— | — | @@ -965,4 +975,28 @@ |
966 | 976 | } |
967 | 977 | } |
968 | 978 | } |
| 979 | + |
| 980 | + class FileTermSet : ITermSet { |
| 981 | + string filename; |
| 982 | + TextReader reader; |
| 983 | + |
| 984 | + public FileTermSet(string filename) { |
| 985 | + this.filename = filename; |
| 986 | + reader = File.OpenText(filename); |
| 987 | + } |
| 988 | + |
| 989 | + public string Next { |
| 990 | + get { |
| 991 | + lock(reader) { |
| 992 | + string next = reader.ReadLine(); |
| 993 | + if (next == null) { |
| 994 | + reader.Close(); |
| 995 | + reader = File.OpenText(filename); |
| 996 | + } |
| 997 | + return next; |
| 998 | + } |
| 999 | + } |
| 1000 | + } |
| 1001 | + } |
| 1002 | + |
969 | 1003 | } |
Index: trunk/mwsearch/Benchmark/Benchmark.cs |
— | — | @@ -43,6 +43,7 @@ |
44 | 44 | string verb = "search"; |
45 | 45 | int runs = 20; |
46 | 46 | int threads = 1; |
| 47 | + ITermSet termset = new SampleTerms(); |
47 | 48 | |
48 | 49 | for(int i = 0; i < args.Length; i++) { |
49 | 50 | if (args[i].Equals("--host")) { |
— | — | @@ -57,10 +58,12 @@ |
58 | 59 | runs = int.Parse(args[++i]); |
59 | 60 | } else if (args[i].Equals("--verb")) { |
60 | 61 | verb = args[++i]; |
| 62 | + } else if (args[i].Equals("--terms")) { |
| 63 | + termset = new FileTermSet(args[++i]); |
61 | 64 | } |
62 | 65 | } |
63 | 66 | |
64 | | - Benchmark bench = new Benchmark(host, port, database, verb); |
| 67 | + Benchmark bench = new Benchmark(host, port, database, verb, termset); |
65 | 68 | bench.RunSets(runs, threads); |
66 | 69 | bench.Report(); |
67 | 70 | } |
— | — | @@ -70,6 +73,7 @@ |
71 | 74 | private string database; |
72 | 75 | private string verb; |
73 | 76 | private int runs; |
| 77 | + private ITermSet termset; |
74 | 78 | |
75 | 79 | /* Access these only in lock(times){} */ |
76 | 80 | private ArrayList times; |
— | — | @@ -81,12 +85,13 @@ |
82 | 86 | private int runningThreads = 0; |
83 | 87 | private readonly object threadlock = new object(); |
84 | 88 | |
85 | | - private Benchmark(string host, ushort port, string database, string verb) { |
| 89 | + private Benchmark(string host, ushort port, string database, string verb, ITermSet termset) { |
86 | 90 | this.host = host; |
87 | 91 | this.port = port; |
88 | 92 | this.database = database; |
89 | 93 | this.verb = verb; |
90 | 94 | this.times = new ArrayList(); |
| 95 | + this.termset = termset; |
91 | 96 | } |
92 | 97 | |
93 | 98 | private void RunSets(int runs, int threads) { |
— | — | @@ -111,7 +116,7 @@ |
112 | 117 | Console.Out.Flush(); |
113 | 118 | try { |
114 | 119 | for (int i = 0; i < runs; i++) { |
115 | | - Search(SampleTerms.Next); |
| 120 | + Search(termset.Next); |
116 | 121 | } |
117 | 122 | } finally { |
118 | 123 | lock (threadlock) { |
— | — | @@ -126,29 +131,35 @@ |
127 | 132 | |
128 | 133 | string encterm = HttpUtility.UrlEncode(term, Encoding.UTF8); |
129 | 134 | string req = "http://" + host + ":" + port + "/" + verb + "/" + database + "/" + encterm; |
130 | | - WebRequest web = WebRequest.Create(req); |
131 | | - web.Timeout = 1000 * 300; // profiling mode on mono is really slow |
132 | | - WebResponse response = web.GetResponse(); |
133 | | - StreamReader reader = new StreamReader(response.GetResponseStream()); |
134 | | - |
135 | | - string numResults = reader.ReadLine(); |
136 | | - string remainder = reader.ReadToEnd(); |
137 | | - reader.Close(); |
138 | | - response.Close(); |
139 | 135 | |
140 | | - string[] lines = remainder.Split('\n'); // last is empty, as \n is terminator |
141 | | - int numReceived = lines.Length - 1; |
142 | | - |
143 | | - TimeSpan delta = DateTime.UtcNow - start; |
144 | | - Console.WriteLine("[{0}] '{1}' received {2} of {3} lines ({4} chars) in {5}.", |
145 | | - Thread.CurrentThread.GetHashCode(), |
146 | | - encterm, numReceived, numResults, remainder.Length, delta); |
147 | | - |
148 | | - lock(times) { |
149 | | - times.Add(delta); |
150 | | - totalTime += delta; |
151 | | - totalResults += numReceived; |
152 | | - ++totalRequests; |
| 136 | + try { |
| 137 | + WebRequest web = WebRequest.Create(req); |
| 138 | + web.Timeout = 1000 * 300; // profiling mode on mono is really slow |
| 139 | + WebResponse response = web.GetResponse(); |
| 140 | + StreamReader reader = new StreamReader(response.GetResponseStream()); |
| 141 | + |
| 142 | + string numResults = reader.ReadLine(); |
| 143 | + string remainder = reader.ReadToEnd(); |
| 144 | + reader.Close(); |
| 145 | + response.Close(); |
| 146 | + |
| 147 | + string[] lines = remainder.Split('\n'); // last is empty, as \n is terminator |
| 148 | + int numReceived = lines.Length - 1; |
| 149 | + |
| 150 | + TimeSpan delta = DateTime.UtcNow - start; |
| 151 | + Console.WriteLine("[{0}] '{1}' received {2} of {3} lines ({4} chars) in {5}.", |
| 152 | + Thread.CurrentThread.GetHashCode(), |
| 153 | + encterm, numReceived, numResults, remainder.Length, delta); |
| 154 | + lock(times) { |
| 155 | + times.Add(delta); |
| 156 | + totalTime += delta; |
| 157 | + totalResults += numReceived; |
| 158 | + ++totalRequests; |
| 159 | + } |
| 160 | + } catch (Exception e) { |
| 161 | + Console.WriteLine("[{0}] '{1}' exploded: {2}", |
| 162 | + Thread.CurrentThread.GetHashCode(), |
| 163 | + encterm, e.ToString()); |
153 | 164 | } |
154 | 165 | } |
155 | 166 | |