NHibernate - 效能分析器



在本章中,我們將瞭解如何從資料庫中檢索、更新、建立和刪除所有記錄,以及這些查詢是如何執行的?

為了理解所有這些,我們可以在配置中簡單地新增一個選項,該選項將 SQL 記錄到控制檯。以下是可以記錄 SQL 查詢的簡單語句:

x.LogSqlInConsole = true;

現在,我們在 NHibernateDemoDB 資料庫的 student 表中有兩條記錄。讓我們檢索資料庫中的所有記錄,如下面的程式碼所示。

using NHibernate.Cfg; 
using NHibernate.Dialect; 
using NHibernate.Driver; 

using System; 
using System.Linq; 
using System.Reflection;

namespace NHibernateDemoApp { 

   class Program { 
      
      static void Main(string[] args) { 
         var cfg = new Configuration();
			
         String Data Source = asia13797\\sqlexpress;
         String Initial Catalog = NHibernateDemoDB;
         String Integrated Security = True;
         String Connect Timeout = 15;
         String Encrypt = False;
         String TrustServerCertificate = False;
         String ApplicationIntent = ReadWrite;
         String MultiSubnetFailover = False;			
         
         cfg.DataBaseIntegration(x = > { x.ConnectionString = "Data Source + 
            Initial Catalog + Integrated Security + Connect Timeout + Encrypt +
            TrustServerCertificate + ApplicationIntent + MultiSubnetFailover"; 
            
            x.Driver<SqlClientDriver>(); 
            x.Dialect<MsSql2008Dialect>(); 
            x.LogSqlInConsole = true; 
         }); 
      
         cfg.AddAssembly(Assembly.GetExecutingAssembly()); 
         var sefact = cfg.BuildSessionFactory();
      
         using (var session = sefact.OpenSession()) { 
         
            using (var tx = session.BeginTransaction()) { 
               Console.WriteLine("\nFetch the complete list again\n");
               var students = session.CreateCriteria<Student>().List<Student>(); 
      
               foreach (var student in students) { 
                  Console.WriteLine("{0} \t{1} \t{2}", student.ID, student.FirstMidName,
                     student.LastName); 
               } 
               
               tx.Commit(); 
            } 
				
            Console.ReadLine(); 
         } 
      } 
   } 
}

因此,讓我們再次執行此應用程式,您將看到以下輸出:

NHibernate: SELECT this_.ID as ID0_0_, this_.LastName as LastName0_0_,
   this_.FirstMidName as FirstMid3_0_0_ FROM Student this_

Fetch the complete list again

3 Allan Bommer
4 Jerry Lewis

如您所見,傳送到資料庫的select 子句,它實際上類似於子句,它將檢索 ID、FirstMidName 和 LastName。因此,所有這些都將傳送到資料庫並在那裡進行處理,而不是將大量記錄帶回您的伺服器並在伺服器端進行處理。

NHibernate 效能分析器

檢視這些結果的另一種方法是使用 NHibernate 效能分析器。NHibernate 效能分析器是一個商業工具,但它對於處理 NHibernate 應用程式非常有用。您可以輕鬆地從 NuGet 將 NHibernate 效能分析器安裝到您的應用程式中。

讓我們從“工具”選單中的“NuGet 包管理器”→“程式包管理器控制檯”進入 NuGet 包管理器控制檯。它將開啟“程式包管理器控制檯”視窗。輸入以下命令並按 Enter 鍵。

PM> install-package NHibernateProfiler

它將安裝 NHibernate 效能分析器所需的所有二進位制檔案,一旦成功安裝,您將看到以下訊息。

NHibernate Profiler

您還將看到 NHibernate 效能分析器已啟動,一旦安裝完成。它需要許可證才能使用,但出於演示目的,我們可以使用 NHibernate 效能分析器的 30 天試用版。

現在,NHibernate 效能分析器已針對 Web 應用程式進行了最佳化,您將看到它在解決方案資源管理器中添加了App_Start 資料夾。為了使所有這些都保持簡單,請刪除 App_Start 資料夾,並且您還會注意到在 Program 類中的 Main 方法開頭添加了一個語句。

App_Start.NHibernateProfilerBootstrapper.PreStart();

也請刪除此語句,只需新增一個簡單的呼叫NHibernateProfiler.Initialize,如下面的程式碼所示。

using HibernatingRhinos.Profiler.Appender.NHibernate; 
using NHibernate.Cfg; 
using NHibernate.Dialect; 
using NHibernate.Driver; 

using System; 
using System.Linq; 
using System.Reflection;

namespace NHibernateDemoApp { 
   
   class Program { 
	
      static void Main(string[] args) { 
		
         NHibernateProfiler.Initialize(); 
         var cfg = new Configuration();
			
         String Data Source = asia13797\\sqlexpress;
         String Initial Catalog = NHibernateDemoDB;
         String Integrated Security = True;
         String Connect Timeout = 15;
         String Encrypt = False;
         String TrustServerCertificate = False;
         String ApplicationIntent = ReadWrite;
         String MultiSubnetFailover = False;			
         
         cfg.DataBaseIntegration(x = > { x.ConnectionString = "Data Source + 
            Initial Catalog + Integrated Security + Connect Timeout + Encrypt +
            TrustServerCertificate + ApplicationIntent + MultiSubnetFailover";
				
            x.Driver<SqlClientDriver>(); 
            x.Dialect<MsSql2008Dialect>(); 
            x.LogSqlInConsole = true; 
         }); 

         cfg.AddAssembly(Assembly.GetExecutingAssembly()); 
         var sefact = cfg.BuildSessionFactory(); 
         
         using (var session = sefact.OpenSession()) { 
            
            using (var tx = session.BeginTransaction()){ 
               var students = session.CreateCriteria<Student>().List<Student>(); 
               Console.WriteLine("\nFetch the complete list again\n");
               
               foreach (var student in students) { 
                  Console.WriteLine("{0} \t{1} \t{2}", student.ID, student.FirstMidName,
                     student.LastName); 
               } 
					
               tx.Commit(); 
            } 
				
            Console.ReadLine(); 
         } 
      } 
   
   }
}

現在,當您執行應用程式時,它將資料傳送到 NHibernate 效能分析器應用程式。

NHibernate Profiler Application

您可以在此處看到,我們有一個很好的顯示,顯示我們已啟動事務,SQL 在資料庫中以一種很好的格式執行的操作。

因此,這對於確定 NHibernate 應用程式內部到底發生了什麼非常有用。一旦應用程式達到一定的複雜程度,它就會變得非常有用,在這種情況下,您需要類似於 SQL Profiler 的工具,但同時還要了解 NHibernate。

廣告