繁体   English   中英

使用Json上下文参数的Visual Studio加载CPU使用率激增

[英]Visual Studio Load CPU usage skyrockets with Json Context Parameters

我使用Visual Studio 2015记录了负载测试所使用的WebPerformance测试。 初始记录后,我可以仅以25%的CPU使用率运行20个同时用户。 但是,我正在测试的网站使用Json,因此为了使测试更加现实,我为Json添加了自定义提取规则:

[DisplayName("JSON Extraction Rule")]
[Description("Extracts the specified JSON value from an object.")]
public class JsonExtractionRule : ExtractionRule
{
    [DisplayName("Name/path of attribute")]
    [Description("String to fetch the attribute from the Json Object.")]
    public string Name { get; set; }

    [DisplayName("Fetch 4 first characters only")]
    [Description("Set to true if only the first 4 characters of the attribute should be fetched.")]
    public Boolean fourFirstCharacters { get; set; }

    public override void Extract(object sender, ExtractionEventArgs e)
    {
        if (e.Response.BodyString != null)
        {
            var json = e.Response.BodyString;

            if (json.StartsWith("["))
            {
                json = "{\"array\":" + json + "}";
            }

            var data = JObject.Parse(json);

            if (data != null)
            {
                var attribute = data.SelectToken(Name).ToString();
                if (fourFirstCharacters)
                {
                    e.WebTest.Context.Add(this.ContextParameterName, attribute.Substring(0, 4));
                } else
                {
                    e.WebTest.Context.Add(this.ContextParameterName, attribute);
                }                   
                e.Success = true;
                return;
            }
        }

        e.Success = false;
        e.Message = String.Format(CultureInfo.CurrentCulture, "Not Found: {0}", Name);
    }
}

我用它从响应中提取了几个Json属性,并使用上下文参数将它们添加到后续请求中。

在添加了一些类似的提取并将一些上下文参数传递给请求之后,我的负载测试仅使用5个同时用户就达到了100%的CPU使用率。

在WebPerformance测试中,上述提取规则使用了大约20次,而在任何单个响应中,使用了0-5次。 Json响应和请求的长度约为2k个字符。 最大的提取包含大约500个字符。

我能理解响应时间是否增加了,或者是由于更现实的Json通过了类似的结果。 但是我不明白为什么CPU使用率飞速增长? 请求的响应时间会影响负载测试的CPU使用率吗?

提取+上下文参数是否是在请求中实现Json的一种不好的方式(在CPU使用情况下)? 有没有更聪明的方法可以节省CPU使用率?

CPU使用率的大部分似乎是由于在验证规则中使用上述自定义Json提取引起的。 我删除了它,因为验证不是很关键,并且一切运行都更加顺利。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM