使用 tSQLt 断言 MSSQL 存储过程执行时间

Assert MSSQL stored procedure execution time with tSQLt

我想创建 SQL 单元测试来测量存储过程的执行时间。有没有办法断言每次执行的持续时间?

我最终使用的解决方案:

--运行我的存储过程

exec mySP

--获取指标

  SELECT d.object_id, d.database_id, OBJECT_NAME(object_id, database_id) 'proc name',   
    d.cached_time, d.last_execution_time, d.total_elapsed_time,  
    d.total_elapsed_time/d.execution_count AS [avg_elapsed_time],  
    d.last_elapsed_time, d.execution_count  
FROM sys.dm_exec_procedure_stats AS d  
where OBJECT_NAME(object_id, database_id) = 'mySP'
ORDER BY [total_worker_time] DESC;  

--朗姆酒我的断言基于last_elapsed_timeavg_elapsed_time

您可以查看 SQLServer 2008 提供的 sys.dm_exec_procedure_stats。此 DMV 提供了一些有用的信息,例如

您也可以查看 SET STATISTICS TIME ON 以及

set statistics time on
exec usp_test
set statistics time off

您可以在消息选项卡中看到时间,如下所示

SQL Server Execution Times:
   CPU time = 171 ms,  elapsed time = 5060 ms.