@@ -303,3 +303,124 @@ def test_compute_derivative_rows_mem_usage(self):
303
303
def test_compute_derivative_rows_benchmark (self , benchmark ):
304
304
sm = StatementMetrics ()
305
305
benchmark (self .__run_compute_derivative_rows , sm )
306
+
307
+ def test_compute_derivative_rows_with_execution_indicators (self ):
308
+ sm = StatementMetrics ()
309
+
310
+ def key (row ):
311
+ return (row ['query' ], row ['db' ], row ['user' ])
312
+
313
+ metrics = ['calls' , 'total_time' , 'rows' ]
314
+ execution_indicators = ['calls' ]
315
+
316
+ # Initial state
317
+ rows1 = [
318
+ {'calls' : 10 , 'total_time' : 1000 , 'rows' : 50 , 'query' : 'SELECT 1' , 'db' : 'test' , 'user' : 'user1' },
319
+ {'calls' : 5 , 'total_time' : 500 , 'rows' : 25 , 'query' : 'SELECT 2' , 'db' : 'test' , 'user' : 'user1' },
320
+ ]
321
+
322
+ sm .compute_derivative_rows (rows1 , metrics , key = key , execution_indicators = execution_indicators )
323
+
324
+ # Second run - only duration changes (should be ignored)
325
+ rows2 = [
326
+ {'calls' : 10 , 'total_time' : 1001 , 'rows' : 50 , 'query' : 'SELECT 1' , 'db' : 'test' , 'user' : 'user1' },
327
+ {'calls' : 5 , 'total_time' : 501 , 'rows' : 25 , 'query' : 'SELECT 2' , 'db' : 'test' , 'user' : 'user1' },
328
+ ]
329
+ assert [] == sm .compute_derivative_rows (rows2 , metrics , key = key , execution_indicators = execution_indicators )
330
+
331
+ # Third run - calls change (should be included)
332
+ rows3 = [
333
+ {'calls' : 11 , 'total_time' : 1002 , 'rows' : 51 , 'query' : 'SELECT 1' , 'db' : 'test' , 'user' : 'user1' },
334
+ {'calls' : 5 , 'total_time' : 502 , 'rows' : 25 , 'query' : 'SELECT 2' , 'db' : 'test' , 'user' : 'user1' },
335
+ ]
336
+ result = sm .compute_derivative_rows (rows3 , metrics , key = key , execution_indicators = execution_indicators )
337
+ assert len (result ) == 1
338
+ assert result [0 ]['calls' ] == 1
339
+ assert result [0 ]['total_time' ] == 1
340
+ assert result [0 ]['rows' ] == 1
341
+
342
+ def test_compute_derivative_rows_with_multiple_execution_indicators (self ):
343
+ sm = StatementMetrics ()
344
+
345
+ def key (row ):
346
+ return (row ['query' ], row ['db' ], row ['user' ])
347
+
348
+ metrics = ['calls' , 'executions' , 'total_time' , 'rows' ]
349
+ execution_indicators = ['calls' , 'executions' ]
350
+
351
+ # Initial state
352
+ rows1 = [
353
+ {
354
+ 'calls' : 10 ,
355
+ 'executions' : 10 ,
356
+ 'total_time' : 1000 ,
357
+ 'rows' : 50 ,
358
+ 'query' : 'SELECT 1' ,
359
+ 'db' : 'test' ,
360
+ 'user' : 'user1' ,
361
+ },
362
+ ]
363
+
364
+ sm .compute_derivative_rows (rows1 , metrics , key = key , execution_indicators = execution_indicators )
365
+
366
+ # Second run - only one execution indicator changes
367
+ rows2 = [
368
+ {
369
+ 'calls' : 11 ,
370
+ 'executions' : 10 ,
371
+ 'total_time' : 1001 ,
372
+ 'rows' : 50 ,
373
+ 'query' : 'SELECT 1' ,
374
+ 'db' : 'test' ,
375
+ 'user' : 'user1' ,
376
+ },
377
+ ]
378
+ result = sm .compute_derivative_rows (rows2 , metrics , key = key , execution_indicators = execution_indicators )
379
+ assert len (result ) == 1
380
+ assert result [0 ]['calls' ] == 1
381
+ assert result [0 ]['executions' ] == 0
382
+ assert result [0 ]['total_time' ] == 1
383
+ assert result [0 ]['rows' ] == 0
384
+
385
+ # Third run - both execution indicators change
386
+ rows3 = [
387
+ {
388
+ 'calls' : 12 ,
389
+ 'executions' : 11 ,
390
+ 'total_time' : 1002 ,
391
+ 'rows' : 51 ,
392
+ 'query' : 'SELECT 1' ,
393
+ 'db' : 'test' ,
394
+ 'user' : 'user1' ,
395
+ },
396
+ ]
397
+ result = sm .compute_derivative_rows (rows3 , metrics , key = key , execution_indicators = execution_indicators )
398
+ assert len (result ) == 1
399
+ assert result [0 ]['calls' ] == 1
400
+ assert result [0 ]['executions' ] == 1
401
+ assert result [0 ]['total_time' ] == 1
402
+ assert result [0 ]['rows' ] == 1
403
+
404
+ def test_compute_derivative_rows_with_invalid_execution_indicators (self ):
405
+ sm = StatementMetrics ()
406
+
407
+ def key (row ):
408
+ return (row ['query' ], row ['db' ], row ['user' ])
409
+
410
+ metrics = ['calls' , 'total_time' , 'rows' ]
411
+
412
+ # Test with empty execution indicators
413
+ rows1 = [
414
+ {'calls' : 10 , 'total_time' : 1000 , 'rows' : 50 , 'query' : 'SELECT 1' , 'db' : 'test' , 'user' : 'user1' },
415
+ ]
416
+ rows2 = [
417
+ {'calls' : 11 , 'total_time' : 1001 , 'rows' : 51 , 'query' : 'SELECT 1' , 'db' : 'test' , 'user' : 'user1' },
418
+ ]
419
+
420
+ # Empty execution indicators should behave like no execution indicators specified
421
+ _ = sm .compute_derivative_rows (rows1 , metrics , key = key , execution_indicators = [])
422
+ result = sm .compute_derivative_rows (rows2 , metrics , key = key , execution_indicators = [])
423
+ assert len (result ) == 1
424
+ assert result [0 ]['calls' ] == 1
425
+ assert result [0 ]['total_time' ] == 1
426
+ assert result [0 ]['rows' ] == 1
0 commit comments