如何延迟执行网络测试的第一个请求? [英] How to delay execution of the first request of a web test?

查看:83
本文介绍了如何延迟执行网络测试的第一个请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2013 Web性能测试中,如何在指定的时间段内将".webtest"中的第一个请求发送延迟?我的网络测试是负载测试的一部分.网络测试的第一个请求应在数据源字段指定的延迟时间后发出.确定何时发出请求很简单:

In a Visual Studio 2013 Web Performance test, how can I delay sending the very first request in a ".webtest" for a period of time that I specify? My web test is part of a load test. The first request of the web test should be issued after a delay period specified by a data source field. Determining when the request should be issued is simply:

delayPeriod = dataSourceValue - ( timeNow - loadTestStartTime )

将此delayPeriod写入请求的思考时间会导致正确的延迟.不幸的是,在收到请求的响应后 会考虑思考时间.因此,很容易将Web测试的第二个请求延迟到所需的时间.我想延迟之前第一个请求.

Writing this delayPeriod into the think time for a request causes the correct delay. Unfortunately, think times are applied after the response to a request is received. Hence it is easy to delay the second request of the web test until the desired time. I want to delay before the first request.

作为一种解决方法,我在http://localhost/中包含了一个虚拟的第一个请求,并将预期的状态码结果设置为404.所需的延迟设置为对此请求的思考时间.但这会向localhost添加不必要的请求.

As a workaround, I have included a dummy first request to http://localhost/ and set the expected status code result to 404. The required delay is set as a think time on this request. But this add an unwanted request to localhost.

背景: 我们有一个来自Web服务器的日志文件.它提供了每个请求的URL和时间.我们希望以与记录请求时相同的速率重复该日志文件中的请求.

Background: We have a log file from a web server. It gives the URL and the time for each request. We want to repeat the requests in that log file at the same rate as when they were recorded.

推荐答案

确定,再拍一次.不过裸露的骨头,没有错误检查.

OK, another shot at it. Bare bones though, no error checking.

LoadTestStartTime负载测试插件将对负载测试本身和负载测试开始时间的引用放入Web测试上下文中.

The LoadTestStartTime load test plugin puts a reference to the load test itself and the load test start time into the web test context.

public class LoadTestStartTime : ILoadTestPlugin
{
    public const string LoadTestContextParameterName = "$LoadTest";
    public const string LoadTestStartTimeContextParameterName = "$LoadTestStartTime";

    public void Initialize(LoadTest loadTest)
    {
        DateTime startTime = default(DateTime);

        loadTest.LoadTestStarting += (sender, e) => { startTime = DateTime.Now; };

        loadTest.TestStarting += (sender,e) => {
            e.TestContextProperties[LoadTestContextParameterName] = loadTest;
            e.TestContextProperties[LoadTestStartTimeContextParameterName] = startTime;
        };
    }
}

DelayWebTest Web测试插件必须附加到包含一个虚拟请求的新Web测试中(该虚拟请求将不会执行).此新的Web测试必须作为初始化测试"添加到负载测试方案"测试组合中.

The DelayWebTest web test plugin must be attached to a new web test containing one dummy request (the dummy request will not be executed). This new web test must be added to the Load Test Scenario test mix as the "Initialize Test".

该插件从上下文参数中读取延迟时间,然后将方案的当前负载设置为0.然后,它设置了一个计时器,该计时器在正确计算的延迟之后调用事件处理程序,这将还原负载配置文件,从而允许测试继续进行.它使用锁定,因此只有第一个虚拟用户才能执行负载配置文件逻辑,而其他用户则等待.

The plugin reads the delay time from a context parameter, then sets the scenario's current load to 0. It then sets up a timer which calls an event handler after the properly calculated delay, which restores the load profile allowing the test to proceed. It uses locking so that only the first virtual user will execute the load profile logic while the others wait.

public class DelayWebTest : WebTestPlugin
{
    public string ContextParameterName { get; set; }

    private bool completed = false;
    private object _lock = new object();

    public override void PreRequest(object sender, PreRequestEventArgs e)
    {
        e.Instruction = WebTestExecutionInstruction.Skip;
        if (!completed)
        {
            lock (_lock)
            {
                if (!completed)
                {
                    if (e.WebTest.Context.WebTestUserId > 0) return;
                    LoadTest loadTest = (LoadTest)e.WebTest.Context[LoadTestStartTime.LoadTestContextParameterName];
                    DateTime startTime = (DateTime)e.WebTest.Context[LoadTestStartTime.LoadTestStartTimeContextParameterName];
                    TimeSpan delay = TimeSpan.Parse(e.WebTest.Context[ContextParameterName].ToString());
                    TimeSpan timer = delay - (DateTime.Now - startTime);
                    if (timer > default(TimeSpan))
                    {
                        var loadProfile = loadTest.Scenarios[0].LoadProfile.Copy();
                        loadTest.Scenarios[0].CurrentLoad = 0;
                        Timer t = new Timer() { AutoReset = false, Enabled = true, Interval = timer.TotalMilliseconds };
                        t.Elapsed += (_sender, _e) =>
                        {
                            loadTest.Scenarios[0].LoadProfile = loadProfile;
                            t.Stop();
                        };
                        t.Start();
                    }
                    completed = true;
                }
            }
        }
    }
}

似乎可以正常工作. 50位用户(延迟30秒):

Seems to work. 50 users with 30 second delay:

这篇关于如何延迟执行网络测试的第一个请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