簡體   English   中英

如何從負載測試插件中獲取負載測試中的測試迭代次數?

[英]How can I get the number of test iterations in a load test from within a load test plugin?

我需要從負載測試插件(其中有一個LoadTest對象的實例)中獲取負載測試的測試迭代次數。 我已經搜索了LoadTest對象的屬性,與通常用於配置負載測試的樹視圖編輯器相比,感覺好像缺少了很多東西。

我已經將測試迭代的次數再次定義為Context參數,並將其傳遞給我的Web測試,但這感覺像是黑客,因為我正在復制數據。

class MyLoadTestPlugin : ILoadTestPlugin
{
    private LoadTest loadTest;

    public void Initialize(LoadTest test)
    {
        loadTest = test;

        loadTest.TestStarting += (_, e) =>
        {
            // Get # of Test Iterations in load test here,
            // "loadTest" object does not have nearly as
            // many properties as it should, compared to
            // the tree view editor.
        };
    }     
}

使用LoadTestPlugin讀取.loadtest文件,該文件是XML文件。 這是讀取.loadtest文件中的TotalIterations的示例。

using System;
using Microsoft.VisualStudio.TestTools.LoadTesting;
using System.IO;
using System.Xml;

namespace LoadTest
{
    public class LoadTestPluginImpl : ILoadTestPlugin
    {

        LoadTest mLoadTest;

        static int TotalIterations;
        public void Initialize(LoadTest loadTest)
        {
            mLoadTest = loadTest;
            //connect to the TestStarting event.
            mLoadTest.TestStarting += new EventHandler<TestStartingEventArgs>(mLoadTest_TestStarting);
            ReadTestConfig();
        }

        void mLoadTest_TestStarting(object sender, TestStartingEventArgs e)
        {
            //When the test starts, copy the load test context parameters to
            //the test context parameters
            foreach (string key in mLoadTest.Context.Keys)
            {
                e.TestContextProperties.Add(key, mLoadTest.Context[key]);
            }
            //add the CurrentTestIteration to the TestContext
            e.TestContextProperties.Add("TestIterationNumber", e.TestIterationNumber);
            //add the TotalIterations to the TestContext and access from the Unit Test.
            e.TestContextProperties.Add("TotalIterations", TotalIterations);

        }

        void ReadTestConfig()
        {
            string filePath = Path.Combine(Environment.CurrentDirectory, mLoadTest.Name + ".loadtest");

            if (File.Exists(filePath))
            {
                string runSettings = mLoadTest.RunSettings.Name;
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(filePath);

                XmlElement root = xdoc.DocumentElement;

                string xmlNameSpace = root.GetAttribute("xmlns");
                XmlNamespaceManager xmlMgr = new XmlNamespaceManager(xdoc.NameTable);
                if (!string.IsNullOrWhiteSpace(xmlNameSpace))
                {
                    xmlMgr.AddNamespace("lt", xmlNameSpace);
                }

                var nodeRunSettings = xdoc.SelectSingleNode(string.Format("//lt:LoadTest/lt:RunConfigurations/lt:RunConfiguration[@Name='{0}']", runSettings), xmlMgr);
                //var nodeRunSettings = xdoc.SelectSingleNode(string.Format("//lt:LoadTest", runSettings), xmlMgr);
                if (nodeRunSettings != null)
                {
                    TotalIterations = Convert.ToInt32(nodeRunSettings.Attributes["TestIterations"].Value);
                }

            }
        }
    }
}

同樣,您可以讀取其他值。

Web測試在webTest.Context.WebTestIteration具有當前的迭代編號(並且還作為名為$WebTestIteration的上下文參數)。

LoadTest可以訪問TestStartingEventArgs對象中的當前迭代編號:

loadTest.TestStarting += ( (sender, e) =>
{
    int iteration = e.TestIterationNumber;
};

為了向自己證明這些值相同並且沒有意外的行為(例如數字在不同情況下可重復使用),我(EDIT:re-)編寫了這些插件,並進行了檢出。

(感謝@AdrianHHH指出先前的代碼不完整)

public class LoadTestIteration : ILoadTestPlugin
{
    List<int> usedTestIterationNumbers = new List<int>();
    public void Initialize(LoadTest loadTest)
    {
        loadTest.TestStarting += (sender, e) =>
        {
            e.TestContextProperties["$LoadTest.TestIterationNumber"] = e.TestIterationNumber;
            System.Diagnostics.Debug.Assert(!usedTestIterationNumbers.Contains(e.TestIterationNumber), "Duplicate LoadTest TestIterationNumber: " + e.TestIterationNumber);
            usedTestIterationNumbers.Add(e.TestIterationNumber);
        };
    }
}

public class TestWebTestIteration : WebTestPlugin
{
    public override void PreWebTest(object sender, PreWebTestEventArgs e)
    {
        int lti = (int)e.WebTest.Context["$LoadTest.TestIterationNumber"];
        int wti = e.WebTest.Context.WebTestIteration;
        System.Diagnostics.Debug.Assert(lti == wti, String.Format("$LoadTestIteration {0} differs from $WebTestIteration {1}", lti, wti));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM