0

我需要从负载测试插件中获取负载测试的测试迭代次数,其中我有一个LoadTest对象的实例。我搜索了LoadTest对象的属性,感觉与通常用于配置负载测试的树视图编辑器相比,缺少很多东西。

我已经将测试迭代的数量再次定义为 Context 参数并将其传递给我的 Web 测试,但这感觉像是一个 hack,因为我正在复制数据。

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.
        };
    }     
}
4

2 回答 2

2

使用 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);
                }

            }
        }
    }
}

同样,您可以读取其他值。

于 2015-10-06T13:36:32.893 回答
1

一个 web 测试有当前的迭代次数webTest.Context.WebTestIteration(也作为一个名为 的上下文参数$WebTestIteration)。

LoadTest 可以访问TestStartingEventArgs对象中的当前迭代次数:

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

为了向自己证明这些值是相同的,并且没有意外的行为,比如在不同场景中重复使用数字,我(编辑:重新)编写了这些插件,并进行了检查。

(感谢@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));
    }
}
于 2014-02-12T04:44:57.797 回答