共用方式為


步驟 1:建立單元測試以將檔提交至 BizTalk Server

BizTalk Server 之類的計算機應用程式伺服器是設計來代表使用者執行特定工作。 這些任務會作為符合應用程式伺服器理解標準的客戶端請求,透過應用程式伺服器所理解的協定發送至伺服器。 例如,用戶端可能會透過 SMTP 通訊協定將因特網電子郵件訊息傳送至電子郵件伺服器,起始處理電子郵件。 同樣地,網頁伺服器會處理用戶端 HTML 或 ASP 要求、資料庫伺服器處理用戶端 SQL 要求和 BizTalk Server,可以使用許多業界標準通訊協議來處理格式化為符合多個產業訊息標準的用戶端訊息。 應用程式伺服器的工作負載容量通常是由應用程式伺服器可在指定時間內處理的訊息數目來測量。 BizTalk Server 的工作負載容量同樣會測量為「每秒收到的檔」、「每秒處理的檔」和/或「每秒完成的協調流程」平均數目,例如忙碌的工作日,甚至是工作周。 Visual Studio 2010 負載測試功能可以模擬多達數百名用戶同時存取伺服器應用程式的負載配置檔。 此負載測試功能提供所選關鍵效能指標的即時計量,以及將這些計量儲存在資料庫中的能力,以供日後分析。 本檔將 Visual Studio 測試專案用於負載測試 BizTalk Server 應用程式的目的,包括如何建立單元測試、如何建立負載測試,以及如何設定負載測試以擷取判斷 BizTalk Server 應用程式的最大永續性輸送量 (MST) 所需的性能計數器數據。

建立 Visual Studio 單元測試以將檔提交至 BizTalk Server

Visual Studio 單元測試會參考 Microsoft.VisualStudio.TestTools.UnitTestinghttps://go.microsoft.com/fwlink/?LinkID=132293) 命名空間,提供數個類別來支援單元測試。 特別重要, UnitTestinghttps://go.microsoft.com/fwlink/?LinkID=132293) 命名空間包含 Microsoft.VisualStudio.TestTools.UnitTesting.TestContexthttps://go.microsoft.com/fwlink/?LinkID=208233) 類別,以儲存提供給單元測試的資訊,並使用 Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttributehttps://go.microsoft.com/fwlink/?LinkID=208235) 類別來定義 Test 方法。 為了測試 BizTalk Server 的負載,測試方法應指定要載入的訊息,以及應傳送訊息的端點/URL。 當建立對應的 BizTalk 接收位置時,端點/URL 會作為 BizTalk Server 的訊息進入點。

