Example #1
0
        public void Concurrency()
        {
            using (ISession s = OpenSession())
            {
                ITransaction tx = s.BeginTransaction();
                for (int index = 0; index < 500; index++) // Note: Was 5000
                {
                    Detective detective = new Detective();
                    detective.Name  = "John Doe " + index;
                    detective.Badge = "123455" + index;
                    detective.PhysicalDescription = "Blond green eye etc etc";
                    s.Save(detective);
                    Suspect suspect = new Suspect();
                    suspect.Name = "Jane Doe " + index;
                    suspect.PhysicalDescription = "brunette, short, 30-ish";
                    if (index % 20 == 0)
                    {
                        suspect.SuspectCharge = "thief liar ";
                    }
                    else
                    {
                        suspect.SuspectCharge = " It's 1875 in London. The police have captured career criminal Montmorency. In the process he has been grievously wounded and it is up to a young surgeon to treat his wounds. During his recovery Montmorency learns of the city's new sewer system and sees in it the perfect underground highway for his thievery.  Washington Post columnist John Kelly recommends this title for middle schoolers, especially to be read aloud.";
                    }
                    s.Save(suspect);
                }
                tx.Commit();
            }
            Thread.Sleep(1000);

            const int nThreads = 15; // Fixed number of threads
            int       workerThreads, completionPortThreads;

            ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
            ThreadPool.SetMinThreads(nThreads, completionPortThreads);
            ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
            ThreadPool.SetMaxThreads(nThreads, completionPortThreads);
            int workerThreadsAvailable;

            ThreadPool.GetAvailableThreads(out workerThreadsAvailable, out completionPortThreads);

            long      start     = DateTime.Now.Ticks;
            const int iteration = 20; // Note: Was 100

            for (int i = 0; i < iteration; i++)
            {
                ThreadPool.QueueUserWorkItem(Work);
                ThreadPool.QueueUserWorkItem(ReverseWork);
            }

            do
            {
                ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
                Thread.Sleep(20); // Wait that all the threads have been terminated before, otherwise, they will be aborted
            }while (workerThreads != workerThreadsAvailable ||
                    worksCount < iteration || reverseWorksCount < iteration);

            System.Diagnostics.Debug.WriteLine(GetType().Name + ": " + iteration + " iterations in " + nThreads
                                               + " threads: " + new TimeSpan(DateTime.Now.Ticks - start).TotalSeconds + " secs; errorsCount = " + errorsCount);

            // Clean up
            using (ISession s = OpenSession())
            {
                ITransaction tx = s.BeginTransaction();
                s.Delete("from System.Object");
                tx.Commit();
            }

            Assert.AreEqual(0, errorsCount, "Some iterations failed");
        }
        public void Concurrency()
        {
            using (ISession s = OpenSession())
            {
                ITransaction tx = s.BeginTransaction();
                for (int index = 0; index < 500; index++) // Note: Was 5000
                {
                    Detective detective = new Detective();
                    detective.Name = "John Doe " + index;
                    detective.Badge = "123455" + index;
                    detective.PhysicalDescription = "Blond green eye etc etc";
                    s.Save(detective);
                    Suspect suspect = new Suspect();
                    suspect.Name = "Jane Doe " + index;
                    suspect.PhysicalDescription = "brunette, short, 30-ish";
                    if (index % 20 == 0)
                        suspect.SuspectCharge = "thief liar ";
                    else
                        suspect.SuspectCharge = " It's 1875 in London. The police have captured career criminal Montmorency. In the process he has been grievously wounded and it is up to a young surgeon to treat his wounds. During his recovery Montmorency learns of the city's new sewer system and sees in it the perfect underground highway for his thievery.  Washington Post columnist John Kelly recommends this title for middle schoolers, especially to be read aloud.";
                    s.Save(suspect);
                }
                tx.Commit();
            }
            Thread.Sleep(1000);

            const int nThreads = 15; // Fixed number of threads
            int workerThreads, completionPortThreads;
            ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
            ThreadPool.SetMinThreads(nThreads, completionPortThreads);
            ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
            ThreadPool.SetMaxThreads(nThreads, completionPortThreads);
            int workerThreadsAvailable;
            ThreadPool.GetAvailableThreads(out workerThreadsAvailable, out completionPortThreads);

            long start = DateTime.Now.Ticks;
            const int iteration = 20; // Note: Was 100
            for (int i = 0; i < iteration; i++)
            {
                ThreadPool.QueueUserWorkItem(Work);
                ThreadPool.QueueUserWorkItem(ReverseWork);
            }

            do
            {
                ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
                Thread.Sleep(20); // Wait that all the threads have been terminated before, otherwise, they will be aborted
            }
            while (workerThreads != workerThreadsAvailable
                || worksCount < iteration || reverseWorksCount < iteration);

            System.Diagnostics.Debug.WriteLine(iteration + " iterations in " + nThreads
                + " threads: " + new TimeSpan(DateTime.Now.Ticks - start).TotalSeconds + " secs; errorsCount = " + errorsCount);

            // Clean up
            using (ISession s = OpenSession())
            {
                ITransaction tx = s.BeginTransaction();
                s.Delete("from System.Object");
                tx.Commit();
            }

            Assert.AreEqual(0, errorsCount, "Some iterations failed");
        }