.NET性能單元測試 NTime

openkk 12年前發布 | 17K 次閱讀 .NET 性能測試和優化

NTime 是一款用來測試 .NET 應用性能的單元測試工具,

using System;
using NTime.Framework;

namespace NTime.Tests
{
    /// <SUMMARY>
    /// This is a tested class.
    /// </SUMMARY>
    [TimerFixture]
    public class TestClass
    {
        [TimerFixtureSetUp]
        public void GlobalSetUp()
        {
            // initialize one-time initialization data in
            // this testfixture.
        }

        [TimerFixtureTearDown]
        public void GlobalTearDown()
        {
            // release one-time initialized data in
            // this testfixture.
        }

        [TimerSetUp]
        public void LocalSetUp()
        {
            // initialize data for every test found
            // in this testfixture class
        }

        [TimerTearDown]
        public void LocalTearDown()
        {
            // release data for every finished test
            // found in this testfixture class
        }

        [TimerHitCountTest(300, Threads = 3,
          Unit = TimePeriod.Second)]
        public void WebRequest()
        {
            // invoke some code from real application
            // to test its functions against specified
            // performance.
            // in example we will keep our fictional
            // web server's response at 10 milliseconds
            if(System.Threading.Thread.CurrentThread.Name == 
                                                  "NTimeThread_0")
              System.Threading.Thread.Sleep(5);
            else if(System.Threading.Thread.CurrentThread.Name == 
                                                   NTimeThread_1")
              System.Threading.Thread.Sleep(8);
            else
              System.Threading.Thread.Sleep(10);
        }

        [TimerDurationTest(20, Unit = TimePeriod.Millisecond)]
        public void GameScreenFlicking()
        {
            // we want to calc game AI, 3d engine and
            // other stuff to keep it running at 50Hz
            // vertical screen sync.
            System.Threading.Thread.Sleep(5);
        }

        [TimerDurationTest(10, Unit = TimePeriod.Millisecond)]
        [TimerIgnore("This test is disabled.")]
        public void SuperSortAlgorithm()
        {
            // code here will not be profiled until TimerIgnore
            // flag was removed.
        }

        [TimerCounterTest("Process", "% Processor Time", 
          Threads = 1, InstanceName = "NTimeGUI", 
          MinimumValue = 0, MaximumValue = 50)]
        public void ProcessorUsage()
        {
            // we want to see whether our application is
            // optimized to work with minimal CPU usage.
            for(int i = 0; i < 150; i++)
            {
                System.Threading.Thread.Sleep(30);
            }
        }
    }
}
界面如下圖所示:

.NET性能單元測試 NTime

項目主頁:http://www.baiduhome.net/lib/view/home/1330432336967

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!