為了說明目的,本主題中的範例程式代碼描述使用 System.ServiceModel.ChannelFactory(TChannel) (https://go.microsoft.com/fwlink/?LinkID=208238) 類別將訊息傳送至使用 WCF net.tcp 端點的服務端點,並由 BizTalk WCF-Custom 接收配接器監視的測試方法。 測試訊息的服務端點定義於測試專案的應用程式組態 (app.config) 檔案中。

如需 Visual Studio 單元測試的詳細資訊,請參閱 單元測試的結構https://go.microsoft.com/fwlink/?LinkID=208232)。

請遵循下列各節中的步驟,建立具有單元測試的測試專案,以將檔提交至一或多部 BizTalk Server 計算機。 這些步驟是使用 Visual Studio 2010 Ultimate Edition 完成的。

設定 Visual Studio 2010 測試項目選項

  1. 啟動 Visual Studio 2010 Ultimate Edition。 單擊 [開始],指向 [ 所有程式],指向 [Microsoft Visual Studio 2010 ],然後按兩下 [Microsoft Visual Studio 2010]。

  2. 在 Visual Studio 2010 中,按兩下 [ 工具 ],然後按兩下 [ 選項 ] 以顯示 [ 選項 ] 對話方塊。

  3. 按兩下以展開 [測試工具 ],然後按兩下 [ 測試專案 ] 以顯示建立新測試項目的選項。

  4. 預設測試項目語言: 設定為 Visual C# 測試專案

  5. 在 [ 選取要新增至每個新測試專案的檔案] 選項下,預設為: 選取 [Visual C# 測試專案],然後取消核取 [單元 測試] 以外的所有 Visual C# 測試專案測試類型。

  6. 確定 以關閉 選項 對話框。

使用測試專案建立新的Visual Studio 2010方案

  1. 在 Visual Studio 2010 Ultimate 電腦上建立 C:\Projects 資料夾。

  2. 在 Visual Studio 2010 中,按兩下 [ 檔案],指向 [ 新增],然後按兩下 [ 專案 ] 以顯示 [ 新增專案 ] 對話框。

  3. [已安裝的範本] 下,按兩下以展開 [Visual C#],然後按兩下 [ 測試]。

  4. 在 [ 新增專案 ] 對話框底部,指定下列選項:

    • Name:BTSLoad

    • Location:C:\Projects\

    • 解決方案名稱:LoadTest

  5. 確定已核取 [ 建立解決方案目錄 ] 的選項,然後按兩下 [ 確定]。

  6. 將資料夾新增至 BTSLoad 專案;此資料夾將包含要提交至 BizTalk Server 的測試訊息。 在方案總管中,以滑鼠右鍵按兩下 BTSLoad專案,指向 [ 新增],然後按兩下 [ 新增資料夾]。 具有醒目提示文字 NewFolder1 的資料夾圖示會出現在 BTSLoad 專案下,輸入 TestMessages 以變更醒目提示的文字,然後按 Enter 鍵以建立資料夾 C:\Projects\LoadTest\BTSLoad\TestMessages。

更新測試專案中的程式代碼,並將應用程式組態檔新增至測試專案

  1. 在 [方案總管] 中,按一下以選取 UnitTest1.cs,並將現有的程式碼替換為下列範例程式碼清單:

    #region Using Directives
    using System;
    using System.IO;
    using System.Diagnostics;
    using System.Text;
    using System.Configuration;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    #endregion
    
    namespace Microsoft.BizTalk.Samples
    {
        [TestClass]
        public class BTSLoadTest
        {
            #region Constants
            private const int MaxBufferSize = 2097152;
            private const string Source = "BTS Load Test";
            private const string Star = "*";
            private const string TestMessageFolderParameter = "testMessageFolder";
            private const string TestMessageFolderDefault = @"C:\Projects\LoadTest\BTSLoad\TestMessages";
            private const string TestMessageFolderFormat = @"Test Message Folder = {0}";
            private const string TestXmlDocument = "testxmldocument.xml";
            #endregion
    
            #region Private Instance Fields
            private TestContext testContextInstance;
            #endregion
    
            #region Private ThreadStatic Fields
            [ThreadStatic]
            private static ChannelFactory<IRequestChannel> channelFactory;
            [ThreadStatic]
            private static IRequestChannel channel = null;
            [ThreadStatic]
            private static byte[] buffer = null;
            #endregion
    
            #region Private Static Fields
            private static string testMessageFolder = null;
            #endregion
    
            #region Public Instance Constructor
            public BTSLoadTest()
            {
            }
            #endregion
    
            #region Public Static Constructor
            static BTSLoadTest()
            {
                try
                {
                    testMessageFolder = ConfigurationManager.AppSettings[TestMessageFolderParameter];
                    if (string.IsNullOrEmpty(testMessageFolder))
                    {
                        testMessageFolder = TestMessageFolderDefault;
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                    EventLog.WriteEntry(Source, ex.Message, EventLogEntryType.Error);
                }
            }
            #endregion
    
            #region Public Properties
            /// <summary>
            ///Gets or sets the test context which provides
            ///information about and functionality for the current test run.
            ///</summary>
            public TestContext TestContext
            {
                get
                {
                    return testContextInstance;
                }
                set
                {
                    testContextInstance = value;
                }
            }
            #endregion
    
            #region Test Methods
    
            [TestMethod]
            public void BTSMessaging()
            {
                InvokeBizTalkReceiveLocation("BTSMessagingEP",
                                               testMessageFolder,
                                               TestXmlDocument,
                                               MessageVersion.Default,
                                               SessionMode.Allowed);
            }
    
            [TestMethod]
            public void BTSMessaging2()
            {
                InvokeBizTalkReceiveLocation("BTSMessagingEP2",
                                               testMessageFolder,
                                               TestXmlDocument,
                                               MessageVersion.Default,
                                               SessionMode.Allowed);
            }
    
            [TestMethod]
            public void BTSOrchestration()
            {
                InvokeBizTalkReceiveLocation("BTSOrchestrationEP",
                                               testMessageFolder,
                                               TestXmlDocument,
                                               MessageVersion.Default,
                                               SessionMode.Allowed);
            }
            #endregion
    
            #region Helper Methods
            public void InvokeBizTalkReceiveLocation(string endpointConfigurationName,
                                               string requestMessageFolder,
                                               string requestMessageName,
                                               MessageVersion messageVersion,
                                               SessionMode sessionMode)
            {
                XmlTextReader xmlTextReader = null;
                Message requestMessage = null;
                Message responseMessage = null;
    
                try
                {
                    if (channel == null || channel.State != CommunicationState.Opened)
                    {
                        channelFactory = new ChannelFactory<IRequestChannel>(endpointConfigurationName);
                        channelFactory.Endpoint.Contract.SessionMode = sessionMode;
                        channel = channelFactory.CreateChannel();
                    }
                    if (buffer == null)
                    {
                        string path = Path.Combine(requestMessageFolder, requestMessageName);
    
                        string message;
                        using (StreamReader reader = new StreamReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
                        {
                            message = reader.ReadToEnd();
                        }
                        buffer = Encoding.UTF8.GetBytes(message);
                    }
                    MemoryStream stream = new MemoryStream(buffer);
                    xmlTextReader = new XmlTextReader(stream);
                    requestMessage = Message.CreateMessage(messageVersion, Star, xmlTextReader);
                    TestContext.BeginTimer(requestMessageName);
                    responseMessage = channel.Request(requestMessage);
                }
                catch (FaultException ex)
                {
                    HandleException(ex);
                    throw;
                }
                catch (CommunicationException ex)
                {
                    HandleException(ex);
                    throw;
                }
                catch (TimeoutException ex)
                {
                    HandleException(ex);
                    throw;
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                    throw;
                }
                finally
                {
                    TestContext.EndTimer(requestMessageName);
                    CloseObjects(xmlTextReader,
                                 requestMessage,
                                 responseMessage);
                }
            }
    
            private void HandleException(Exception ex)
            {
                try
                {
                    Trace.WriteLine(ex.Message);
                    EventLog.WriteEntry(Source, ex.Message, EventLogEntryType.Error);
                }
                catch (Exception)
                {
                }
            }
    
            private void CloseObjects(XmlTextReader xmlTextReader,
                               Message requestMessage,
                               Message responseMessage)
            {
                try
                {
                    if (xmlTextReader != null)
                    {
                        xmlTextReader.Close();
                    }
                    if (requestMessage != null)
                    {
                        requestMessage.Close();
                    }
                    if (responseMessage != null)
                    {
                        responseMessage.Close();
                    }
                }
                catch (Exception)
                {
                }
            }
    
            #endregion
        }
    }
    
  2. 將應用程式群組態檔新增至測試專案:

    1. 在方案總管中,以滑鼠右鍵點擊 BTSLoad 專案,指向 [ 新增 ],然後點擊 [ 新增項目]。

    2. 在 [ 新增專案 ] 對話框的 [已安裝的範本] 底下,按兩下 [ 一般]。

    3. 在顯示的專案中,按兩下以選取 [應用程式組態檔 ],然後按下 [ 新增]。

    4. 在 [方案總管] 中,選取 app.config 檔案,並將 app.config 檔案的內容取代為下列範例程式代碼清單:

      這很重要

      針對此檔案中定義的每個用戶端端點,BizTalk Server Computer 是代表實際BizTalk Server電腦名稱的佔位元,您將以該名稱進行負載測試。

      
      <configuration>
        <system.serviceModel>
          <!-- Bindings used by client endpoints -->
          <bindings>
            <netTcpBinding>
              <binding name="netTcpBinding" closeTimeout="01:10:00" openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="100" maxBufferPoolSize="1048576" maxBufferSize="10485760" maxConnections="400" maxReceivedMessageSize="10485760">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
                <security mode="None">
                  <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                  <message clientCredentialType="Windows" />
                </security>
              </binding>
            </netTcpBinding>
          </bindings>
          <client>
            <!-- Client endpoints used to exchange messages with WCF Receive Locations -->
      
            <!-- BTSMessagingEP -->
            <endpoint address="net.tcp://BizTalk Server Computer:8123/btsloadtest" binding="netTcpBinding" bindingConfiguration="netTcpBinding" contract="System.ServiceModel.Channels.IRequestChannel" name="BTSMessagingEP" />
            <endpoint address="net.tcp://BizTalk Server Computer:8123/btsloadtest" binding="netTcpBinding" bindingConfiguration="netTcpBinding" contract="System.ServiceModel.Channels.IRequestChannel" name="BTSMessagingEP" />
      
            <!-- BTSOrchestrationEP -->
            <endpoint address="net.tcp://BizTalk Server Computer:8122/btsloadtest" binding="netTcpBinding" bindingConfiguration="netTcpBinding" contract="System.ServiceModel.Channels.IRequestChannel" name="BTSOrchestrationEP" />
          </client>
        </system.serviceModel>
        <appSettings>
          <!-- Folder containing test messages -->
          <add key="testMessageFolder" value="C:\Projects\LoadTest\BTSLoad\TestMessages" />
          <add key="ClientSettingsProvider.ServiceUri" value="" />
        </appSettings>
      </configuration>
      
    5. 單擊 Visual Studio 2010 中的 [ 檔案 ] 功能表,然後按兩下 [ 全部儲存]。

將測試訊息新增至專案

針對此範例的目的,BizTalk Server 接收位置和傳送埠會設定為使用通過管線,且不會執行任何文件驗證。 請遵循下列步驟,將測試訊息新增至專案:

  1. 開啟記事本。 按兩下 [開始],按兩下 [執行],然後在 [執行] 對話框中輸入記事本

  2. 將下列文字複製到 [記事本],然後儲存為 “C:\Projects\LoadTest\BTSLoad\TestMessages\TestXmlDocument.xml”

    <BTSLoadTest xmlns="http://Microsoft.BizTalk.Samples.BTSLoadTest">
    <MessageText>
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    This is sample message text. This is sample message text. This is sample message text. This is sample message text.
    </MessageText>
    </BTSLoadTest>
    
  3. 關閉記事本。

這很重要

如果使用多個負載測試代理程式計算機進行負載測試,則必須在每個負載測試代理程式計算機上使用相同的檔名,將此檔案儲存到相同的路徑。

將必要的參考新增至專案並建置測試專案

  1. 在方案總管中,以滑鼠右鍵按兩下 BTSLoad 專案的 [參考 ] 資料夾,然後按兩下 [ 新增參考]。

  2. 在 [新增參考] 對話框中,按兩下 [.NET ] 索引標籤,然後使用 CTRL+按兩下鍵盤/滑鼠組合,同時選取下列 .NET 命名空間:

    • System.Configuration

    • System.Runtime.Serialization

    • System.ServiceModel.Channels

    • System.ServiceModel

    • System.Web.Extensions

    • System.Xml

  3. 選取命名空間之後,按兩下 [確定 ] 將這些元件新增為BTSLoad測試項目的參考。

  4. 以滑鼠右鍵點擊 BTSLoad 專案,然後點擊 建置 以編譯專案為 BTSLoad 元件。