// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Net; using System.Threading; using System.Text.RegularExpressions; namespace E { public abstract class Test { private const int NPIC = 1000; private const int PICSIZ = 25000; private Random rng = new Random(1234567); private int picforthis; public void DoTest() { for(int i = 0; i < picforthis; i++) { int id = rng.Next(NPIC); WebRequest wr = WebRequest.Create(StartUrl + id); string html = (new StreamReader(wr.GetResponse().GetResponseStream())).ReadToEnd(); string url = Regex.Matches(html, "(?:src\\s*=\\s*\")([^\"]*)(?:\")")[0].Groups[1].Value; WebRequest req = WebRequest.Create(url); WebResponse resp = req.GetResponse(); Stream stm = resp.GetResponseStream(); byte[] b = new byte[resp.ContentLength]; int ix = 0; int n; while((n = stm.Read(b, ix, b.Length - ix)) > 0) { ix += n; } stm.Close(); if(b.Length != PICSIZ) { Console.WriteLine("Error"); Environment.Exit(0); } for(int j = 0; j < b.Length; j++) { if(b[j] != (byte)(id % 256)) { Console.WriteLine("Error"); Environment.Exit(0); } } } } public int PicForThis { set { picforthis = value; } } protected abstract string StartUrl { get; } } public class FileTest : Test { protected override string StartUrl { get { return "http://localhost/getfile.aspx?id="; } } } public class FileCacheTest : Test { protected override string StartUrl { get { return "http://localhost/getfilec.aspx?id="; } } } public class FileDirectTest : Test { protected override string StartUrl { get { return "http://localhost/getfiled.aspx?id="; } } } public class DbTest : Test { protected override string StartUrl { get { return "http://localhost/getdb.aspx?id="; } } } public class DbCacheTest : Test { protected override string StartUrl { get { return "http://localhost/getdbc.aspx?id="; } } } public class MainClass { private const int NREP = 3; private const int NGET = 5000; public static void TestThreads(int nthreads, Test o, string label) { o.PicForThis = NGET / nthreads; Thread[] thr = new Thread[nthreads]; for(int i = 0; i < thr.Length; i++) { thr[i] = new Thread(new ThreadStart(o.DoTest)); } long t1 = DateTime.Now.Ticks; for(int i = 0; i < thr.Length; i++) { thr[i].Start(); } for(int i = 0; i < thr.Length; i++) { thr[i].Join(); } long t2 = DateTime.Now.Ticks; Console.WriteLine(label + " (" + nthreads + " threads): " + ((10*NGET) / ((t2 - t1)/1000000)) / 10.0 + " get per second"); } public static void Main(string[] args) { for(int i = 0; i < NREP; i++) { TestThreads(1, new FileTest(), "File"); TestThreads(10, new FileTest(), "File"); TestThreads(1, new FileCacheTest(), "File with web app cache"); TestThreads(10, new FileCacheTest(), "File with web app cache"); TestThreads(1, new FileDirectTest(), "File directly by web server"); TestThreads(10, new FileDirectTest(), "File directly by web server"); TestThreads(1, new DbTest(), "Database"); TestThreads(10, new DbTest(), "Database"); TestThreads(1, new DbCacheTest(), "Database with web app cache"); TestThreads(10, new DbCacheTest(), "Database with web app cache"); } } } }