// 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 { class TestClass { private const int NPIC = 1000; private const int PICSIZ = 25000; private const int NGET = 10000; private static Random rng; private static int getprthread; private static void TestFileThread() { for(int i = 0; i < getprthread; i++) { int id = rng.Next(NPIC); WebRequest wr = WebRequest.Create("http://localhost/getfile.aspx?id=" + 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"); } for(int j = 0; j < b.Length; j++) { if(b[j] != (byte)(id % 256)) { Console.WriteLine("Error"); } } } } public static void TestFile(int nthreads) { rng = new Random(1234567); getprthread = NGET / nthreads; Thread[] thr = new Thread[nthreads]; for(int i = 0; i < thr.Length; i++) { thr[i] = new Thread(new ThreadStart(TestFileThread)); } 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("Picture file served by web server (" + nthreads + " threads): " + NGET / ((t2 - t1)/1000000) + " get per second"); } private static void TestDbThread() { for(int i = 0; i < getprthread; i++) { int id = rng.Next(NPIC); WebRequest wr = WebRequest.Create("http://localhost/getdb.aspx?id=" + 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"); } for(int j = 0; j < b.Length; j++) { if(b[j] != (byte)(id % 256)) { Console.WriteLine("Error"); } } } } public static void TestDb(int nthreads) { rng = new Random(1234567); getprthread = NGET / nthreads; Thread[] thr = new Thread[nthreads]; for(int i = 0; i < thr.Length; i++) { thr[i] = new Thread(new ThreadStart(TestDbThread)); } 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("Picture from database via script (" + nthreads + " threads): " + NGET / ((t2 - t1)/1000000) + " get per second"); } public static void Main(string[] args) { TestFile(1); TestFile(10); TestDb(1); TestDb(10); } } }