// 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.Data; using System.Data.SqlClient; using System.IO; using System.Threading; namespace E { public class TestClass { private const int NPIC = 1000; private const int PICSIZ = 25000; private const int NGET = 100000; private const string CONNSTR = "Server=ARNEPC3;Integrated Security=SSPI;Database=Test"; private const string BASEDIR = @"C:\pictest\"; private static Random rng; private static int getprthread; public static void SetupDbAndFile() { SqlConnection con = new SqlConnection(CONNSTR); con.Open(); SqlCommand cre = new SqlCommand("CREATE TABLE daf (id INTEGER PRIMARY KEY, path VARCHAR(255))", con); cre.ExecuteNonQuery(); SqlCommand ins = new SqlCommand("INSERT INTO daf VALUES (@id, @path)", con); ins.Parameters.Add("@id", SqlDbType.Int); ins.Parameters.Add("@path", SqlDbType.VarChar); for(int i = 0; i < NPIC; i++) { byte[] b = new byte[PICSIZ]; for(int j = 0; j < b.Length; j++) b[j] = (byte)(i % 256); string fnm = BASEDIR + i + ".pic"; ins.Parameters["@id"].Value = i; ins.Parameters["@path"].Value = fnm; ins.ExecuteNonQuery(); Stream stm = new FileStream(fnm, FileMode.CreateNew, FileAccess.Write); stm.Write(b, 0, b.Length); stm.Close(); } con.Close(); } private static void TestDbAndFileThread() { for(int i = 0; i < getprthread; i++) { int id = rng.Next(NPIC); SqlConnection con = new SqlConnection(CONNSTR); con.Open(); SqlCommand sel = new SqlCommand("SELECT path FROM daf WHERE id = " + id, con); string fnm = (string)sel.ExecuteScalar(); con.Close(); Stream stm = new FileStream(fnm, FileMode.Open, FileAccess.Read); byte[] b = new byte[stm.Length]; stm.Read(b, 0, b.Length); stm.Close(); if(b.Length != PICSIZ) { Console.WriteLine("Error"); Environment.Exit(1); } for(int j = 0; j < b.Length; j++) { if(b[j] != (byte)(id % 256)) { Console.WriteLine("Error"); Environment.Exit(1); } } } } public static void TestDbAndFile(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(TestDbAndFileThread)); } 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("Database and file (" + nthreads + " threads): " + NGET / ((t2 - t1)/1000000) + " get per second"); } public static void DestroyDbAndFile() { SqlConnection con = new SqlConnection(CONNSTR); con.Open(); SqlCommand drp = new SqlCommand("DROP TABLE daf", con); drp.ExecuteNonQuery(); con.Close(); string[] allf = Directory.GetFiles(BASEDIR); foreach(string f in allf) { File.Delete(f); } } public static void SetupOnlyDb() { SqlConnection con = new SqlConnection(CONNSTR); con.Open(); SqlCommand cre = new SqlCommand("CREATE TABLE od (id INTEGER PRIMARY KEY, pic IMAGE)", con); cre.ExecuteNonQuery(); SqlCommand ins = new SqlCommand("INSERT INTO od VALUES (@id, @pic)", con); ins.Parameters.Add("@id", SqlDbType.Int); ins.Parameters.Add("@pic", SqlDbType.Image); for(int i = 0; i < NPIC; i++) { byte[] b = new byte[PICSIZ]; for(int j = 0; j < b.Length; j++) b[j] = (byte)(i % 256); ins.Parameters["@id"].Value = i; ins.Parameters["@pic"].Value = b; ins.ExecuteNonQuery(); } con.Close(); } private static void TestOnlyDbThread() { for(int i = 0; i < getprthread; i++) { int id = rng.Next(NPIC); SqlConnection con = new SqlConnection(CONNSTR); con.Open(); SqlCommand sel = new SqlCommand("SELECT pic FROM od WHERE id = " + id, con); byte[] b = (byte[])sel.ExecuteScalar(); con.Close(); if(b.Length != PICSIZ) { Console.WriteLine("Error"); Environment.Exit(1); } for(int j = 0; j < b.Length; j++) { if(b[j] != (byte)(id % 256)) { Console.WriteLine("Error"); Environment.Exit(1); } } } } public static void TestOnlyDb(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(TestOnlyDbThread)); } 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("Only database (" + nthreads + " threads): " + NGET / ((t2 - t1)/1000000) + " get per second"); } public static void DestroyOnlyDb() { SqlConnection con = new SqlConnection(CONNSTR); con.Open(); SqlCommand drp = new SqlCommand("DROP TABLE od", con); drp.ExecuteNonQuery(); con.Close(); } public static void Main(string[] args) { SetupDbAndFile(); TestDbAndFile(1); TestDbAndFile(10); DestroyDbAndFile(); SetupOnlyDb(); TestOnlyDb(1); TestOnlyDb(10); DestroyOnlyDb(); } } }