VS2015负载测试中如何通过代码动态设置用户数与迭代次数
Hey there! I get that you want to tweak iteration counts and user loads for your web/load tests directly in code—no more digging through UI settings every time you need to adjust how many purchase operations run, or how many concurrent users simulate buying. Let's walk through exactly how to set this up for your scenario:
1. Dynamically Set Web Test Iteration Counts
If you're looking to run a purchase flow X times (like your 100-example), you have two solid options depending on whether you're using a recorded web test or a coded one:
Option 1: Coded Web Test (Direct Iteration Control)
Create a coded web test project, then build your purchase logic with a hardcoded (or config-driven) iteration count right in the code:
using Microsoft.VisualStudio.TestTools.WebTesting; using System.Collections; public class PurchaseFlowWebTest : WebTest { // Just change this number to adjust how many times the purchase runs private const int TotalPurchaseIterations = 100; public PurchaseFlowWebTest() { PreAuthenticate = true; } public override IEnumerator<WebTestRequest> GetRequestEnumerator() { // Loop through the purchase flow for our target iteration count for (int i = 0; i < TotalPurchaseIterations; i++) { // Replace this with your actual purchase request (POST to checkout, etc.) var checkoutRequest = new WebTestRequest("https://your-purchase-site.com/checkout"); checkoutRequest.Method = "POST"; // Add form data, headers, or authentication here as needed checkoutRequest.FormPostParameters.Add("productId", "123"); checkoutRequest.FormPostParameters.Add("quantity", "1"); yield return checkoutRequest; // Add any follow-up requests (like order confirmation) here } } }
Now you just need to update TotalPurchaseIterations whenever you want to change how many times the purchase runs—no UI edits required.
Option 2: Web Test Plugin (For Recorded Tests)
If you have a recorded web test and don't want to rewrite it as code, build a plugin to inject dynamic iteration logic:
using Microsoft.VisualStudio.TestTools.WebTesting; [DisplayName("Dynamic Iteration Controller")] public class DynamicIterationPlugin : WebTestPlugin { // Set this value directly in the plugin properties (or via config) public int TargetIterations { get; set; } = 100; public override void PreWebTest(object sender, PreWebTestEventArgs e) { // Store the iteration count in the test context so you can reference it later if needed e.WebTest.Context["TotalIterations"] = TargetIterations; // If you're using this with a load test, you can also tie this to load profile settings } }
Add this plugin to your recorded web test, then adjust the TargetIterations property in the plugin settings—no need to open the test editor's iteration configuration.
2. Dynamically Set Load Test Concurrent Users
For simulating multiple concurrent buyers, use a load test plugin to adjust user counts on the fly:
using Microsoft.VisualStudio.TestTools.LoadTesting; [DisplayName("Dynamic User Load Controller")] public class DynamicUserLoadPlugin : LoadTestPlugin { // Adjust this number to change how many concurrent users simulate purchases public int TargetConcurrentUsers { get; set; } = 50; public override void Initialize(object sender, InitializeEventArgs e) { // Grab your load test scenario (adjust the index if you have multiple scenarios) var purchaseScenario = e.LoadTest.Scenarios[0]; // Set the max and initial user count to your target purchaseScenario.LoadProfile.MaxUserCount = TargetConcurrentUsers; purchaseScenario.LoadProfile.InitialUserCount = TargetConcurrentUsers; // If you want a stepped load (e.g., ramp up users over time), uncomment these: // purchaseScenario.LoadProfile.StepDuration = 10; // Seconds between user increments // purchaseScenario.LoadProfile.StepUserCount = 10; // Users added each step } }
Add this plugin to your load test, then update TargetConcurrentUsers whenever you need to simulate more or fewer simultaneous buyers.
Pro Tip: Use Config Files for Even Easier Adjustments
If you want to avoid touching code entirely, move these values to an app.config file:
<appSettings> <add key="PurchaseIterations" value="100" /> <add key="ConcurrentUsers" value="50" /> </appSettings>
Then read them in your code:
using System.Configuration; // For web tests private readonly int TotalPurchaseIterations = int.Parse(ConfigurationManager.AppSettings["PurchaseIterations"]); // For load tests public int TargetConcurrentUsers { get; set; } = int.Parse(ConfigurationManager.AppSettings["ConcurrentUsers"]);
Now you can tweak numbers in the config file without opening the test code at all.
内容的提问来源于stack exchange,提问作者Reema

