詳解CodeTimer及XCode性能測(cè)試
詳解CodeTimer及XCode性能測(cè)試是本文要介紹的內(nèi)容,在測(cè)試XCode性能的時(shí)候,發(fā)現(xiàn)每次執(zhí)行測(cè)試程序得到的執(zhí)行時(shí)間差距實(shí)在太大,于是采用了老趙的CodeTimer來計(jì)算線程時(shí)間,后來因?yàn)?strong>測(cè)試程序稍微有點(diǎn)復(fù)雜,在使用匿名委托時(shí)會(huì)有參數(shù)的“打包”過程,于是改進(jìn)了CodeTimer,測(cè)試功能代碼通過實(shí)現(xiàn)一個(gè)繼承自CodeTimer的類來實(shí)現(xiàn),避免每次迭代時(shí)參數(shù)“打包”的過程。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using System.Threading;
- using NewLife.Reflection;
- using NewLife.Exceptions;
- namespace NewLife.Log
- {
- /// <summary>
- /// 代碼性能計(jì)時(shí)器
- /// </summary>
- /// <remarks>參考了老趙(http://www.cnblogs.com/jeffreyzhao/archive/2009/03/10/codetimer.html)
- 和eaglet(http://www.cnblogs.com/eaglet/archive/2009/03/10/1407791.html)兩位的作品</remarks>
- /// <remarks>為了保證性能比較的公平性,采用了多種指標(biāo),并使用計(jì)時(shí)器重寫等手段來避免各種不必要的損耗</remarks>
- public class CodeTimer
- {
- #region 靜態(tài)快速計(jì)時(shí)
- /// <summary>
- /// 計(jì)時(shí)
- /// </summary>
- /// <param name="times"></param>
- /// <param name="action"></param>
- /// <returns></returns>
- public static CodeTimer Time(Int32 times, Action<Int32> action)
- {
- CodeTimer timer = new CodeTimer();
- timer.Times = times;
- timer.Action = action;
- timer.TimeOne();
- timer.Time();
- return timer;
- }
- /// <summary>
- /// 計(jì)時(shí),并用控制臺(tái)輸出行
- /// </summary>
- /// <param name="title"></param>
- /// <param name="times"></param>
- /// <param name="action"></param>
- public static void TimeLine(String title, Int32 times, Action<Int32> action)
- {
- Console.Write("{0,16}:", title);
- CodeTimer timer = new CodeTimer();
- timer.Times = times;
- timer.Action = action;
- timer.ShowProgress = true;
- ConsoleColor currentForeColor = Console.ForegroundColor;
- Console.ForegroundColor = ConsoleColor.Yellow;
- timer.TimeOne();
- timer.Time();
- Console.WriteLine(timer.ToString());
- Console.ForegroundColor = currentForeColor;
- }
- #endregion
- #region PInvoke
- [DllImport("kernel32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);
- [DllImport("kernel32.dll")]
- static extern IntPtr GetCurrentThread();
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime, out long lpExitTime,
- out long lpKernelTime, out long lpUserTime);
- static Boolean supportCycle = true;
- private static ulong GetCycleCount()
- {
- //if (Environment.Version.Major < 6) return 0;
- if (!supportCycle) return 0;
- try
- {
- ulong cycleCount = 0;
- QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
- return cycleCount;
- }
- catch
- {
- supportCycle = false;
- return 0;
- }
- }
- private static long GetCurrentThreadTimes()
- {
- long l;
- long kernelTime, userTimer;
- GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime, out userTimer);
- return kernelTime + userTimer;
- }
- #endregion
- #region 私有字段
- ulong cpuCycles = 0;
- long threadTime = 0;
- int[] gen;
- #endregion
- #region 屬性
- private Int32 _Times;
- /// <summary>次數(shù)</summary>
- public Int32 Times
- {
- get { return _Times; }
- set { _Times = value; }
- }
- private Action<Int32> _Action;
- /// <summary>迭代方法,如不指定,則使用Time(int index)</summary>
- public Action<Int32> Action
- {
- get { return _Action; }
- set { _Action = value; }
- }
- private Boolean _ShowProgress;
- /// <summary>是否顯示控制臺(tái)進(jìn)度</summary>
- public Boolean ShowProgress
- {
- get { return _ShowProgress; }
- set { _ShowProgress = value; }
- }
- private Int32 _Index;
- /// <summary>進(jìn)度</summary>
- public Int32 Index
- {
- get { return _Index; }
- set { _Index = value; }
- }
- private ulong _CpuCycles;
- /// <summary>CPU周期</summary>
- public ulong CpuCycles
- {
- get { return _CpuCycles; }
- set { _CpuCycles = value; }
- }
- private long _ThreadTime;
- /// <summary>線程時(shí)間,單位是100ns,除以10000轉(zhuǎn)為ms</summary>
- public long ThreadTime
- {
- get { return _ThreadTime; }
- set { _ThreadTime = value; }
- }
- private Int32[] _Gen = new Int32[] { 0, 0, 0 };
- /// <summary>GC代數(shù)</summary>
- public Int32[] Gen
- {
- get { return _Gen; }
- set { _Gen = value; }
- }
- private TimeSpan _Elapsed;
- /// <summary>執(zhí)行時(shí)間</summary>
- public TimeSpan Elapsed
- {
- get { return _Elapsed; }
- set { _Elapsed = value; }
- }
- #endregion
- #region 方法
- /// <summary>
- /// 計(jì)時(shí)核心方法,處理進(jìn)程和線程優(yōu)先級(jí)
- /// </summary>
- public virtual void Time()
- {
- if (Times <= 0) throw new XException("非法迭代次數(shù)!");
- // 設(shè)定進(jìn)程、線程優(yōu)先級(jí),并在完成時(shí)還原
- ProcessPriorityClass pp = Process.GetCurrentProcess().PriorityClass;
- ThreadPriority tp = Thread.CurrentThread.Priority;
- try
- {
- Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
- Thread.CurrentThread.Priority = ThreadPriority.Highest;
- StartProgress();
- TimeTrue();
- }
- finally
- {
- StopProgress();
- Thread.CurrentThread.Priority = tp;
- Process.GetCurrentProcess().PriorityClass = pp;
- }
- }
- /// <summary>
- /// 真正的計(jì)時(shí)
- /// </summary>
- protected virtual void TimeTrue()
- {
- if (Times <= 0) throw new XException("非法迭代次數(shù)!");
- // 統(tǒng)計(jì)GC代數(shù)
- GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
- gen = new Int32[GC.MaxGeneration + 1];
- for (Int32 i = 0; i <= GC.MaxGeneration; i++)
- {
- gen[i] = GC.CollectionCount(i);
- }
- Stopwatch watch = new Stopwatch();
- watch.Start();
- cpuCycles = GetCycleCount();
- threadTime = GetCurrentThreadTimes();
- // 如果未指定迭代方法,則使用內(nèi)部的Time
- Action<Int32> action = Action;
- if (action == null)
- {
- action = Time;
- // 初始化
- Init();
- }
- for (Int32 i = 0; i < Times; i++)
- {
- Index = i;
- action(i);
- }
- if (Action == null)
- {
- // 結(jié)束
- Finish();
- }
- CpuCycles = GetCycleCount() - cpuCycles;
- ThreadTime = GetCurrentThreadTimes() - threadTime;
- watch.Stop();
- Elapsed = watch.Elapsed;
- // 統(tǒng)計(jì)GC代數(shù)
- List<Int32> list = new List<Int32>();
- for (Int32 i = 0; i <= GC.MaxGeneration; i++)
- {
- int count = GC.CollectionCount(i) - gen[i];
- list.Add(count);
- }
- Gen = list.ToArray();
- }
- /// <summary>
- /// 執(zhí)行一次迭代,預(yù)熱所有方法
- /// </summary>
- public void TimeOne()
- {
- Int32 n = Times;
- try
- {
- Times = 1;
- Time();
- }
- finally { Times = n; }
- }
- /// <summary>
- /// 迭代前執(zhí)行,計(jì)算時(shí)間
- /// </summary>
- public virtual void Init() { }
- /// <summary>
- /// 每一次迭代,計(jì)算時(shí)間
- /// </summary>
- /// <param name="index"></param>
- public virtual void Time(Int32 index) { }
- /// <summary>
- /// 迭代后執(zhí)行,計(jì)算時(shí)間
- /// </summary>
- public virtual void Finish() { }
- #endregion
- #region 進(jìn)度
- Thread thread;
- void StartProgress()
- {
- if (!ShowProgress) return;
- // 使用低優(yōu)先級(jí)線程顯示進(jìn)度
- thread = new Thread(new ParameterizedThreadStart(Progress));
- thread.IsBackground = true;
- thread.Priority = ThreadPriority.BelowNormal;
- thread.Start();
- }
- void StopProgress()
- {
- if (thread != null && thread.IsAlive)
- {
- thread.Abort();
- thread.Join(3000);
- }
- }
- void Progress(Object state)
- {
- Int32 left = Console.CursorLeft;
- // 設(shè)置光標(biāo)不可見
- Boolean cursorVisible = Console.CursorVisible;
- Console.CursorVisible = false;
- Stopwatch sw = new Stopwatch();
- sw.Start();
- while (true)
- {
- try
- {
- Int32 i = Index;
- if (i >= Times) break;
- if (i > 0 && sw.Elapsed.TotalMilliseconds > 10)
- {
- Double d = (Double)i / Times;
- Console.Write("{0,7:n0}ms {1:p}", sw.Elapsed.TotalMilliseconds, d);
- Console.CursorLeft = left;
- }
- }
- catch (ThreadAbortException) { break; }
- catch { break; }
- Thread.Sleep(500);
- }
- sw.Stop();
- Console.CursorLeft = left;
- Console.CursorVisible = cursorVisible;
- }
- #endregion
- #region 重載
- /// <summary>
- /// 已重載。輸出依次分別是:執(zhí)行時(shí)間、CPU線程時(shí)間、時(shí)鐘周期、GC代數(shù)
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return String.Format("{0,7:n0}ms {1,7:n0}ms {2,15:n0} {3}/{4}/{5}", Elapsed.TotalMilliseconds,
- ThreadTime / 10000, CpuCycles, Gen[0], Gen[1], Gen[2]);
- }
- #endregion
- }
- }
對(duì)于控制臺(tái)測(cè)試項(xiàng)目,另外起了一個(gè)線程負(fù)責(zé)輸出進(jìn)度,不知道這樣對(duì)測(cè)試會(huì)有多大影響。
#p#
XCode性能測(cè)試
XCode每次升級(jí)都會(huì)進(jìn)行性能測(cè)試,不過那是為了檢查升級(jí)是否造成了性能瓶頸,實(shí)際上性能測(cè)試就是作為XCode升級(jí)的最后一道工作。
上一次與ADO.Net進(jìn)行性能對(duì)比測(cè)試時(shí)XCode的版本是v3.5,XCode各種操作的耗時(shí)大概是ADO.Net的1.2倍,vs統(tǒng)計(jì)代碼只有2000行。
目前XCode最新版本是v7.3,vs統(tǒng)計(jì)代碼有5100行,并且引用一個(gè)4100行的核心庫,一些常用的擴(kuò)展功能形成4800行的通用實(shí)體類庫。
由此可見,現(xiàn)在的XCode至少在代碼上是v3.5的7倍。(當(dāng)然,這個(gè)代碼量是遠(yuǎn)不如NH的,記得它有好些文件超過了1000行代碼)
廢話少說,下面開始測(cè)試!
本地環(huán)境:win7+MSSQL2007
說明:
1、以下截圖,黃色數(shù)字分別代表執(zhí)行時(shí)間、線程時(shí)間、CPU周期、GC,白色數(shù)字表示與第一個(gè)測(cè)試項(xiàng)相比較的比列,兩個(gè)白色分別表示執(zhí)行時(shí)間比例和線程時(shí)間比例
2、ADO.SQL表示用sql方式執(zhí)行,ADO.Param表示用參數(shù)化執(zhí)行
3、DAL表示用XCode的數(shù)據(jù)訪問層執(zhí)行SQL,DALIdentity表示插入后查詢自增,如果開啟一級(jí)緩存,這兩項(xiàng)會(huì)有影響
4、Entity是普通實(shí)體類操作,WeakEntity表示弱類型操作實(shí)體,DynEntity表示動(dòng)態(tài)創(chuàng)建實(shí)體類(CodeDom)執(zhí)行操作
5、所有比例的計(jì)算以ADO.SQL為基準(zhǔn),因?yàn)閄Code也是采用這種方式
本地普通測(cè)試:
總體來看,XCode的性能大概是ADO的1.5倍。
后面的查詢中,WeakEntity和DynEntity的比例小于1,Entity也很小,主要是因?yàn)閄Code的二級(jí)緩存(實(shí)體緩存)。每一次查詢測(cè)試,實(shí)際上包含了查一個(gè)管理員和一個(gè)角色,而角色表數(shù)據(jù)較少,XCode使用了實(shí)體緩存,所以XCode對(duì)角色的查詢幾乎接近于0。XCode的實(shí)體緩存能夠保證數(shù)據(jù)數(shù)據(jù)的新鮮性,這里不能說不公平。
開啟一級(jí)緩存
可以注意到,開啟一級(jí)緩存后,XCode的表現(xiàn)非常出色,并且越是后面的測(cè)試項(xiàng)越出色。因?yàn)?,后面三?xiàng)都必須通過DAL來執(zhí)行,而一級(jí)緩存正是位于DAL中。所以XCode的第一個(gè)測(cè)試項(xiàng)DAL會(huì)比較慢,因?yàn)樗木彺婷新侍土耍⑶疫€要負(fù)責(zé)緩存數(shù)據(jù)等操作。查詢哪個(gè)管理員是隨機(jī)的,越是到了后面,隨著緩存命中率的提高,速度就越快。
XCode的一級(jí)緩存也是能保證實(shí)時(shí)更新的,也許這個(gè)測(cè)試作為與ADO的標(biāo)準(zhǔn)測(cè)試比較好。
下面我們?cè)囋噭e的數(shù)據(jù)庫,SQLite吧,開啟一級(jí)緩存。SQLite插入后獲取自增的方法跟MSSQL不一樣,為了讓測(cè)試代碼簡(jiǎn)單,我們放過它,允許ADO的兩個(gè)測(cè)試項(xiàng)不插入角色。而XCode是能夠很好支持各種數(shù)據(jù)庫獲取自增的
首先看到的是,沒有開啟事務(wù)的SQLite,實(shí)在是太不給力了,執(zhí)行時(shí)間很長(zhǎng),但是線程時(shí)間很短。這個(gè)測(cè)試告訴我們,用SQLite要盡可能的開事務(wù)。
為了更切近生產(chǎn)環(huán)境,下面我們?cè)囋囘h(yuǎn)程的MSSQL,位于局域網(wǎng)內(nèi)的window 2008 r2上的MSSQL2008
可以看到,越是切近生產(chǎn)環(huán)境,數(shù)據(jù)量越大,XCode表現(xiàn)越是出色!
把MySql也拉出來溜溜
該MySql部署在一個(gè)XP虛擬機(jī)上(512M內(nèi)存安裝了MySql、Oracle、Firebird、PostgreSQL),并且各種配置都是開發(fā)用配置,測(cè)試數(shù)據(jù)不是很穩(wěn)定。
后面會(huì)附上測(cè)試程序,以及測(cè)試程序的源代碼,感興趣的同學(xué)可以在自己機(jī)器上執(zhí)行測(cè)試程序看看結(jié)果如何。
建議對(duì)XCode感興趣的同學(xué)都看看Performance.cs源碼,每一個(gè)測(cè)試項(xiàng),同時(shí)也展示著如何使用XCode,如何支持多數(shù)據(jù)庫,如何做到更好的性能!
BTW:
這段時(shí)間一直在準(zhǔn)備一篇文章《XCode這樣處理無限增長(zhǎng)的海量數(shù)據(jù)》,靈感源自于一位使用XCode做項(xiàng)目的同學(xué),他用了三百多張相同結(jié)構(gòu)的表,并且表的數(shù)量可能會(huì)無限增多,每張表有數(shù)百萬的數(shù)據(jù)。沒錯(cuò),這是一個(gè)數(shù)據(jù)采集系統(tǒng),包括采集、分析整理、查詢展現(xiàn)三大塊。
他使用了XCode十八般武藝中的動(dòng)態(tài)修改表,實(shí)現(xiàn)一個(gè)實(shí)體類控制幾百張表的需求,當(dāng)然,也包括自動(dòng)創(chuàng)建表。盡管這項(xiàng)功能位列于十八般武藝當(dāng)中,與三級(jí)緩存并重,但實(shí)際上項(xiàng)目使用得不多,風(fēng)險(xiǎn)還是挺大的。至少,到現(xiàn)在為止,沒有發(fā)現(xiàn)太大的問題。
我想以他的這個(gè)項(xiàng)目為例子,詳細(xì)的講解一下XCode的各個(gè)緩存,以及如何去處理海量數(shù)據(jù)。當(dāng)然,還要包括最新版本的分布式,是的,下一版本的XCode直接支持異構(gòu)數(shù)據(jù)庫的分布式,提高性能,或者實(shí)現(xiàn)數(shù)據(jù)的熱備,業(yè)務(wù)層不需要做任何修改。
小結(jié):詳解CodeTimer及XCode性能測(cè)試的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!
測(cè)試代碼請(qǐng)看http://xcode.codeplex.com