简体   繁体   English

通过聚合SQL审计记录来衡量应用程序性能

[英]Measure application performance by aggregating SQL audit records

Suppose there is a simple audit table with two columns (in production there are more columns): 假设有一个包含两列的简单审计表(在生产中有更多列):

ID | Date

When the request is processed, we add a record into this table. 处理请求时,我们在此表中添加一条记录。 Requests are processed in batches, there can be any number of items in a batch. 请求分批处理,批处理中可以有任意数量的项目。 For each item, we will add a record. 对于每个项目,我们将添加一条记录。 There will be at least 2 second delay between batches (the number is configurable). 批次之间将存在至少2秒的延迟(该数量是可配置的)。

The performance is measured by how fast we can process requests, per unit of time, for example, per second. 性能是通过每单位时间(例如每秒)处理请求的速度来衡量的。 Consider this sample data (2 clusters, the number of items is equal for demo purposes only): 考虑这个示例数据(2个集群,项目数量相同,仅用于演示目的):

--2016-01-29 10:27:25.603
--2016-01-29 10:27:25.620
--2016-01-29 10:27:25.637
--2016-01-29 10:27:25.653
--2016-01-29 10:27:25.723
--Avg time between requests = 24ms

--2016-01-29 10:27:34.647
--2016-01-29 10:27:34.667
--2016-01-29 10:27:34.680
--2016-01-29 10:27:34.690
--2016-01-29 10:27:34.707
--Avg time = 12ms

We can say that at worst, 41.67 requests can be processed per second, and 83.33 at best. 我们可以说,在最坏的情况下,每秒可以处理41.67个请求,最多可以处理83.33个请求。 Would be nice to know the average batch performance as well. 很高兴知道平均批次性能。

Question. 题。 Is it possible to get these metrics using T-SQL alone and how? 是否可以单独使用T-SQL获取这些指标以及如何使用?

EDIT: To make results statistically significant, it might be useful to discard batches than are less than 10 items in size (configurable). 编辑:要使结果具有统计显着性,丢弃批次可能比小于10个项目(可配置)更有用。

Perhaps I've over simplified your request, but consider the following 也许我已经过度简化了您的请求,但请考虑以下内容

Declare @YourTable table (ID int,Date datetime)
Insert Into @YourTable values
( 1,'2016-01-29 10:27:25.603'),
( 2,'2016-01-29 10:27:25.620'),
( 3,'2016-01-29 10:27:25.637'),
( 4,'2016-01-29 10:27:25.653'),
( 5,'2016-01-29 10:27:25.723'),
( 6,'2016-01-29 10:27:34.647'),
( 7,'2016-01-29 10:27:34.667'),
( 8,'2016-01-29 10:27:34.680'),
( 9,'2016-01-29 10:27:34.690'),
(10,'2016-01-29 10:27:34.707')


Declare @BatchSecondsGap int = 2  -- Seconds Between Batches
Declare @MinObservations int = 5  -- Batch must n or greater

;with cte as (
      Select *,Cnt = sum(1) over (Partition By Batch)
       From  (
              Select *,Batch = sum(Flg) over (Order By Date)
               From (
                     Select ID,Date
                           ,Flg = case when DateDiff(SECOND,Lag(Date,1,null) over (Order By Date),Date)>=@BatchSecondsGap then 1 else 0 end
                           ,MS  = case when DateDiff(SECOND,Lag(Date,1,Date) over (Order By Date),Date)>=@BatchSecondsGap then 0 else DateDiff(MILLISECOND,Lag(Date,1,Date) over (Order By Date),Date) end
                      From  @YourTable
                     ) A
             ) B
 )
Select Title    = 'Total'
      ,DateR1   = min(Date)
      ,DateR2   = max(Date)
      ,BatchCnt = count(Distinct Batch)
      ,TransCnt = count(*)
      ,MS_Ttl   = sum(MS)
      ,MS_Avg   = avg(MS*1.0)
      ,MS_Std   = stdev(MS)
 From  cte
 Where Cnt>=@MinObservations
Union All
Select Title    = concat('Batch ',Batch)
      ,DateR1   = min(Date)
      ,DateR2   = max(Date)
      ,BatchCnt = count(Distinct Batch)
      ,TransCnt = count(*)
      ,MS_Ttl   = sum(MS)
      ,MS_Avg   = avg(MS*1.0)
      ,MS_Std   = stdev(MS)
 From  cte
 Where Cnt>=@MinObservations
 Group By Batch

Returns 返回

在此输入图像描述


The image below illustrates that you won't be penalized for the time between batches, so then it becomes a simple aggregation for the final results 下图说明您不会因批次之间的时间而受到惩罚,因此它将成为最终结果的简单聚合

在此输入图像描述

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

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