r9087 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r9086‎ | r9087 | r9088 >
Date:11:25, 17 May 2005
Author:vibber
Status:old
Tags:
Comment:
Support loading the search term set from a file. Don't kill the benchmark if the server returns a 500 error.
Modified paths:
  • /trunk/mwsearch/Benchmark/Benchmark.cs (modified) (history)
  • /trunk/mwsearch/Benchmark/SampleTerms.cs (modified) (history)

Diff [purge]

Index: trunk/mwsearch/Benchmark/SampleTerms.cs
@@ -1,7 +1,16 @@
22 // created on 4/8/2005 at 5:22 PM
33
 4+using System;
 5+using System.IO;
 6+
47 namespace MediaWiki.Search.Benchmark {
5 - class SampleTerms {
 8+ interface ITermSet {
 9+ string Next {
 10+ get;
 11+ }
 12+ }
 13+
 14+ class SampleTerms : ITermSet {
615 private static string[] terms = {
716 "1406",
817 "new york",
@@ -952,11 +961,12 @@
953962 "volcom",
954963 "Charlotte ross"
955964 };
956 - private static int position = 0;
 965+ private int position = 0;
 966+ private readonly object locker = new object();
957967
958 - public static string Next {
 968+ public string Next {
959969 get {
960 - lock(terms) {
 970+ lock(locker) {
961971 string next = terms[position];
962972 if (++position >= terms.Length)
963973 position = 0;
@@ -965,4 +975,28 @@
966976 }
967977 }
968978 }
 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+
9691003 }
Index: trunk/mwsearch/Benchmark/Benchmark.cs
@@ -43,6 +43,7 @@
4444 string verb = "search";
4545 int runs = 20;
4646 int threads = 1;
 47+ ITermSet termset = new SampleTerms();
4748
4849 for(int i = 0; i < args.Length; i++) {
4950 if (args[i].Equals("--host")) {
@@ -57,10 +58,12 @@
5859 runs = int.Parse(args[++i]);
5960 } else if (args[i].Equals("--verb")) {
6061 verb = args[++i];
 62+ } else if (args[i].Equals("--terms")) {
 63+ termset = new FileTermSet(args[++i]);
6164 }
6265 }
6366
64 - Benchmark bench = new Benchmark(host, port, database, verb);
 67+ Benchmark bench = new Benchmark(host, port, database, verb, termset);
6568 bench.RunSets(runs, threads);
6669 bench.Report();
6770 }
@@ -70,6 +73,7 @@
7174 private string database;
7275 private string verb;
7376 private int runs;
 77+ private ITermSet termset;
7478
7579 /* Access these only in lock(times){} */
7680 private ArrayList times;
@@ -81,12 +85,13 @@
8286 private int runningThreads = 0;
8387 private readonly object threadlock = new object();
8488
85 - private Benchmark(string host, ushort port, string database, string verb) {
 89+ private Benchmark(string host, ushort port, string database, string verb, ITermSet termset) {
8690 this.host = host;
8791 this.port = port;
8892 this.database = database;
8993 this.verb = verb;
9094 this.times = new ArrayList();
 95+ this.termset = termset;
9196 }
9297
9398 private void RunSets(int runs, int threads) {
@@ -111,7 +116,7 @@
112117 Console.Out.Flush();
113118 try {
114119 for (int i = 0; i < runs; i++) {
115 - Search(SampleTerms.Next);
 120+ Search(termset.Next);
116121 }
117122 } finally {
118123 lock (threadlock) {
@@ -126,29 +131,35 @@
127132
128133 string encterm = HttpUtility.UrlEncode(term, Encoding.UTF8);
129134 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();
139135
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());
153164 }
154165 }
155166

Status & tagging log