LayoutFarm.Demo.DemoForm.OnRunTestButtonClick C# (CSharp) Method

OnRunTestButtonClick() private method

Execute performance test by setting all sample htmls in a loop.
private OnRunTestButtonClick ( object sender, EventArgs e ) : void
sender object
e EventArgs
return void
        private void OnRunTestButtonClick(object sender, EventArgs e)
        {
            _updateLock = true;
            _runTestButton.Text = "Running..";
            _runTestButton.Enabled = false;
            Application.DoEvents();
            GC.Collect();
#if NET_40
            AppDomain.MonitoringIsEnabled = true;
            var startMemory = AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize;
#endif


            const int iterations = 40;
            //for (int i = 0; i < iterations; i++)
            //{                
            //    foreach (var html in _perfTestSamples)
            //    {
            //        _htmlPanel.Text = html;
            //        Application.DoEvents(); // so paint will be called
            //    }
            //}
            List<int> selectedSamples = new List<int>();
            var allTestCount = _perfTestSamples.Count;
            for (int i = 0; i < allTestCount; ++i)
            {
                selectedSamples.Add(2);
            }



            //HtmlRenderer.dbugCounter.dbugStartRecord = true;
            //HtmlRenderer.dbugCounter.dbugDrawStringCount = 0;
            long ms_total = 0;
            System.Diagnostics.Stopwatch sw = new Stopwatch();
#if DEBUG
            for (int i = 0; i < iterations; i++)
            {
                foreach (var sampleNum in selectedSamples)
                {
                    ms_total += dbugCounter.Snap(sw, () =>
                    {
                        //HtmlRenderer.dbugCounter.dbugStartRecord = true;
                        //HtmlRenderer.dbugCounter.dbugDrawStringCount = 0;
                        _htmlPanel.Text = _perfTestSamples[sampleNum];
                        Application.DoEvents(); // so paint will be called 
                    });
                }
            }
#endif
            long endMemory = 0;
            float totalMem = 0;
#if NET_40
            endMemory = AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize;
            totalMem = (endMemory - startMemory)/1024f;
#endif
            float htmlSize = 0;
            foreach (var sample in _perfTestSamples)
            {
                htmlSize += sample.Length * 2;
            }
            htmlSize = htmlSize / 1024f;
            var msg = string.Format("{0} HTMLs ({1:N0} KB)\r\n{2} Iterations", _perfTestSamples.Count, htmlSize, iterations);
            msg += "\r\n\r\n";
            msg += string.Format("CPU:\r\nTotal: {0} msec\r\nIterationAvg: {1:N2} msec\r\nSingleAvg: {2:N2} msec",
                                   ms_total, ms_total / iterations, ms_total / (double)iterations / _perfTestSamples.Count);
            msg += "\r\n\r\n";
            msg += string.Format("Memory:\r\nTotal: {0:N0} KB\r\nIterationAvg: {1:N0} KB\r\nSingleAvg: {2:N0} KB\r\nOverhead: {3:N0}%",
                                 totalMem, totalMem / iterations, totalMem / iterations / _perfTestSamples.Count, 100 * (totalMem / iterations) / htmlSize);
            Clipboard.SetDataObject(msg);
            MessageBox.Show(msg, "Test run results");
            _updateLock = false;
            _runTestButton.Text = "Run Performance Test";
            _runTestButton.Enabled = true;
        }