// 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.Threading; using System.Data; using ByteFX.Data.MySqlClient; namespace Test { public class PicTestThread { private int n; private int npics; private int picsize; public PicTestThread(int n, int npics, int picsize) { this.n = n; this.npics = npics; this.picsize = picsize; } public void Run() { PicTest.CheckN(n, npics, picsize); } } public class PicTest { private const int N = 10000; public static void CreateTable(int npics, int picsize) { MySqlConnection con = new MySqlConnection("Database=Test;Data Source=localhost;User Id=;Password="); con.Open(); MySqlCommand cre = new MySqlCommand("CREATE TABLE pics (id INTEGER PRIMARY KEY, pic MEDIUMBLOB)", con); cre.ExecuteNonQuery(); MySqlCommand ins = new MySqlCommand("INSERT INTO pics VALUES (@id, @pic)", con); ins.Parameters.Add("@id", MySqlDbType.Int); ins.Parameters.Add("@pic", MySqlDbType.MediumBlob); for(int i = 0; i < npics; i++) { ins.Parameters["@id"].Value = i; byte[] data = new Byte[picsize]; for(int j = 0; j < picsize; j++) { data[j] = (byte)((i + j) % 256); } ins.Parameters["@pic"].Value = data; ins.ExecuteNonQuery(); } con.Close(); } public static void CheckOne(int id, int picsize) { MySqlConnection con = new MySqlConnection("Database=Test;Data Source=localhost;User Id=;Password="); con.Open(); MySqlCommand sel = new MySqlCommand("SELECT pic FROM pics WHERE id = " + id, con); byte[] data = (byte[])sel.ExecuteScalar(); if(data.Length != picsize) { Console.WriteLine("picture #" + id + " corrupted"); } for(int j = 0; j < data.Length; j++) { if(((id + j) % 256) != data[j]) { Console.WriteLine("picture #" + id + " corrupted"); } } con.Close(); } public static void DropTable() { MySqlConnection con = new MySqlConnection("Database=Test;Data Source=localhost;User Id=;Password="); con.Open(); MySqlCommand drp = new MySqlCommand("DROP TABLE pics", con); drp.ExecuteNonQuery(); con.Close(); } public static void CheckN(int n, int npics, int picsize) { Random rng = new Random(); for(int i = 0; i < n; i++) { int id = rng.Next(npics); CheckOne(id, picsize); } } public static string CheckTest(int nthreads, int npics, int picsize) { long t1 = DateTime.Now.Ticks; PicTestThread[] ptt = new PicTestThread[nthreads]; Thread[] thr = new Thread[nthreads]; for(int i = 0; i < thr.Length; i++) { ptt[i] = new PicTestThread(N/nthreads, npics, picsize); thr[i] = new Thread(new ThreadStart(ptt[i].Run)); } 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; return (nthreads + " conc reqs, " + npics + " pics in db, " + picsize/1000 + " KB pics => " + N/((t2 - t1) / 1000000.0) + " pics/sec"); } } }