我有一个数据驱动的单元测试,它从访问数据库中读取并执行数据库中的所有行。当我将它添加到负载测试并使用 5 个并发用户执行时,数据库中的所有记录都执行了 5 次。问题我面对这里是当数据库中有更多记录时,测试需要更多时间来执行。相反有没有办法限制测试只执行一个数据行?
3 回答
Are you trying to just run the test one time per row in the database and then stop? If so, you should probably avoid using your web test as a load test.
I think you have two options but I don't have my work computer in front of me to confirm.
Option 1: Create the web test like you've already done including wiring it up to the access database like you probably already have. Then convert the test to a coded web test. And change the code so that it runs once for each record in the datasource (in other words, add an outer loop to the coded webtest).
Option 2: Edit your local test run config to run the test N number of times. From the main menu go to Test/Edit Test Run Configurations, choose your test config, Select Web Test from the left pane, then change Fixed Run Count to 5. I can't confirm this right now but I believe each time the test runs it will advance to the next record as opposed to staying on the first.
我假设您只希望在有一行数据可用时运行数据测试。
我将更改驱动测试的数据以读取具有原子转换的存储过程,例如以下 SQL 代码:
BEGIN TRANSACTION
DECLARE @Id UNIQUEIDENTIFIER
SET @Id = (SELECT top 1 ID from #TestData WHERE TestRun = 0)
SELECT top 1 * FROM #TestData WHERE ID = @Id
UPDATE #TestData SET TestRun = 1 WHERE ID = @ID
COMMIT TRANSACTION
这将在每次运行测试时为您提供一个唯一的数据行,从而允许在负载测试中使用该测试。
您将不得不使用 SQL Express 而不是访问,因为我认为它不能很好地处理并发性(此处可更正)。
如果您需要更多地控制负载测试期间发生的事情,请考虑创建一个负载测试插件,该插件将允许您从以下负载测试事件中实现代码:
- LoadTestStarting
- 加载测试完成
- LoadTestWarmupComplete
- 测试开始
- 测试完成
- 超过阈值
- 心跳
- 负载测试中止
后来我在负载测试中找出了场景的测试迭代属性,以控制您想要为每个用户运行测试的次数。
在运行设置中,设置 Use test iterations = true test iterations = xxx(你需要的迭代次数)
还要在每次迭代之间暂停,在负载测试的场景属性中,设置以下属性
1)Think Profile = On 2)Think Time between test iterations = 1