共用方式為


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

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

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

Visual Studio 單元測試會參考 Microsoft.VisualStudio.TestTools.UnitTesting (https://go.microsoft.com/fwlink/?LinkID=132293) 命名空間,提供數個類別來支援單元測試。 特別重要, UnitTesting (https://go.microsoft.com/fwlink/?LinkID=132293) 命名空間包含 Microsoft.VisualStudio.TestTools.UnitTesting.TestCoNtext (https://go.microsoft.com/fwlink/?LinkID=208233) 類別來儲存提供給單元測試和 Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute (https://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電腦是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接收位置 (s) ,並將傳送埠 () 設定為使用通過管線,且不會執行任何檔驗證。 請遵循下列步驟,將測試訊息新增至專案:

  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 元件。