您对localhost的虚拟请求的解决方案与我所知的一样好。但是这里有一个替代方案。
两个部分:
(1)一个负载测试插件负载测试开始时间复制到Web测试上下文:
[DisplayName("Load Test Start Time")]
[Description("Record the load test start time in the context of new web test iterations")]
public class LoadTestStartTime : ILoadTestPlugin
{
public const string LoadTestStartTimeContextParameterName = "$LoadTestStartTime";
public void Initialize(LoadTest loadTest)
{
DateTime startTime = DateTime.Now;
// Copy the load test start time into the context of each new test
loadTest.TestStarting += (sender, e) =>
{
e.TestContextProperties[LoadTestStartTimeContextParameterName] = startTime;
};
}
}
(2)一个网络测试插件,获取其延迟时间从中可以得到一个静态值或上下文参数,并可以HH属性:MM:SS.SSS或整数毫秒:
[DisplayName("Delay WebTest")]
[Description("Delay the start of this WebTest by a certain amount of time.")]
public class DelayWebTest : WebTestPlugin
{
[Description("Time to delay this WebTest, in HH:MM:SS.SSS time format or in ms (e.g. 00:01:23.456 or 83456 for 1m 23.456s delay)")]
public string Delay { get; set; }
public override void PreWebTest(object sender, PreWebTestEventArgs e)
{
DateTime start;
// Make sure this works when not in a load test
if (!e.WebTest.Context.ContainsKey("$LoadTestUserContext"))
start = DateTime.Now;
else
start = (DateTime)(e.WebTest.Context[LoadTestStartTime.LoadTestStartTimeContextParameterName]);
TimeSpan elapsed = DateTime.Now - start;
TimeSpan delay;
// Support {{context parameter}} substitutions
if (Delay.StartsWith("{{") && Delay.EndsWith("}}"))
{
string key = Delay.Substring(2, Delay.Length - 4);
object value = e.WebTest.Context[key];
if (value is TimeSpan) delay = (TimeSpan)value;
else if (value is int) delay = new TimeSpan(0, 0, 0, 0, (int)value);
else if (value is string) delay = ParseDelayFromString(value as string);
else throw new ArgumentException("Delay value must be TimeSpan, int or string. Found: " + value.GetType());
}
else delay = ParseDelayFromString(Delay);
TimeSpan sleep = delay - elapsed;
if (sleep > new TimeSpan(0))
{
e.WebTest.AddCommentToResult("Current time: " + DateTime.Now);
e.WebTest.AddCommentToResult("About to sleep for " + sleep);
System.Threading.Thread.Sleep(sleep);
e.WebTest.AddCommentToResult("Current time: " + DateTime.Now);
}
}
public static TimeSpan ParseDelayFromString(string s)
{
TimeSpan ts;
if (TimeSpan.TryParse(s, out ts)) return ts;
int i;
if (int.TryParse(s, out i)) return new TimeSpan(0, 0, 0, 0, (int)i);
throw new FormatException("Could not parse value as TimeSpan or int: " + s);
}
}
这似乎为我工作。
但是它使用Thread.Sleep,“...不推荐用于 Web测试是因为它是一个阻塞API,并且多个Web测试可以共享一个线程,因此它可能会对多个vuser产生不利影响。 “ (See Visual Studio Performance Testing Quick Reference Guide)第187页。
因此,对于单用户负载测试以及对于虚拟用户数量不超过线程的小负载,这肯定是可以的,但很可能会导致更大负载的结果。
(*我并不知道线程模型的工作原理不过)
这个答案的主要部分是'了Thread.Sleep(...)'调用,它曾是我的第一件事情。正如你在答案中所说的那样,它阻止了整个线程,因此只能为极少数的虚拟用户工作。请在VSPTQRG中提供页码。 – AdrianHHH