精通軟件性能測試與LoadRunner最佳實戰 連載十五
14.3 前端性能測試自動化介紹
隨著功能測試的發展與壯大,功能自動化測試已經越來越廣泛地應用于現行軟件系統的測試,那么性能測試方面是否能給實現自動化的控制呢?答案是肯定的,隨著行業的發展,性能測試的研究也日趨深入,前面我們向大家介紹了如何自動化控制場景的運行,當然這只是性能測試自動化的冰山一角,其目的也是拓展大家性能測試方面的思路。
本節作者將向大家介紹前端性能測試的自動化控制,據我所知有一些單位已經針對自己公司的業務開發了一些適用其自身的性能測試工具,鑒于其針對行業的特殊性,這里我們不予探討。我們謹以通用的、被大家認可的前端性能工具為例,講解前端性能自動化控制的相關內容,在此我們挑選的工具就是HttpWatch,在前面的章節相信大家已經看到了HttpWatch在前端性能測試分析方面是如此強大,肯定也很關心其更深層次的應用,這里我向大家介紹該工具自動化前端性能測試的實現方法,您可以通過訪問“http://apihelp.httpwatch.com/”了解更多關于httpwatch自動化方面的內容。
圖14-21描述了Controller、Log、Entry和Plugin這4個主要類與瀏覽器之間的關系,下面我們來看一下這4個主要類都是用來做什么的。
1.控制類(Controller Class)
HttpWatch控制類用來創建一個HttpWatch插件實例,或者是打開一個已經存在的實例文件。可以通過這個控制類的一個“OpenLog”方法打開一個日志文件,并且返回這個日志文件的相應的說明信息,這個說明信息包括的就是錄制過中的請求和響應文件信息。
2.插件類(Plugin Class)
HttpWatch分別為IE和Firefox提供了插件類,它主要是針對Http協議交互提供啟動和停止方法去控制HttpWatch的錄制和停止錄制功能,對應的方法名稱為“Record”和“Stop”,并且還提供了一些方法和屬性去管理和配置自動化錄制方式。其中插件類中的“GotoURL”方法可以用于重定向瀏覽任何指定的URL地址。
3.日志類(Log Class)
HttpWatch日志類用于獲取日志信息,這些日志信息就是HttpWatch錄制過程記錄的請求和響應信息。日志類提供了許多屬性和方法,并且允許對這些錄制的數據信息進行檢索和保存,或者以多種格式導出(支持:HWL、CSV、XML、HAR格式)等。
4.屬性類(Entry Class)
HttpWatch屬性類的每個日志文件都包含一個屬性列表,且這個屬性列表中包括詳細的HTTP交互信息。這些內容具體包括請求的資源信息和一些返回的信息。這個請求和響應屬性信息提供了訪問頭文件和Cookies文件,這些信息都是在與服務器發生交互過程中產生的。同時最后產生的結果信息也通過該類屬性進行輸出,如:BytesReceived屬性是接收的總字節數。
14.4 HttpWatch前端性能測試自動化腳本
HttpWatch提供了一些腳本示例代碼供大家參考,在HttpWatch安裝目錄下的“api_examples”文件夾下,您會看到2個子文件夾:“ie”和“Firefox”,這里我們希望看到基于IE瀏覽器的相關API調用方法,所以選擇“ie”文件夾,再進入到“page_check”,在該文件夾下還有4個子文件夾,分別是“csharp”、“javascript”、“ruby”、“VBScript”,下面就讓我們一起來看一下這4個文件夾下主要的腳本實現代碼。
Ruby腳本代碼(page_check.rb和page_check_watir.rb文件內容):
#page_check.rb文件 # Page Check Ruby Example # # For more information about this example please refer to the readme.txt file in the # same directory # require 'win32ole' puts "Enter the URL of the page to check (press enter for www.httpwatch.com):\n"; $stdout. flush url = gets.chomp! if url.empty? url = "www.httpwatch.com" end puts "\nChecking " + url + "...\r\n\r\n"; $stdout.flush # Create a new instance of HttpWatch in IE control = WIN32OLE.new('HttpWatch.Controller') plugin = control.IE.New() # Start Recording HTTP traffic plugin.Log.EnableFilter(false) plugin.Record() # Goto to the URL and wait for the page to be loaded plugin.GotoURL(url) control.Wait(plugin, -1) # Stop recording HTTP plugin.Stop() if plugin.Log.Pages.Count != 0 printf "\nPage Title: '%s'\n", plugin.Log.Pages(0).Title # Display summary statistics for page summary = plugin.Log.Pages(0).Entries.Summary printf "Total time to load page (secs): %.3f\n", summary.Time printf "Number of bytes received on network: %d\n", summary.BytesReceived printf "HTTP compression saving (bytes): %d\n", summary.CompressionSavedBytes printf "Number of round trips: %d\n", summary.RoundTrips printf "Number of errors: %d\n", summary.Errors.Count end |
# Close down IE plugin.CloseBrowser() puts "\r\nPress Enter to exit"; $stdout.flush gets # page_check_watir.rb文件 # Page Check Ruby and WATIR Example # # For more information about this example please refer to the readme.txt file in the # same directory # require 'win32ole' require 'watir' puts "Enter the URL of the page to check (press enter for www.httpwatch.com):\n"; $stdout. flush url = gets.chomp! if url.empty? url = "www.httpwatch.com" end puts "\nChecking " + url + "...\r\n\r\n"; $stdout.flush # Attach HttpWatch control = WIN32OLE.new('HttpWatch.Controller') # Open the IE browser plugin = control.IE.New # Attach Watir to IE browser containing HttpWatch plugin Watir::IE::attach_timeout = 30 ie = Watir::IE.attach(:hwnd, plugin.Container.HWND ) # Start Recording HTTP traffic plugin.Clear() plugin.Log.EnableFilter(false) plugin.Record() # Goto to the URL and wait for the page to be loaded ie.goto(url) # Stop recording HTTP plugin.Stop() if plugin.Log.Pages.Count != 0 printf "\nPage Title: '%s'\n", plugin.Log.Pages(0).Title # Display summary statistics for page summary = plugin.Log.Pages(0).Entries.Summary printf "Total time to load page (secs): %.3f\n", summary.Time printf "Number of bytes received on network: %d\n", summary.BytesReceived printf "HTTP compression saving (bytes): %d\n", summary.CompressionSavedBytes printf "Number of round trips: %d\n", summary.RoundTrips printf "Number of errors: %d\n", summary.Errors.Count end # Close down IE plugin.CloseBrowser(); puts "\r\nPress Enter to exit"; $stdout.flush gets |
C#源代碼(pagechecker.cs文件內容):
// Page Check C# Example // // For more information about this example please refer to the readme.txt file in the // same directory // using System; using HttpWatch; namespace page_check { class PageChecker { [STAThread] static void Main(string[] args) { Console.WriteLine("Enter the URL of the page to check (press enter for www.httpwatch.com):\r\n"); string url = Console.ReadLine(); if ( url.Length == 0 ) url = "www.httpwatch.com"; Console.WriteLine("\r\nChecking " + url + "...\r\n"); // Create a new instance of HttpWatch in IE Controller control = new Controller(); Plugin plugin = control.IE.New(); // Start Recording HTTP traffic plugin.Log.EnableFilter(false); plugin.Record(); // Goto to the URL and wait for the page to be loaded plugin.GotoURL(url); control.Wait(plugin, -1); // Stop recording HTTP plugin.Stop(); if (plugin.Log.Pages.Count != 0) { Console.WriteLine("\r\nPage Title: '" + plugin.Log.Pages[0].Title + "'"); Console.WriteLine(); // Display summary statistics for page Summary summary = plugin.Log.Pages[0].Entries.Summary; Console.WriteLine("Total time to load page (secs): " + summary.Time); Console.WriteLine("Number of bytes received on network: " + summary.BytesReceived); Console.WriteLine("HTTP compression saving (bytes): " + summary.CompressionSavedBytes); Console.WriteLine("Number of round trips: " + summary.RoundTrips); Console.WriteLine("Number of errors: " + summary.Errors.Count); } |
VBScript腳本代碼(page_check.vbs文件內容):
' Page Check VBScript Example ' ' For more information about this example please refer to the readme.txt file in the ' same directory ' WScript.Echo(vbCrLf & "Enter the URL of the page to check (press enter for www.httpwatch.com):" & vbCrLf) Dim url url = WScript.StdIn.ReadLine if Len(url) = 0 then url = "www.httpwatch.com" end if WScript.Echo( vbCrLf & "Checking " & url & "..." & vbCrLf) ' Create a new instance of HttpWatch in IE Dim control Set control = CreateObject("HttpWatch.Controller") Dim plugin Set plugin = control.IE.New ' Start Recording HTTP traffic plugin.Log.EnableFilter false plugin.Record ' Goto to the URL and wait for the page to be loaded plugin.GotoURL url control.Wait plugin, -1 ' Stop recording HTTP plugin.Stop if plugin.Log.Pages.Count <> 0 then WScript.Echo("") WScript.Echo("Page Title: '" & plugin.Log.Pages(0).Title & "'") ' Display summary statistics for page Dim summary Set summary = plugin.Log.Pages(0).Entries.Summary WScript.Echo( "Total time to load page (secs): " & summary.Time) WScript.Echo( "Number of bytes received on network: " & summary.BytesReceived) WScript.Echo( "HTTP compression saving (bytes): " & summary.CompressionSavedBytes) WScript.Echo( "Number of round trips: " & summary.RoundTrips) WScript.Echo( "Number of errors: " & summary.Errors.Count) end if ' Close down IE plugin.CloseBrowser WScript.Echo( vbCrLf & "Press Enter to exit") WScript.StdIn.ReadLine |
JavaScript腳本代碼(page_check.js文件內容):
// Page Check Javascript Example // // For more information about this example please refer to the readme.txt file in the // same directory // WScript.Echo("\nEnter the URL of the page to check (press enter for www.httpwatch.com):\n"); var url = WScript.StdIn.ReadLine(); if ( url.length == 0 ) url = "www.httpwatch.com"; WScript.Echo("\nChecking " + url + "...\n"); // Create a new instance of HttpWatch in IE var control = new ActiveXObject('HttpWatch.Controller'); var plugin = control.IE.New(); // Start Recording HTTP traffic plugin.Log.EnableFilter(false); plugin.Record(); // Goto to the URL and wait for the page to be loaded plugin.GotoURL(url); control.Wait(plugin, -1); // Stop recording HTTP plugin.Stop(); if ( plugin.Log.Pages.Count != 0 ) { WScript.Echo( "\nPage Title: '" + plugin.Log.Pages(0).Title + "'"); // Display summary statistics for page var summary = plugin.Log.Pages(0).Entries.Summary; WScript.Echo( "Total time to load page (secs): " + summary.Time); WScript.Echo( "Number of bytes received on network: " + summary.BytesReceived); WScript.Echo( "HTTP compression saving (bytes): " + summary.CompressionSavedBytes); WScript.Echo( "Number of round trips: " + summary.RoundTrips); WScript.Echo( "Number of errors: " + summary.Errors.Count); } // Close down IE plugin.CloseBrowser(); WScript.Echo( "\r\nPress Enter to exit"); WScript.StdIn.ReadLine(); |
上述4個腳本其實現的目標是一致的,都是讓您先輸入一個要考察的網址,然后其調用httpwatch相關的API函數開始錄制、記錄過程數據、停止錄制、輸出結果信息、關閉瀏覽器這樣一個處理過程。這里以JavaScript腳本為例,運行“run.cmd”文件,在彈出的控制臺界面輸入“www.baidu.com”,將會自動打開“百度”頁面,過一會頁面將會關閉返回控制臺界面,在控制臺將產生相關的結果信息,如圖14-22所示,按任意鍵將退出控制臺界面。
(未完待續)
版權聲明:51Testing軟件測試網及相關內容提供者擁有51testing.com內容的全部版權,未經明確的書面許可,任何人或單位不得對本網站內容復制、轉載或進行鏡像。51testing軟件測試網歡迎與業內同行進行有益的合作和交流,如果有任何有關內容方面的合作事宜,請聯系我們。
相關鏈接:
posted on 2013-07-18 10:55 順其自然EVO 閱讀(1750) 評論(0) 編輯 收藏 所屬分類: loadrunner 、web 前端性能測試