Esempio n. 1
0
        public void FindMaxTest()
        {
            Assert.AreEqual(0, queue.Count);
            Assert.Throws <NoSuchItemException>(() => queue.FindMax());
            Random rng = new Random();

            if (prefill)
            {
                for (int i = 0; i < n; i++)
                {
                    queue.Add(rng.Next(1000));
                }
            }
            Thread[] threads = new Thread[threadCount];
            for (int i = 0; i < threadCount; i++)
            {
                Thread t = new Thread(() =>
                {
                    for (int y = 0; y < n; y++)
                    {
                        queue.FindMax();
                    }
                });
                threads[i] = t;
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Start();
            }
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }
            Assert.IsTrue(queue.Check());
            Assert.AreEqual((prefill ? n : 0), queue.Count);
        }
Esempio n. 2
0
        /// <summary>
        /// Main benchmarking method
        /// </summary>
        /// <param name="config"></param>
        /// <param name="type"></param>
        void BenchMark(BenchmarkConfig config, Type type)
        {
            var SecondsPerTestTimer = new Stopwatch();

            //Run the benchmark for all the number of threads specified
            foreach (int threadsToRun in config.Threads)
            {
                SecondsPerTestTimer.Reset();
                var   tempThroughPut = new ArrayList <Int64>();
                Int64 throughput     = 0;
                int   runs           = 0;

                //Inner loop that runs until standard deviation is below some threshold or it has done too many runs and throws an exception
                while ((SecondsPerTestTimer.ElapsedMilliseconds / 1000.0) < ((config.SecondsPerTest * 1.0) / config.Threads[config.Threads.Length - 1]) || runs <= ((config.WarmupRuns) + config.Threads[0]))
                {
                    if (type.Equals(typeof(SprayList <int>)))
                    {
                        dictionary = (IConcurrentPriorityQueue <int>)Activator.CreateInstance(type, threadsToRun);
                    }
                    else
                    {
                        dictionary = (IConcurrentPriorityQueue <int>)Activator.CreateInstance(type);
                    }
                    //dictionary = new GlobalLockDEPQ<int>(); // Create the correct dictionary for this run

                    // Get tree to correct size before we start, if applicable.
                    if (config.Prefill)
                    {
                        /* Each trial should start with trees that are prefilled to their expected size in the steady state,
                         * so that you are measuring steady state performance, and not inconsistent performance as the size
                         * of the tree is changing. If your experiment consists of random operations in the proportions
                         * i% insertions, d% deletions, and s% searches on keys drawn uniformly randomly from a key range of
                         * size r, then the expected size of the tree in the steady state will be ri/(i+d)
                         */
                        int r = (config.EndRangeRandom - config.StartRangeRandom) * 4; // double the range
                        int steadyStateSize = config.CurrentNumberOfElements / 2;

                        if (config.CurrentPercentageInsert > 0 && config.CurrentPercentageDeleteMin > 0 && config.CurrentPercentageDeleteMax > 0)
                        {
                            steadyStateSize = (r * config.CurrentPercentageInsert) / (config.CurrentPercentageInsert + config.CurrentPercentageDeleteMin + config.CurrentPercentageDeleteMax);
                        }

                        if (steadyStateSize > r)
                        {
                            throw new Exception("Range of numbers is too small to reach steady state");
                        }

                        Queue <int> threadQueue = generateRandomQueue(steadyStateSize, config.StartRangeRandom, config.EndRangeRandom); //double the size
                        while (threadQueue.Count > 0)
                        {
                            int element = threadQueue.Dequeue();
                            dictionary.Add(element);
                        }
                    }

                    // Create the start and end barrier
                    barrier = new Barrier(threadsToRun + 1);

                    // Submit the work
                    for (int threads = 1; threads <= threadsToRun; threads++)
                    {
                        int         start       = config.StartRangeRandom;
                        int         end         = config.EndRangeRandom;
                        Queue <int> threadQueue = generateRandomQueue(config.CurrentNumberOfElements, start, end);
                        Thread      thread      = new Thread(Work);
                        thread.Start(threadQueue);
                    }

                    barrier.SignalAndWait();// Wait for all tasks / threads to be ready to begin work


                    var t = new Stopwatch(); // Start the timers
                    t.Start();
                    SecondsPerTestTimer.Start();

                    barrier.SignalAndWait();      // Wait for all tasks / threads to be finished with their work. Unlike Java, no need to reset the barrier in C#

                    double time = t.ElapsedTicks; // Get elapsed time
                    config.ExecutionTime = t.ElapsedMilliseconds;
                    SecondsPerTestTimer.Stop();

                    if (runs > config.WarmupRuns) // Only add results after the warmup runs
                    {
                        Int64 toAdd = (Int64)(((config.CurrentNumberOfElements * threadsToRun) / time) * 1000.0 * 10000.0);
                        tempThroughPut.Add(toAdd);
                    }

                    runs++;// Increment number of runs
                }

                throughput = (Int64)tempThroughPut.Average();

                if (throughput > maxThroughput)
                {
                    maxThroughput = throughput;
                }
                //add a line with threads and execution time and a line with threads and throughput
                //datafile.Log(threadsToRun + " " + config.ExecutionTime);
                datafile.Log(threadsToRun + " " + throughput);
            }
        }
Esempio n. 3
0
 public void IsEmptyTest()
 {
     Assert.IsTrue(queue.IsEmpty());
     queue.Add(1);
     Assert.IsFalse(queue.IsEmpty());
 }