性能テスト類は、書き方コードによくテストする良い習慣を身につけさせます.NET C#

4075 ワード

紹介:
コード内でテストが必要な関数をループして実行時間を自動的に統計し、マルチスレッドをサポートするのに便利です.
 
使用方法:
            PerformanceTest p = new PerformanceTest();
            p.SetCount(10);//    (  :1)
            p.SetIsMultithread(true);//          (  :false)
            p.Execute(
            i =>
            {
                //       
                Response.Write(i+"<br>");
                System.Threading.Thread.Sleep(1000);


            },
            message =>
            {

                //        
                Response.Write(message);   //      :1.02206 
             
            }
            );

  
 
 
 
ソース:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SyntacticSugar
{
    /// <summary>
    /// **   :       
    /// **     :2015-5-30
    /// **     :-
    /// **    :sunkaixuan
    /// **     :tml
    /// </summary>
    public class PerformanceTest
    {
        private DateTime BeginTime;
        private DateTime EndTime;
        private ParamsModel Params;

        /// <summary>
        ///      (  :1)
        /// </summary>
        public void SetCount(int count)
        {
            Params.RunCount = count;
        }
        /// <summary>
        ///       (  :false)
        /// </summary>
        /// <param name="isMul">true    </param>
        public void SetIsMultithread(bool isMul)
        {
            Params.IsMultithread = isMul;
        }

        /// <summary>
        ///     
        /// </summary>
        public PerformanceTest()
        {
            Params = new ParamsModel()
            {
                RunCount = 1
            };
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="action"></param>
        public void Execute(Action<int> action, Action<string> rollBack)
        {
            List<Thread> arr = new List<Thread>();
            BeginTime = DateTime.Now;
            for (int i = 0; i < Params.RunCount; i++)
            {
                if (Params.IsMultithread)
                {
                    var thread = new Thread(new System.Threading.ThreadStart(() =>
                    {
                        action(i);
                    }));
                    thread.Start();
                    arr.Add(thread);
                }
                else
                {
                    action(i);
                }
            }
            if (Params.IsMultithread)
            {
                foreach (Thread t in arr)
                {
                    while (t.IsAlive)
                    {
                        Thread.Sleep(10);
                    }
                }

            }
            rollBack(GetResult());
        }

        public string GetResult()
        {
            EndTime = DateTime.Now;
            string totalTime = ((EndTime - BeginTime).TotalMilliseconds / 1000.0).ToString("n5");
            string reval = string.Format("      :{0} ", totalTime);
            Console.Write(reval);
            return reval;
        }

        private class ParamsModel
        {
            public int RunCount { get; set; }
            public bool IsMultithread { get; set; }
        }
    }
}