Beispiel #1
0
        private int DoParallelTasks()
        {
            TaskStats stats = RunData.Points.CurrentStats;

            InitTasksArray();
            ParallelTask[] t = runningParallelTasks = new ParallelTask[repetitions * tasks.Count];
            // prepare threads
            int index = 0;

            for (int k = 0; k < repetitions; k++)
            {
                for (int i = 0; i < tasksArray.Length; i++)
                {
                    PerfTask task = (PerfTask)(tasksArray[i].Clone());
                    t[index++] = new ParallelTask(this, task);
                }
            }
            // run threads
            StartThreads(t);

            if (Stop)
            {
                foreach (ParallelTask task in t)
                {
                    task.Task.StopNow();
                }
            }

            // wait for all threads to complete
            int count = 0;

            for (int i = 0; i < t.Length; i++)
            {
                t[i].Join();
                count += t[i].Count;
                if (t[i].Task is TaskSequence sub && sub.countsByTime != null)
                {
                    if (countsByTime == null)
                    {
                        countsByTime = new int[sub.countsByTime.Length];
                    }
                    else if (countsByTime.Length < sub.countsByTime.Length)
                    {
                        countsByTime = ArrayUtil.Grow(countsByTime, sub.countsByTime.Length);
                    }
                    for (int j = 0; j < sub.countsByTime.Length; j++)
                    {
                        countsByTime[j] += sub.countsByTime[j];
                    }
                }
            }

            if (countsByTime != null)
            {
                stats.SetCountsByTime(countsByTime, logByTimeMsec);
            }

            // return total count
            return(count);
        }
        private int DoSerialTasksWithRate()
        {
            InitTasksArray();
            long delayStep = (perMin ? 60000 : 1000) / rate;
            long nextStartTime = J2N.Time.CurrentTimeMilliseconds();
            int count = 0;
            long t0 = J2N.Time.CurrentTimeMilliseconds();
            for (int k = 0; (repetitions == REPEAT_EXHAUST && !exhausted) || k < repetitions; k++)
            {
                if (Stop)
                {
                    break;
                }
                for (int l = 0; l < tasksArray.Length; l++)
                {
                    PerfTask task = tasksArray[l];
                    while (!Stop)
                    {
                        long waitMore = nextStartTime - J2N.Time.CurrentTimeMilliseconds();
                        if (waitMore > 0)
                        {
                            // TODO: better to use condition to notify
                            Thread.Sleep(1);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (Stop)
                    {
                        break;
                    }
                    nextStartTime += delayStep; // this aims at avarage rate.
                    try
                    {
                        int inc = task.RunAndMaybeStats(letChildReport);
                        count += inc;
                        if (countsByTime != null)
                        {
                            int slot = (int)((J2N.Time.CurrentTimeMilliseconds() - t0) / logByTimeMsec);
                            if (slot >= countsByTime.Length)
                            {
                                countsByTime = ArrayUtil.Grow(countsByTime, 1 + slot);
                            }
                            countsByTime[slot] += inc;
                        }

                        if (anyExhaustibleTasks)
                            UpdateExhausted(task);
                    }
                    catch (NoMoreDataException /*e*/)
                    {
                        exhausted = true;
                    }
                }
            }
            Stop = false;
            return count;
        }
Beispiel #3
0
        /// <summary>
        /// Compute a report line for the given task stat.
        /// </summary>
        /// <param name="longestOp">Size of longest op name in the table.</param>
        /// <param name="stat">Task stat to be printed.</param>
        /// <returns>The report line.</returns>
        protected virtual string TaskReportLine(string longestOp, TaskStats stat)
        {
            PerfTask      task = stat.Task;
            StringBuilder sb   = new StringBuilder();

            sb.Append(Formatter.Format(task.GetName(), longestOp));
            string round = (stat.Round >= 0 ? "" + stat.Round : "-");

            sb.Append(Formatter.FormatPaddLeft(round, ROUND));
            sb.Append(RunData.Config.GetColsValuesForValsByRound(stat.Round));
            sb.Append(Formatter.Format(stat.NumRuns, RUNCNT));
            sb.Append(Formatter.Format(stat.Count / stat.NumRuns, RECCNT));
            long elapsed = (stat.Elapsed > 0 ? stat.Elapsed : 1); // assume at least 1ms

            sb.Append(Formatter.Format(2, (float)(stat.Count * 1000.0 / elapsed), RECSEC));
            sb.Append(Formatter.Format(2, (float)stat.Elapsed / 1000, ELAPSED));
            sb.Append(Formatter.Format(0, (float)stat.MaxUsedMem / stat.NumRuns, USEDMEM));
            sb.Append(Formatter.Format(0, (float)stat.MaxTotMem / stat.NumRuns, TOTMEM));
            return(sb.ToString());
        }
Beispiel #4
0
 // update state regarding exhaustion.
 private void UpdateExhausted(PerfTask task)
 {
     if (task is ResetInputsTask)
     {
         exhausted      = false;
         resetExhausted = true;
     }
     else if (task is TaskSequence t)
     {
         if (t.resetExhausted)
         {
             exhausted        = false;
             resetExhausted   = true;
             t.resetExhausted = false;
         }
         else
         {
             exhausted |= t.exhausted;
         }
     }
 }
Beispiel #5
0
 public virtual void AddTask(PerfTask task)
 {
     tasks.Add(task);
     task.Depth = Depth + 1;
 }
Beispiel #6
0
 public ParallelTask(TaskSequence outerInstance, PerfTask task)
 {
     this.outerInstance = outerInstance;
     this.task          = task;
 }
Beispiel #7
0
        private int DoSerialTasks()
        {
            if (rate > 0)
            {
                return(DoSerialTasksWithRate());
            }

            InitTasksArray();
            int count = 0;

            long runTime = (long)(runTimeSec * 1000);
            List <RunBackgroundTask> bgTasks = null;

            long t0 = Support.Time.CurrentTimeMilliseconds();

            for (int k = 0; fixedTime || (repetitions == REPEAT_EXHAUST && !exhausted) || k < repetitions; k++)
            {
                if (Stop)
                {
                    break;
                }
                for (int l = 0; l < tasksArray.Length; l++)
                {
                    PerfTask task = tasksArray[l];
                    if (task.RunInBackground)
                    {
                        if (bgTasks == null)
                        {
                            bgTasks = new List <RunBackgroundTask>();
                        }
                        RunBackgroundTask bgTask = new RunBackgroundTask(task, letChildReport);
#if FEATURE_THREAD_PRIORITY
                        bgTask.Priority = (task.BackgroundDeltaPriority + Thread.CurrentThread.Priority);
#endif
                        bgTask.Start();
                        bgTasks.Add(bgTask);
                    }
                    else
                    {
                        try
                        {
                            int inc = task.RunAndMaybeStats(letChildReport);
                            count += inc;
                            if (countsByTime != null)
                            {
                                int slot = (int)((Support.Time.CurrentTimeMilliseconds() - t0) / logByTimeMsec);
                                if (slot >= countsByTime.Length)
                                {
                                    countsByTime = ArrayUtil.Grow(countsByTime, 1 + slot);
                                }
                                countsByTime[slot] += inc;
                            }
                            if (anyExhaustibleTasks)
                            {
                                UpdateExhausted(task);
                            }
                        }
                        catch (NoMoreDataException /*e*/)
                        {
                            exhausted = true;
                        }
                    }
                }
                if (fixedTime && Support.Time.CurrentTimeMilliseconds() - t0 > runTime)
                {
                    repetitions = k + 1;
                    break;
                }
            }

            if (bgTasks != null)
            {
                foreach (RunBackgroundTask bgTask in bgTasks)
                {
                    bgTask.StopNow();
                }
                foreach (RunBackgroundTask bgTask in bgTasks)
                {
                    bgTask.Join();
                    count += bgTask.Count;
                }
            }

            if (countsByTime != null)
            {
                RunData.Points.CurrentStats.SetCountsByTime(countsByTime, logByTimeMsec);
            }

            Stop = false;

            return(count);
        }
Beispiel #8
0
 public RunBackgroundTask(PerfTask task, bool letChildReport)
 {
     this.task           = task;
     this.letChildReport = letChildReport;
 }
Beispiel #9
0
        private int DoSerialTasks()
        {
            if (rate > 0)
            {
                return(DoSerialTasksWithRate());
            }

            InitTasksArray();
            int count = 0;

            long runTime = (long)(runTimeSec * 1000);
            IList <RunBackgroundTask> bgTasks = null;

            long t0 = J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results

            for (int k = 0; fixedTime || (repetitions == REPEAT_EXHAUST && !exhausted) || k < repetitions; k++)
            {
                if (Stop)
                {
                    break;
                }
                for (int l = 0; l < tasksArray.Length; l++)
                {
                    PerfTask task = tasksArray[l];
                    if (task.RunInBackground)
                    {
                        if (bgTasks is null)
                        {
                            bgTasks = new JCG.List <RunBackgroundTask>();
                        }
                        RunBackgroundTask bgTask = new RunBackgroundTask(task, letChildReport);
                        bgTask.Priority = (task.BackgroundDeltaPriority + Thread.CurrentThread.Priority);
                        bgTask.Start();
                        bgTasks.Add(bgTask);
                    }
                    else
                    {
                        try
                        {
                            int inc = task.RunAndMaybeStats(letChildReport);
                            count += inc;
                            if (countsByTime != null)
                            {
                                int slot = (int)(((J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) - t0) / logByTimeMsec); // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
                                if (slot >= countsByTime.Length)
                                {
                                    countsByTime = ArrayUtil.Grow(countsByTime, 1 + slot);
                                }
                                countsByTime[slot] += inc;
                            }
                            if (anyExhaustibleTasks)
                            {
                                UpdateExhausted(task);
                            }
                        }
                        catch (NoMoreDataException /*e*/)
                        {
                            exhausted = true;
                        }
                    }
                }
                if (fixedTime && (J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) - t0 > runTime) // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
                {
                    repetitions = k + 1;
                    break;
                }
            }

            if (bgTasks != null)
            {
                foreach (RunBackgroundTask bgTask in bgTasks)
                {
                    bgTask.StopNow();
                }
                foreach (RunBackgroundTask bgTask in bgTasks)
                {
                    bgTask.Join();
                    count += bgTask.Count;
                }
            }

            if (countsByTime != null)
            {
                RunData.Points.CurrentStats.SetCountsByTime(countsByTime, logByTimeMsec);
            }

            Stop = false;

            return(count);
        }